Set iOS sharing URL

It’s very clear to me how to share the link with iOS sharing activity ... But I would like to set up url tracking for different types of stocks, always using the same standard ... Examples of the following ...

Twitter:

http://www.example.com?utm_source=TWITTER&utm_medium=social&utm_campaign=socialbuttons&utm_content=app_android

Facebook:

http://www.example.com?utm_source=FACEBOOK&utm_medium=social&utm_campaign=socialbuttons&utm_content=app_android

Etc ...

Is it possible? How can i do this?

+5
source share
2 answers

I decided in Objective-C to add a custom UIActivityItemProvider :

 #import "CustomUiActivityItemProvider.h" @implementation CustomUiActivityItemProvider - (id)initWithText:(NSString *)text{ if ((self = [super initWithPlaceholderItem:text])) { self.text = text ?: @""; self.url = @""; } return self; } - (id)item { NSString *activityType = self.activityType; if ([self.placeholderItem isKindOfClass:[NSString class]]) { if ([self.activityType isEqualToString:UIActivityTypePostToFacebook]) { self.url = [[NSString alloc] initWithFormat:@"%@&utm_source=facebook", self.text]; } else if ([activityType isEqualToString:UIActivityTypePostToTwitter]) { self.url = [[NSString alloc] initWithFormat:@"%@&utm_source=twitter", self.text]; } else if ([activityType isEqualToString:UIActivityTypeMessage]) { self.url = [[NSString alloc] initWithFormat:@"%@&utm_source=message", self.text]; } else if([activityType isEqualToString:UIActivityTypeMail]){ self.url = [[NSString alloc] initWithFormat:@"%@&utm_source=email", self.text]; } else if ([activityType isEqualToString:UIActivityTypePostToWeibo]){ self.url = [[NSString alloc] initWithFormat:@"%@&utm_source=weibo", self.text]; }else{ self.url = [[NSString alloc] initWithFormat:@"%@&utm_source=other", self.text]; } return self.url; } return self.placeholderItem; } @end 

And using it like this:

 CustomUiActivityItemProvider *customProvider = [[CustomUiActivityItemProvider alloc] initWithText:urlString]; UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:@[customProvider] applicationActivities:nil]; activityVC.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll]; activityVC.completionHandler = ^(NSString *activityType, BOOL completed) { // CODE.... } [self presentViewController:activityVC animated:YES completion:nil]; 
+4
source
  • For this, we will use UIActivityItemSource , which act as a data provider for our UIActivityViewController . The documentation reads as follows.

When implementing this protocol, your object becomes a data provider, giving the view controller access to the elements.

  • To learn more about the documentation link.
  • Create an NSObject class as follows and add it after the UIViewController class

     class shareDifferentUrl: NSObject, UIActivityItemSource { @objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject { return "" } @objc func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? { if activityType == UIActivityTypePostToTwitter { return NSURL(string:"https://twitter.com/") } else if activityType == UIActivityTypePostToFacebook { return NSURL(string:"https://www.facebook.com/") } return nil } } 
  • Now it just adds the above class as the activityItems your UIActivityViewController

     @IBAction func share(sender: AnyObject) { let activityVC = UIActivityViewController(activityItems: [shareDifferentUrl()] as [AnyObject], applicationActivities: nil) self.navigationController?.presentViewController(activityVC, animated: true, completion: nil) } 
  • Now we can share different url on twitter and fb

+2
source

All Articles