如何在Swift 3中将我的应用程序的位置分享到WhatsApp

时间:2017-08-17 13:20:33

标签: ios swift ios10 whatsapp uiactivityviewcontroller

我正在尝试从我的iOS应用程序到WhatsApp共享一个位置,我希望它看起来像这样:

enter image description here

我正在做的是发送带有此代码的vCard:

 func vCardURL(from coordinate: CLLocationCoordinate2D, with name: String?) -> URL {
    let vCardFileURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("Shared Location.loc.vcf")

    let vCardString = [
        "BEGIN:VCARD",
        "VERSION:3.0",
        //"PRODID:-//Apple Inc.//iPhone OS 10.3.2//EN",
        "N:;My Location;;;",
        "FN:My Location",
        "item1.URL;type=pref:https://maps.apple.com/?ll=50.359890\\,12.934560&q=My%20Location&t=m",
        "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).")
    }

    print(vCardString)

    return vCardFileURL
} // end of function

//调用上面的方法

let vURL = LocationVCard.vCardURL(from: self.newLocation.coordinate, with: "Berlin")

                let activityViewController = UIActivityViewController(activityItems: [vURL], applicationActivities: nil)
                self.present(activityViewController, animated: true, completion: nil)

但我总是以这种风格而不是我想要的结果: enter image description here

1 个答案:

答案 0 :(得分:2)

无需将vCard字符串保存到文件,只需将其转换为Data并用带有正确类型标识符NSItemProvider的{​​{1}}包裹起来,就可以了:

public.vcard