我使用以下代码设置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:^{
// ...
}];
问题是,当我将消息或帖子复制到Facebook或Twitter或电子邮件或gmail应用程序或默认消息应用程序时,将保留[self textMessageToShare]
中的新行字符。但是,如果我与WhatsApp或Viber等其他活动共享 - 所有新行字符都将被删除,整条消息将作为一行发送。
然而,如果我通过iOS默认Notes应用程序共享文本,则在与这些应用程序共享时会保留新的行字符。 Notes应用程序如何存储新行字符?我使用\n
作为换行符。
我的生活甚至无法找到原因。有人可以帮忙吗?
答案 0 :(得分:5)
我能够通过将换行符转换为"<br/>"
来实现它的工作:
_myDataString= self.textview.text;
_myDataString= [_myDataString stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
答案 1 :(得分:2)
请使用uiactivityviewcontroller检查whats app share中的新行问题。
#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 @"";
}
在View Controller Class中的任何操作按钮上弹出共享视图 在任何按钮操作上应用此操作
-(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];
}
答案 2 :(得分:0)
再次,我在寻找正确的答案
我发现您可以通过向不同的提供商使用自定义共享消息来实现此目的。
您可以在MyActivityItemProvider类的此代码检查中找到一个示例。
https://github.com/apascual/flip-your-phone
我在这里发布代码时遇到问题所以我认为上面的链接会有所帮助
答案 3 :(得分:0)
感谢MuslimDev2015,我已经能够开发出一个解决方案: https://github.com/lorenzoPrimi/NewlineActivityItemProvider
试试并告诉我。
答案 4 :(得分:0)
您可以将文本作为多个项目发送,每个项目只有一行。
let lines = text.components(separatedBy: "\n")
let activityViewController = UIActivityViewController(activityItems: lines, applicationActivities: nil)