Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize URL for iOS sharing

Tags:

ios

It's pretty clear to me how to share a link with the iOS sharing activity... But I would like to customize the tracking in the url for different kind of shares, always using the same standard... Examples 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 it?

like image 589
napolux Avatar asked Aug 04 '16 13:08

napolux


People also ask

What is URL scheme in iOS?

Working with URL Schemes in iOS Apps. The URL scheme is an interesting feature provided by the iOS SDK that allows developers to launch system apps and third-party apps through URLs. For example, let’s say your app displays a phone number, and you want to make a call whenever a user taps that number.

How do I add a custom URL scheme to my App?

To support a custom URL scheme: Define the format for your app’s URLs. Register your scheme so that the system directs appropriate URLs to your app. Handle the URLs that your app receives. URLs must start with your custom scheme name. Add parameters for any options your app supports.

How do I handle URLs that my App receives?

Handle the URLs that your app receives. URLs must start with your custom scheme name. Add parameters for any options your app supports. For example, a photo library app might define a URL format that includes the name or index of a photo album to display.

What are Custom URLs and how do they work?

Custom URL schemes provide a way to reference resources inside your app. Users tapping a custom URL in an email, for example, launch your app in a specified context. Other apps can also trigger your app to launch with specific context data; for example, a photo library app might display a specified image.


1 Answers

I solved in Objective-C by adding 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];
like image 155
napolux Avatar answered Sep 23 '22 20:09

napolux