Sending user data through the UIActivityViewController

I am trying to send data (NSData) from my application to one iOS device to another through AirDrop using the UIActivityViewController. I created a new CSM (user data type) in my plist application. Public.filename-extension = ppm. So how do I add the ppm extension to the NSDate object I'm trying to send? Do I think that when you present the UIActivityViewController, the My Applications icon will not appear in the UIActivityViewController window if the Im-sending object has no extension for my applications (ppm)? .... yes, I'm really confused !! Here is the code I use to represent the 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 user data to be used in my application.

0
source share
1 answer

You should take a look at the AirDrop sample code , which covers the case of defining your own file type and sharing this application with another device. The key part, if you want to share the raw data, is that you need to instantiate UIActivityItemSourceand pass it to UIActivityViewController. Something like that:

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)

}
+3
source

All Articles