UIActivityViewController 没有将换行符传递给某些活动
UIActivityViewController not passing new line characters to some activities
我正在使用以下代码设置 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:^{
// ...
}];
问题是,当我将消息或 post 复制到 facebook、twitter、电子邮件、gmail 应用程序或默认消息应用程序时,[self textMessageToShare]
中的换行符会保留。但是,如果我分享到 WhatsApp 或 Viber 等其他活动 - 所有新行字符都将被删除,并且整个消息将作为一行发送。
然而,如果我通过 iOS 默认的 Notes 应用程序共享文本,则在共享到这些应用程序时会保留换行符。 Notes 应用程序将如何存储换行符?我使用 \n
作为换行符。
对于我这辈子连理由都找不到。有人可以帮忙吗?
我能够通过将换行符转换为 "<br/>"
:
来使其工作
_myDataString= self.textview.text;
_myDataString= [_myDataString stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
嗨,又是我,我正在寻找正确的答案
而且我发现您可以通过向不同提供商使用自定义共享消息来做到这一点。
你可以从这个代码检查中找到一个示例,用于 MyActivityItemProvider class。
https://github.com/apascual/flip-your-phone
我在此处发布代码时遇到问题,所以我认为上面的 link 会有所帮助
感谢 MuslimDev2015,我已经能够开发出一个解决方案:
https://github.com/lorenzoPrimi/NewlineActivityItemProvider
试试看然后告诉我。
请使用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 @"";
}
在视图控制器中 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];
}
您可以将文本作为多个项目发送,每个项目只有一行。
let lines = text.components(separatedBy: "\n")
let activityViewController = UIActivityViewController(activityItems: lines, applicationActivities: nil)
我正在使用以下代码设置 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:^{
// ...
}];
问题是,当我将消息或 post 复制到 facebook、twitter、电子邮件、gmail 应用程序或默认消息应用程序时,[self textMessageToShare]
中的换行符会保留。但是,如果我分享到 WhatsApp 或 Viber 等其他活动 - 所有新行字符都将被删除,并且整个消息将作为一行发送。
然而,如果我通过 iOS 默认的 Notes 应用程序共享文本,则在共享到这些应用程序时会保留换行符。 Notes 应用程序将如何存储换行符?我使用 \n
作为换行符。
对于我这辈子连理由都找不到。有人可以帮忙吗?
我能够通过将换行符转换为 "<br/>"
:
_myDataString= self.textview.text;
_myDataString= [_myDataString stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
嗨,又是我,我正在寻找正确的答案
而且我发现您可以通过向不同提供商使用自定义共享消息来做到这一点。
你可以从这个代码检查中找到一个示例,用于 MyActivityItemProvider class。
https://github.com/apascual/flip-your-phone
我在此处发布代码时遇到问题,所以我认为上面的 link 会有所帮助
感谢 MuslimDev2015,我已经能够开发出一个解决方案: https://github.com/lorenzoPrimi/NewlineActivityItemProvider
试试看然后告诉我。
请使用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 @"";
}
在视图控制器中 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];
}
您可以将文本作为多个项目发送,每个项目只有一行。
let lines = text.components(separatedBy: "\n")
let activityViewController = UIActivityViewController(activityItems: lines, applicationActivities: nil)