仅通过 AirDrop 分享

Fel*_*mit 3 ios uiactivityviewcontroller airdrop uiactivitytypeairdrop

我想拿出带有 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];
Run Code Online (Sandbox Code Playgroud)

Jon*_*nny 6

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
    }   
}
Run Code Online (Sandbox Code Playgroud)