UIActivityViewController 在共享到消息时删除冗余 space

UIActivityViewController remove redundant space when share to Messages

我的项目中有类似的代码,共享项之间变得多余 space。是否可以删除它?

let text = "Some text\n"
let link = NSURL(string: "http://whosebug.com/")!

let items = [text, link]
let activityVC = UIActivityViewController(activityItems: items, applicationActivities: nil)

self.presentViewController(activityVC, animated: true, completion: nil)

我想一定是哪里出了问题。 我尝试使用以下代码块重现您的案例

    let text = "Some text"
    let text2 = "ABC text"
    let text3 = "DEF text"
    let link = NSURL(string: "http://whosebug.com/")!

    let items = [text, text2, text3, link]
    let activityVC = UIActivityViewController(activityItems: items, applicationActivities: nil)

    self.presentViewController(activityVC, animated: true, completion: nil)
  1. 当select"Copy"然后粘贴出来,我得到: Some text ABC text DEF text http://whosebug.com/

  2. 当select通过消息分享时,我得到 Some text ABC text DEF text http://whosebug.com/

这是正确的(默认)行为。顺便说一句,改变这个默认行为是不可能的。

如果您仍想这样做,请按照@KudoCC 的建议更改您的文本格式或创建自定义格式UIActivity 是唯一的选择。

祝你好运

更新:这是一个复杂的解决方案。好像只有"Message"不行,所以我用两个activity对象来变通

您应该创建两个符合 UIActivityItemSource 协议的 classes。我对Swift不熟悉,所以用Objective-C来实现,相信你能看懂。

1 ActivityObject class

@interface ActivityObject : NSObject

@end

@implementation ActivityObject

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

- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType {
    if ([activityType isEqualToString:@"com.apple.UIKit.activity.Message"]) {
        return @"Some text\nhttp://whosebug.com/";
    } else {
        return @"Some text";
    }
}
@end

2 ActivityObjectURL class

@interface ActivityObjectURL : NSObject

@end

@implementation ActivityObjectURL

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

- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType {
    if ([activityType isEqualToString:@"com.apple.UIKit.activity.Message"]) {
        return @"";
    } else {
        return [NSURL URLWithString:@"http://whosebug.com"];
    }
}

@end

然后像这样使用它们。

ActivityObject *o = [[ActivityObject alloc] init];
ActivityObjectURL *ol = [[ActivityObjectURL alloc] init];
UIActivityViewController *avc = [[UIActivityViewController alloc] initWithActivityItems:@[o, ol] applicationActivities:nil];
[self presentViewController:avc animated:YES completion:nil];

@dsiddhpura 的解决方案将在邮件应用程序中添加一个奇怪的换行符。

按照下面提到的代码使用 NSString 而不是 NSURL:

NSString *text = @"Some Text";
NSString *URL = @"\nhttp://www.apple.com";
UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:@[text, URL] applicationActivities:nil];

[self presentViewController:controller animated:YES completion:nil];

--------------------Swift 代码(根据问题)------------

let text = "Some text"
let link = "\nhttp://whosebug.com/"

let items = [text, link]
let activityVC = UIActivityViewController(activityItems: items, applicationActivities: nil)

self.presentViewController(activityVC, animated: true, completion: nil)

您可以看到附件的屏幕截图。一旦你 post/send 消息它仍然会显示为 link.

如果 url 不是 activity 项数组中的第一个对象,

URL 默认添加一个 space。

希望这对您有所帮助。

试试吧。

let text = "Some text"
let link = NSURL(string: "\nhttp://whosebug.com/")!
let items = [text, link]
let activityVC = UIActivityViewController(activityItems: items, applicationActivities: nil)
self.presentViewController(activityVC, animated: true, completion: nil)