在IOS中自定义AirDrop警报说明

时间:2013-11-26 16:51:17

标签: ios iphone objective-c airdrop

我有以下代码通过URL发送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];

然后收件人会收到以下消息:

enter image description here

是否可以将'X想要分享'之后找到的URL的文字更改为更像用户友好的内容,例如'X想与您分享列表'?提前谢谢!

修改

我现在有了这个,但仍然产生了相同的结果:

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

4 个答案:

答案 0 :(得分:7)

您必须实现符合UIActivityItemSource协议的类。这里有一个很好的例子:https://developer.apple.com/LIBRARY/IOS/samplecode/sc2273/Introduction/Intro.html。请特别注意APLCustomURLContainer。实现URL容器类后,您可以将其与字符串一起添加到活动项目中(这将是您的自定义消息)

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

修改

我一开始没有两部手机可以尝试,所以我只测试了Facebook和Twitter的工作正常,但对于AirDrop,我现在可以确认(经过一些测试)它总是使用NSURL的relativeString即使你覆盖NSURL的那个方法,共享也不会有效,所以对于AirDrop(其他活动,比如FB都没问题),无法用当前的SDK更改该消息。

答案 1 :(得分:2)

您需要按照教程HERE进行操作。标题为“快速浏览UIActivityViewController”后,我认为这正是您所寻找的。

UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];
[self presentViewController:controller animated:YES completion:nil];

答案 2 :(得分:1)

在我看来它正在使用description的{​​{1}}。我会尝试继承URL并覆盖NSURL方法以返回description之类的内容。我没试过,但似乎它可以工作......

编辑:我发现此问题与您的问题相同,并且有一个接受的答案:Airdrop: making a custom URL scheme be less ugly for recipient

答案 3 :(得分:0)

这个问题已经在很久以前发布了。我尝试了@Ivan的答案以及帖子中的其他答案,但是,它们只是不起作用!

我原来的是左边的图像。我通过AirDrop分享自定义数据,并查看UIActivityController默认生成的标题“ data-3A4F9D ... ”,orz。我想要实现的是一个更具描述性的标题,以便它对接收者更具可读性(右图)。它似乎与您的问题非常相似(即使我在您分享NSData时分享NSURL

以下是我的工作,效果非常好:在UIActivityItemSource中实施以下UIActivityItemProvider协议。

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

如果您仍然遇到此问题,请随意尝试一下,让我知道它对您有用。