问题
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