8

I am using the following code to set up UIActivityViewController:

NSArray *activityItems = [NSArray arrayWithObjects:[self textMessageToShare], nil];

UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
        [activityViewController setCompletionHandler:^(NSString *activityType, BOOL completed) {
            if (completed) {
                [self sendFeedbackWithIndexPath:indexPath AndLikeType:100 AndCell:nil];
            }
        }];
[self.navigationController presentViewController:activityViewController
                                                animated:YES
                                              completion:^{
                                                  // ...
                                              }];

Issue is that when I copy a message or post to facebook or twitter or email or gmail app or to default Messages app, the new line characters that are in [self textMessageToShare] are maintained. However, if I share to other activities like WhatsApp or Viber - all the new line characters are removed, and the whole message is sent as one single line.

Whereas, if I share just text through iOS default Notes app, new line characters are maintained when shared to these apps. How would the Notes app be storing the new line characters? I am using \n as the new line character.

For my life unable to even find the reason. Can anyone help?

BufferStack
  • 549
  • 9
  • 20

5 Answers5

5

I was able to make it work by converting the newline characters to "<br/>":

_myDataString=  self.textview.text;
_myDataString= [_myDataString  stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
Dan Getz
  • 8,774
  • 6
  • 30
  • 64
  • Hey this is superb!! This does solves the issue. But now the other activities - which take \n as new line character are printing `
    `. Could you also find a way to selectively apply br replacement for only those activities that follow it?
    – BufferStack Jun 08 '15 at 11:05
  • 1
    i try with this=> NSString *strRemove = [textToShare stringByReplacingOccurrencesOfString:@"
    " withString:@" \n\n\n"]; but not getting new line in whats app textfield..
    – ramesh bhuja Jul 05 '16 at 07:33
2

Please check the new line issue in whats app share using uiactivityviewcontroller.

#import <UIKit/UIKit.h>

@interface ShareActivity : UIActivityItemProvider

@property (nonatomic, strong) NSString *message;
@property (nonatomic, strong) NSArray *activities;

@end


#import "ShareActivity.h"

@implementation ShareActivity
@synthesize message = _message;
@synthesize activities = _activities;

 - (id) activityViewController:(UIActivityViewController     *)activityViewController itemForActivityType:(NSString *)activityType
{
if([activityType    isEqualToString:@"net.whatsapp.WhatsApp.ShareExtension"])
{
return [self.message stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
}
else if ([self.activities containsObject:activityType]) 
{
return [self.message stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
}
else 
{
    return self.message;
}
return nil;
}

- (id) activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController 
{ 
return @"";
}

In View Controller Class on any action button to pop up the share view Apply this action on any button action

-(void)shareAction
{
ShareActivity *shareObj = [[ShareActivity alloc] initWithPlaceholderItem:@""];

NSString *message = @"New\nLine\nText\nMessage";
[shareObj setMessage:message];

NSArray* dataToShare = @[shareObj];
NSArray *excludeActivities = @[UIActivityTypePrint,UIActivityTypeOpenInIBooks,UIActivityTypeAddToReadingList,UIActivityTypePostToTencentWeibo,UIActivityTypeSaveToCameraRoll,UIActivityTypeAirDrop];

UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:dataToShare applicationActivities:nil];
activityVC.excludedActivityTypes = excludeActivities;
[activityVC setCompletionHandler:^(NSString *act, BOOL done)
{

NSString *ServiceMsg = nil;
if ( [act isEqualToString:UIActivityTypeMail] )
{
ServiceMsg = @"Mail sent!";
}
else if ( [act isEqualToString:UIActivityTypePostToTwitter] )
{
ServiceMsg = @"Post on twitter, ok!";
}
else if ( [act isEqualToString:UIActivityTypePostToFacebook] )
{
ServiceMsg = @"Post on facebook, ok!";
}
else if ( [act isEqualToString:UIActivityTypeCopyToPasteboard] )
{
ServiceMsg = @"Message copy to pasteboard";
}
else if ( [act isEqualToString:UIActivityTypePostToFlickr] )
{
ServiceMsg = @"Message sent to flickr";
}
else if ( [act isEqualToString:UIActivityTypePostToVimeo] )
{
ServiceMsg = @"Message sent to Vimeo";
}
else
{

}

}];
[self presentViewController:activityVC animated:YES completion:nil];

}
  • How is this different from the accepted answer? Also try to be as precise in answers as possible. – UditS Jul 07 '16 at 09:44
  • If we add accepted answer line then whats app activity issue will resolve but add the br/ character in other activities like facebook,imessage ,notes etc. – Rajni Arora Jul 12 '16 at 05:16
  • 1
    This is the better answer, because it considers the different sharing end points. – AlexVogel Mar 17 '17 at 13:09
0

hi its me again i was looking for the right answer

and i found that you can do it by using a Custom Share Message to Different Providers .

and you can find an example from this Code check for MyActivityItemProvider class .

https://github.com/apascual/flip-your-phone

i hove a problem posting the code here so i think the link above will help

  • Link only answers are discouraged as links can go stale. Could you put the relevant part in the body of your answer here? – beresfordt Jun 29 '15 at 22:33
0

Thanks to MuslimDev2015 I have been able to develop a solution: https://github.com/lorenzoPrimi/NewlineActivityItemProvider

Try it and let me know.

lorenzop
  • 580
  • 1
  • 6
  • 24
  • Note that [link-only answers](http://meta.stackoverflow.com/tags/link-only-answers/info) are discouraged, SO answers should be the end-point of a search for a solution (vs. yet another stopover of references, which tend to get stale over time). Please consider adding a stand-alone synopsis here, keeping the link as a reference. – kleopatra Nov 25 '15 at 11:27
0

You can send the text as multiple items each item is just one line.

let lines = text.components(separatedBy: "\n")
let activityViewController = UIActivityViewController(activityItems: lines, applicationActivities: nil)
Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
mohamede1945
  • 7,092
  • 6
  • 47
  • 61