10

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

ChikabuZ
  • 10,031
  • 5
  • 63
  • 86

4 Answers4

7

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

Dharmesh Siddhpura
  • 1,610
  • 12
  • 22
  • In my case it looks like: if activityType == UIActivityTypeMessage { return "\n" + url.absoluteString! } – ChikabuZ Sep 16 '15 at 16:35
  • @Dharmesh To answer your query, there is a 24 hour grace period after the end of the bounty. If the bounty hasn't been manually awarded at the end of the grace period, it is awarded to the accepted answer, or otherwise the highest voted (ties broken by earliest time posted) – Bohemian Sep 24 '15 at 15:45
3

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

KudoCC
  • 6,912
  • 1
  • 24
  • 53
  • Yes it will work, but it won't work for example in twitter, I'm searching universal solution. – ChikabuZ Sep 16 '15 at 08:28
  • There is no universal solution. If you declare a NSURL object then there will be always a space as it is iOS framework given. If you place the URL as part of a string the url is text and thus not recognized. Only exception to this if the receiving app is parsing the text directly or indirect. Direct should be clear. Indirect would mean an UITextField or UIWebView with link detection activated. – Helge Becker Sep 16 '15 at 08:45
  • Thank you, I'm waiting, maybe there is even better answer. – ChikabuZ Sep 16 '15 at 10:20
2

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

Ducky
  • 2,754
  • 16
  • 25
0

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)
Sankalap Yaduraj Singh
  • 1,167
  • 2
  • 14
  • 32