通过 UIActivityViewController 发送自定义数据
Sending custom data via UIActivityViewController
我正在尝试使用 UIActivityViewController 通过 AirDrop 将数据 (NSData) 从我在一台 iOS 设备上的应用程序发送到另一台设备。我在我的应用程序列表中创建了一个新的 CSM(自定义数据类型)。 public.filename-扩展 = ppm。
那么如何将 ppm 扩展名添加到我要发送的 NSDate 对象?我认为当您呈现 UIActivityViewController 时,如果我发送的对象没有我的应用程序 public 扩展名 (ppm),我的应用程序图标将不会显示在 UIActivityViewController window 中,我的想法是否正确? ......是的,我真的很困惑!
这是我用来呈现 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)
}
基本上,我要做的就是发送要在我的应用中使用的自定义数据
您应该看一下 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
并将其传递给 UIActivityViewController
。像这样:
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 通过 AirDrop 将数据 (NSData) 从我在一台 iOS 设备上的应用程序发送到另一台设备。我在我的应用程序列表中创建了一个新的 CSM(自定义数据类型)。 public.filename-扩展 = ppm。 那么如何将 ppm 扩展名添加到我要发送的 NSDate 对象?我认为当您呈现 UIActivityViewController 时,如果我发送的对象没有我的应用程序 public 扩展名 (ppm),我的应用程序图标将不会显示在 UIActivityViewController window 中,我的想法是否正确? ......是的,我真的很困惑! 这是我用来呈现 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)
}
基本上,我要做的就是发送要在我的应用中使用的自定义数据
您应该看一下 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
并将其传递给 UIActivityViewController
。像这样:
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)
}