Customize the AirDrop alert description in IOS
Asked Answered
V

4

17

I have the following code to send a URL through AirDrop:

NSString* selfUrlScheme = [[[[[[NSBundle mainBundle]
                               infoDictionary]
                              valueForKey:@"CFBundleURLTypes"]
                             objectAtIndex:0]
                            valueForKey:@"CFBundleURLSchemes"]
                           objectAtIndex:0];

NSURL* schemeURL = [NSURL URLWithString:
                    [NSString stringWithFormat:
                     @"addList:%@,%@",
                     self.list.uniqueID,
                     selfUrlScheme]];

NSArray *objectsToShare = @[schemeURL];
controller = [[UIActivityViewController alloc]
              initWithActivityItems:objectsToShare
              applicationActivities:nil];

// Exclude all activities except AirDrop
   NSArray *excludedActivities = @[UIActivityTypePostToTwitter,
                                   UIActivityTypePostToWeibo,
                                   UIActivityTypeAssignToContact,
                                   UIActivityTypeSaveToCameraRoll,
                                   UIActivityTypeAddToReadingList,
                                   UIActivityTypePostToFlickr,
                                   UIActivityTypePostToVimeo,
                                   UIActivityTypePostToTencentWeibo];
controller.excludedActivityTypes = excludedActivities;
[self presentViewController:controller animated:YES completion:nil];

The recipient then gets the following message:

enter image description here

Is it possible to change the text of the URL found after 'X would like to share' to something more user friendly like 'X would like to share a list with you'? Thanks in advance!

EDIT

I now have this but it still produced the same result above:

AirDropCustomURL *container = [[AirDropCustomURL alloc] initWithUrl:schemeURL];
NSString *message = @"a list";
controller = [[UIActivityViewController alloc] initWithActivityItems:@[message, container] applicationActivities:nil];

@interface AirDropCustomURL : NSObject <UIActivityItemSource>

@property (strong, nonatomic) NSURL *url;
@property (strong, nonatomic) UIImage *productImage;
- (id)initWithUrl:(NSURL *)url;

@implementation AirDropCustomURL

- (id)initWithUrl:(NSURL *)url {
    if (self = [super init]) {
        _url = url;
    }
    return self;
}

#pragma mark - UIActivityItemSource

- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController {
    //Because the URL is already set it can be the placeholder. The API will use this to determine that an object of class type NSURL will be sent.
    return self.url;
}

- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType {
    //Return the URL being used. This URL has a custom scheme (see ReadMe.txt and Info.plist for more information about registering a custom URL scheme).
    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
        return nil;
    } else {
        if ([activityType isEqualToString:UIActivityTypeAirDrop]) {
            return self.url;
        }
    }
    return  nil;
}
Venter answered 26/11, 2013 at 16:51 Comment(1)
I guess you're out of luck for this one... see my edited answer.Subtorrid
S
7

You'll have to implement a class conforming to the UIActivityItemSource protocol. There is a very nice example here: https://developer.apple.com/LIBRARY/IOS/samplecode/sc2273/Introduction/Intro.html. Take a look at the APLCustomURLContainer in particular. After you implement your URL container class you can add it to the activity items along with a string (which will be your custom message)

MyURLContainer *container = [[MyURLContainer alloc] initWithURL:yourURL];
NSString *message = @"Your message";
UIActivityViewController activityController = [[UIActivityViewController alloc] initWithActivityItems:@[message, container] applicationActivities:nil];

EDIT:

I didn't have two phones to try that out at first so I tested only for Facebook and Twitter where it's working correctly, but for AirDrop I can confirm now (after some testing) that it is always using the relativeString of NSURL and even if you override that method of NSURL the sharing won't work so for AirDrop (the other activities such as FB are OK) it is not possible to change that message with the current SDK.

Subtorrid answered 28/11, 2013 at 17:59 Comment(2)
Where did you find that? Because they don't do that in the sample code you linked.Toilsome
The sample code that I just posted is from developer.apple.com and I do exactly that - MyURLContainer should be a subclass of NSObject, conforming to the UIActivityItemSource protocol just like the APLCustomURLContainer from the sample. You can also check the documentation for UIActivityViewController, which is explaining the activityItems parameter for the init method - developer.apple.com/library/IOS/documentation/UIKit/Reference/…:.Subtorrid
N
2

You need to follow the tutorial HERE. After the heading "A Quick Look at UIActivityViewController", i think thats exactly what you are looking for.

UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];
[self presentViewController:controller animated:YES completion:nil];
Notogaea answered 26/1, 2014 at 12:55 Comment(0)
T
1

Seems to me like it is using the description of the URL. I would try to subclass NSURL and override the description method to return something like @"a list". I didn't try it, but seems like it could work...

EDIT: I found this question that looks to be the same as yours and it has an accepted answer: Airdrop: making a custom URL scheme be less ugly for recipient

Toilsome answered 28/11, 2013 at 18:2 Comment(2)
I tried to override the description in a subclassed NSURL class, but no luck.Finalize
Ok, then I guess I better remove that part of the answer since it's clearly wrong. Thanks.Toilsome
H
0

This question has been posted a looong time ago. I tried @Ivan's answer as well as other answers in the post, however, they just do not work!

What I have originally is the image on the left. I am sharing custom data via AirDrop and looked at the title that is generated by UIActivityController by default "data-3A4F9D...", orz. What I want to achieve is a more descriptive title so that it is more readable for the recipient (image on the right). It seems that it is extremely similar to your question (even though I am sharing NSData while you are sharing NSURL.

Here is what I do, which works pretty well: implement the following UIActivityItemSource protocol within my UIActivityItemProvider.

- (NSString *)activityViewController:(UIActivityViewController *)activityViewController
          subjectForActivityType:(NSString *)activityType
{
        // This returning string is added to the email title
        return NSLocalizedString(@"CustomTitle", nil);
}

If you are still having this problem, feel free to give this a try and let me know how it works for you.

Hunchbacked answered 3/2, 2015 at 18:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.