gpt4 book ai didi

ios - 通过 UIActivityViewController 发送自定义数据

转载 作者:行者123 更新时间:2023-11-28 19:38:53 25 4
gpt4 key购买 nike

我正在尝试使用 UIActivityViewController 通过 AirDrop 将数据 (NSData) 从我在一台 iOS 设备上的应用程序发送到另一台设备。我在我的应用程序列表中创建了一个新的 CSM(自定义数据类型)。 public.filename-extension = ppm。那么如何将 ppm 扩展名添加到我要发送的 NSDate 对象?我是否正确地认为,当您呈现 UIActivityViewController 时,如果我发送的对象没有我的应用程序公共(public)扩展名 (ppm),我的应用程序图标将不会显示在 UIActivityViewController 窗口中??...是的,我是真的很困惑!这是我用来呈现 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这涵盖了定义您自己的文件类型并与您在其他设备上的应用程序共享的情况。如果您想共享原始数据,关键部分是您必须创建 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)

}

关于ios - 通过 UIActivityViewController 发送自定义数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36165222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com