Sending custom data via UIActivityViewController

前端 未结 1 1907
长发绾君心
长发绾君心 2021-01-16 09:35

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 ap

1条回答
  •  旧巷少年郎
    2021-01-16 10:18

    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)
    
    }
    

    0 讨论(0)
提交回复
热议问题