Customize the AirDrop alert description in IOS

与世无争的帅哥 提交于 2019-12-03 08:00:20

问题


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:

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;
}

回答1:


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.




回答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];



回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/20223107/customize-the-airdrop-alert-description-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!