我尝试使用UIActivityViewController通过AirDrop从一台iOS设备上的应用程序向另一台设备发送数据(NSData)。我在我的应用程序plist中创建了一个新的CSM(自定义数据类型)。 public.filename-extension = ppm。 那么如何将ppm扩展添加到我正在尝试发送的NSDate对象?我是否正确地认为,当您呈现UIActivityViewController时,如果我发送的对象没有我的应用程序公共扩展名(ppm),我的应用程序图标将不会显示在UIActivityViewController窗口中?? ....是的,我是真迷茫!! 下面是我用来呈现UIActivityViewController的代码
public override async Task PreAuthenticate(IHttpClient client, IHttpRequestMessage request, ICredentials credentials)
{
...
// get private "handler" field of type DefaultHttpClient from client by reflection here
// get private "proxy" field of type IWebProxy from "handler" by reflection here
// set private "proxy" field if it's null by reflection here
...
}
基本上,我要做的就是发送要在我的应用中使用的自定义数据
答案 0 :(得分:3)
您应该查看AirDrop sample code,其中包含定义您自己的文件类型并在其他设备上与您的应用共享的情况。如果要共享原始数据,关键部分是您必须创建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)
}