2

我想使用 AirDrop 作为我的应用程序中的唯一选项来显示共享表。

在 iOS 7 中,我可以简单地将所有共享类型添加到( , , ...)的excludedActivityTypes属性中UIActivityViewControllerUIActivityTypeMessageUIActivityTypeMail

但是现在在 iOS 8 中,通过应用程序扩展,用户可能会出现额外的共享提供程序。有没有办法显示 AirDrop ?

UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:@[[NSURL URLWithString:url]]
                                                                         applicationActivities:nil];

NSArray *excludedActivities = @[UIActivityTypePostToTwitter, UIActivityTypePostToFacebook,
                                UIActivityTypePostToWeibo,
                                UIActivityTypeMessage, UIActivityTypeMail,
                                UIActivityTypePrint, UIActivityTypeCopyToPasteboard,
                                UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll,
                                UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr,
                                UIActivityTypePostToVimeo, UIActivityTypePostToTencentWeibo];
4

3 回答 3

3
class AirDropOnlyActivityItemSource : NSObject, UIActivityItemSource {

    /// The item you want to send via AirDrop.
    let item: AnyObject

    init(item: AnyObject) {
        self.item = item
    }

    func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject {
        /// Use an empty URL as placeholder to let iOS Share Sheet show the AirDrop only.
        return NSURL(string: "")!
    }

    func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
        return item
    }   
}
于 2016-04-12T12:53:40.137 回答
1

Jonny 回答的Swift 4.2 版本:

class AirDropOnlyActivityItemSource: NSObject, UIActivityItemSource {
    ///The item you want to send via AirDrop.
    let item: Any

    init(item: Any) {
        self.item = item
    }

    func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
        //using NSURL here, since URL with an empty string would crash
        return NSURL(string: "")!
    }

    func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {
        return item
    }
}

用法:

let itemSource = AirDropOnlyActivityItemSource(item: "test")

let activityVc = UIActivityViewController(activityItems: [itemSource], applicationActivities: nil)
于 2019-01-14T16:33:21.023 回答
-1

目前这是不可能的。您应该向苹果提交增强请求,解释为什么这对用户/开发人员有用。

于 2015-02-23T20:24:50.213 回答