Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

enter image description here

like image 475
ChikabuZ Avatar asked Sep 13 '15 19:09

ChikabuZ


3 Answers

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.

enter image description here

like image 142
Dharmesh Siddhpura Avatar answered Nov 20 '22 03:11

Dharmesh Siddhpura


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.

enter image description here enter image description here

like image 45
KudoCC Avatar answered Nov 20 '22 02:11

KudoCC


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

like image 2
Ducky Avatar answered Nov 20 '22 03:11

Ducky