Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIActivityViewController not passing new line characters to some activities

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?

like image 972
BufferStack Avatar asked May 15 '15 09:05

BufferStack


Video Answer


2 Answers

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

_myDataString=  self.textview.text;
_myDataString= [_myDataString  stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
like image 137
MuslimDev2015 Avatar answered Sep 20 '22 16:09

MuslimDev2015


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];

}
like image 36
Rajni Arora Avatar answered Sep 23 '22 16:09

Rajni Arora