简体   繁体   中英

UIActivityViewController remove redundant space when share to Messages

I have similar code in my project, and I get redundant space between share items. Is it possible to remove it?

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

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

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

在此输入图像描述

Use NSString instead of NSURL as per below mentioned code:

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 Code(as per question)-------------

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

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

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

You can see the attached screenshots. Once you post/send message it will still appear as a link.

URL adds a space by default if the url is not the first object in the activity items array.

Hope this helps.

在此输入图像描述

Update: Here is a complex solution. It seems that only "Message" doesn't work, so I use two activity objects to work around.

You should create two classes which conform to UIActivityItemSource protocol. I'm not familiar with Swift so they are implemented with Objective-C, I believe you can understand.

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://stackoverflow.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://stackoverflow.com"];
    }
}

@end

Then use them like this.

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's solution will add a odd line break in Mail app.

在此输入图像描述 在此输入图像描述

I think there must be something wrong somewhere else. I tried to reproduce your case with following code block

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

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

    self.presentViewController(activityVC, animated: true, completion: nil)
  1. When select "Copy" then paste it out, I got: Some text ABC text DEF text http://stackoverflow.com/

  2. When select to share via Message, I got Some text ABC text DEF text http://stackoverflow.com/

which is correct (default) behavior. It's impossible to change this default behavior by the way.

If you still would like to do so, change your text format like @KudoCC suggested or create a custom UIActivity is the only choice.

Good luck

Try it.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM