Sending custom data via UIActivityViewController

风流意气都作罢 提交于 2019-12-13 06:43:51

问题


Im trying to send data (NSData) from my app on one iOS Device to another via AirDrop using the UIActivityViewController. I have created a new CSM (custom data type) in my apps plist. The public.filename-extension = ppm. So how do I add the ppm extension to the NSDate object I'm trying to send ?? Am I right in thinking that when a you present a UIActivityViewController, my apps Icon will not be displayed in the UIActivityViewController window if the object Im sending does not have my apps public extension (ppm) ??.... yea, I'm really confused !! Heres the code I'm using to present UIActivityViewController

    @IBAction func shareButton(sender: AnyObject) {

    // myData is the object I want to send to be used in my app on another device

    let vc = UIActivityViewController(activityItems: [myData],applicationActivities: [])
    presentViewController(vc, animated: true, completion: nil)

    }

Basically, all I'm trying to do is send custom data to be used in my app


回答1:


You should take a look at the AirDrop sample code that covers the case of defining your own file type and sharing that with your app on the other device. The key part if you want to share raw data is that you have to create an instance of UIActivityItemSource and pass that to UIActivityViewController. Something like this:

class DataActivityItemSource: NSObject, UIActivityItemSource {
    let myData: NSData
    let typeIdentifier: String
    let subject: String
    let previewImage: UIImage

    init(myData: NSData, typeIdentifier: String, subject: String, previewImage: UIImage) {
        self.myData = myData
        self.typeIdentifier = typeIdentifier
        self.subject = subject
        self.previewImage = previewImage
    }

    // called to determine data type. only the class of the return type is consulted. it should match what -itemForActivityType: returns later
    @objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject {
        return myData
    }

    // called to fetch data after an activity is selected. you can return nil.
    @objc func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
        return myData
    }

    // if activity supports a Subject field. iOS 7.0
    @objc func activityViewController(activityViewController: UIActivityViewController, subjectForActivityType activityType: String?) -> String {
        return subject
    }

    // UTI for item if it is an NSData. iOS 7.0. will be called with nil activity and then selected activity
    @objc func activityViewController(activityViewController: UIActivityViewController, dataTypeIdentifierForActivityType activityType: String?) -> String {
        return typeIdentifier
    }

    // if activity supports preview image. iOS 7.0
    @objc func activityViewController(activityViewController: UIActivityViewController, thumbnailImageForActivityType activityType: String?, suggestedSize size: CGSize) -> UIImage? {
        // look at suggestedSize and resize image (see AirDrop sample code for how to do this)
        return previewImage
    }
}


@IBAction func shareButton(sender: AnyObject) {

    // myData is the object I want to send to be used in my app on another device
    let itemSource = DataActivityItemSource(myData, "com.foo.ppm.typeIdentifier", "My Amazing Journey", aPreviewImage)
    let vc = UIActivityViewController(activityItems: [itemSource],applicationActivities: [])
    presentViewController(vc, animated: true, completion: nil)

}


来源:https://stackoverflow.com/questions/36165222/sending-custom-data-via-uiactivityviewcontroller

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