我正在尝试构建一个应用程序,我可以在其中共享地址并在任何导航应用程序(Google map 、Apple map 、waze 等)中打开它。 下面是我目前拥有的代码(在浏览了包括数十个 stackoverflow 问题在内的谷歌搜索结果页面之后)
@IBAction func navigeer(_ sender: Any) {
var items = [AnyObject]()
let latitude: Double = 52.033809
let longitude: Double = 6.882286
let locationTitle = "Navigate to this address"
let URLString = "https://maps.apple.com?ll=\(latitude),\(longitude)"
guard let cachesPathString = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first else {
print("Error: couldn't find the caches directory.")
return
}
if let url = NSURL(string: URLString) {
items.append(url)
}
let vCardString = [
"BEGIN:VCARD",
"VERSION:3.0",
"N:;Shared Location;;;",
"FN:Shared Location",
"item1.URL;type=pref:http://maps.apple.com/?ll=\(latitude),\(longitude)",
"item1.X-ABLabel:map url",
"END:VCARD"
].joined(separator: "\n")
let vCardFilePath = (cachesPathString as NSString).appendingPathComponent("vCard.loc.vcf")
let nsVCardData = NSURL(fileURLWithPath: vCardFilePath)
let shareItems:Array = [nsVCardData]
let activityController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
present(activityController, animated:true, completion: nil)
}
当我在我的模拟器上运行该应用程序时,我得到以下信息:
点击分享按钮后
为什么我没有收到 Apple map 或 Google map 等应用建议?我也不明白为什么它建议我将其复制到联系人中..
提前致谢。
最佳答案
使用 UIActivityViewController、Swift 5 共享位置
func activityItems(latitude: Double, longitude: Double) -> [AnyObject]? {
var items = [AnyObject]()
let URLString = "https://maps.apple.com?ll=\(latitude),\(longitude)"
if let url = NSURL(string: URLString) {
items.append(url)
}
let locationVCardString = [
"BEGIN:VCARD",
"VERSION:3.0",
"PRODID:-//Joseph Duffy//Blog Post Example//EN",
"N:;Shared Location;;;",
"FN:Shared Location",
"item1.URL;type=pref:\(URLString)",
"item1.X-ABLabel:map url",
"END:VCARD"
].joined(separator: "\n")
guard let vCardData : NSSecureCoding = locationVCardString.data(using: .utf8) as NSSecureCoding? else {
return nil
}
let vCardActivity = NSItemProvider(item: vCardData, typeIdentifier: kUTTypeVCard as String)
items.append(vCardActivity)
return items
}
如何使用?
if let shareObject = self.activityItems(latitude: 52.033809, longitude: 6.882286) {
//open UIActivityViewController
}
引用链接:https://new.josephduffy.co.uk/posts/ios-share-sheets-the-proper-way-locations
关于ios - Swift 4 - 通过 UIActivityViewController 共享位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47335111/