Share via AirDrop only

2020-03-29 08:38发布

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

3条回答
SAY GOODBYE
2楼-- · 2020-03-29 08:46

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楼-- · 2020-03-29 08:54
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
    }   
}
查看更多
神经病院院长
4楼-- · 2020-03-29 09:08

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

查看更多
登录 后发表回答