ios - 如何在动作扩展中将 NSSecureCoding 转换为 MKMapItem
问题描述
我正在尝试为我的应用程序制作一个操作扩展,用户可以在其中添加他当前的位置以及其他一些数据。在与 Apple Maps App 共享位置后,我调试了扩展程序,发现 Maps 发送了四个具有以下内容的提供商:
- 所选位置的电子名片
- 选定位置的 Apple 地图 URL
- 纯文本,它是所选位置的名称
- 一个
MKMapItem
以上所有内容均为NSSecureCoding
. 强制转换为 vCard 的数据Data
并使用该数据进行初始化,纯文本和 url 从 NSSecureCoding 成功,但我还没有找到从收到的数据创建对象的方法。String
String
MKMapItem
这是我尝试过的:
provider.loadItem(forTypeIdentifier: "com.apple.mapkit.map-item", options: nil) { (content, _) in
let item = content as! MKMapItem
}
但它失败了。我可能必须Data
先将它转换为,但我找不到任何初始化MKMapItem
器Data
解决方案
使用 NSKeyedUnarchiver
itemProvider.loadItem(forTypeIdentifier: "com.apple.mapkit.map-item", options: nil) { (item, error) in
guard let data = item as? Data else { return }
do {
guard let mapItem = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? MKMapItem else { return }
print(mapItem)
} catch {
print("Error unarchiving mapItems, \(error)")
}
推荐阅读
- pdf - 如何在边缘编辑pdf中的内容
- rust - 无法找到将字符转换为 u8 的方法
- javascript - php可以在响应ajax请求之前等待很长时间吗
- c# - 寻求 C# ASP.NET Core Web App 项目的帮助
- docker - 在 docker 上等待用户输入 golang cli app
- css - 针对移动设备的 GridView CSS 更改
- javascript - 如何将图像的响应高度或宽度添加到容器中?
- azure - Azure CDN 规则引擎将 www 添加到裸域
- javascript - 尝试使用 React-Router-Dom 时出现问题
- javascript - 将 div 移动到另一个 div