通过UIActivityViewController发送自定义数据 [英] Sending custom data via UIActivityViewController
问题描述
我正在尝试使用UIActivityViewController通过AirDrop从一个iOS设备上的应用程序向另一个应用程序发送数据(NSData).我在应用程序plist中创建了一个新的CSM(自定义数据类型). public.filename-extension = ppm. 那么如何将ppm扩展名添加到我要发送的NSDate对象中呢?我的想法是对的,当您提出一个UIActivityViewController时,如果我发送的对象没有我的应用程序公共扩展名(ppm),我的应用程序图标将不会显示在UIActivityViewController窗口中.真的很困惑! 这是我用来呈现UIActivityViewController的代码
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
推荐答案
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)
}
这篇关于通过UIActivityViewController发送自定义数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!