Swift 5 - 使用 vCardURL 共享位置
Swift 5 - Sharing location using vCardURL
我正在尝试通过 UIActivityViewController 共享一个地址。
我就是这样得到 URL:
func vCardURL(from coordinate: CLLocationCoordinate2D, with name: String?) -> URL {
let vCardFileURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("vCard.loc.vcf")
let vCardString = [
"BEGIN:VCARD",
"VERSION:4.0",
"FN:\(name ?? "Shared Location")",
"item1.URL;type=pref:http://maps.apple.com/?ll=\(coordinate.latitude),\(coordinate.longitude)",
"item1.X-ABLabel:map url",
"END:VCARD"
].joined(separator: "\n")
do {
try vCardString.write(toFile: vCardFileURL.path, atomically: true, encoding: .utf8)
} catch let error {
print("Error, \(error.localizedDescription), saving vCard: \(vCardString) to file path: \(vCardFileURL.path).")
}
return vCardFileURL
}
func didTapShareButton() {
let coordinate = CLLocationCoordinate2D(latitude: 52.520007, longitude: 13.404954)
let url = self.vCardURL(from: coordinate, with: "Berlin")
let activityViewController = UIActivityViewController(activityItems: [url], applicationActivities: nil)
//Here it opens the UIActivityViewController
present(activityViewController, animated: true, completion: nil)
}
URL 输出:
file:///Users/edoardodecal/Library/Developer/CoreSimulator/Devices/E1A8A47C-ABF7-4048-ACB1-0AC91E7E0B5B/data/Containers/Data/Application/F79E6A32-B2E7-4137-A75B-AFDE3294A1C2/tmp/vCard.loc.vcf
这是结果:
我的期望:
有什么提示吗?谢谢
忘记所有这些东西。您唯一需要做的就是与坐标共享 Apple 地图 URL...就是这样!使用 UIKit 和 SwiftUI 在 iOS 14 上测试。
if let url = URL(string: "https://maps.apple.com?ll=\(latitude),\(longitude)") {
let activity = UIActivityViewController(activityItems: [url], applicationActivities: nil)
}
然后显示视图控制器...
我正在尝试通过 UIActivityViewController 共享一个地址。
我就是这样得到 URL:
func vCardURL(from coordinate: CLLocationCoordinate2D, with name: String?) -> URL {
let vCardFileURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("vCard.loc.vcf")
let vCardString = [
"BEGIN:VCARD",
"VERSION:4.0",
"FN:\(name ?? "Shared Location")",
"item1.URL;type=pref:http://maps.apple.com/?ll=\(coordinate.latitude),\(coordinate.longitude)",
"item1.X-ABLabel:map url",
"END:VCARD"
].joined(separator: "\n")
do {
try vCardString.write(toFile: vCardFileURL.path, atomically: true, encoding: .utf8)
} catch let error {
print("Error, \(error.localizedDescription), saving vCard: \(vCardString) to file path: \(vCardFileURL.path).")
}
return vCardFileURL
}
func didTapShareButton() {
let coordinate = CLLocationCoordinate2D(latitude: 52.520007, longitude: 13.404954)
let url = self.vCardURL(from: coordinate, with: "Berlin")
let activityViewController = UIActivityViewController(activityItems: [url], applicationActivities: nil)
//Here it opens the UIActivityViewController
present(activityViewController, animated: true, completion: nil)
}
URL 输出:
file:///Users/edoardodecal/Library/Developer/CoreSimulator/Devices/E1A8A47C-ABF7-4048-ACB1-0AC91E7E0B5B/data/Containers/Data/Application/F79E6A32-B2E7-4137-A75B-AFDE3294A1C2/tmp/vCard.loc.vcf
这是结果:
我的期望:
有什么提示吗?谢谢
忘记所有这些东西。您唯一需要做的就是与坐标共享 Apple 地图 URL...就是这样!使用 UIKit 和 SwiftUI 在 iOS 14 上测试。
if let url = URL(string: "https://maps.apple.com?ll=\(latitude),\(longitude)") {
let activity = UIActivityViewController(activityItems: [url], applicationActivities: nil)
}
然后显示视图控制器...