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

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

是否可以将在“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;
}

最佳答案

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

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(其他 Activity ,例如FB都可以),无法使用当前的SDK更改该消息。

关于ios - 在IOS中自定义AirDrop警报说明,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20223107/

10-14 22:02