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)
When select "Copy" then paste it out, I got:
Some text
ABC text
DEF text
http://stackoverflow.com/
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With