Share via AirDrop only

时间秒杀一切 提交于 2019-12-01 07:57:44

问题


I want to bring out the share sheet with the AirDrop as the only option in my application.

In iOS 7, I could simple add all share types to the excludedActivityTypes property of UIActivityViewController (UIActivityTypeMessage, UIActivityTypeMail, ...)

But now in iOS 8, with the app extensions, the users may have extra sharing providers that show up. Is there any way to show AirDrop only?

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

回答1:


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



回答2:


Swift 4.2 version of Jonny's answer:

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

Usage:

let itemSource = AirDropOnlyActivityItemSource(item: "test")

let activityVc = UIActivityViewController(activityItems: [itemSource], applicationActivities: nil)



回答3:


That's currently not possible. You should file an enhancement request with apple explaining why this would be useful to the user/developer.



来源:https://stackoverflow.com/questions/28607781/share-via-airdrop-only

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