Share via AirDrop only

試著忘記壹切 提交于 2019-12-01 10:50:02
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
    }   
}

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)

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

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