Swift 4 - 通过UIActivityViewController分享位置

5
我正在尝试构建一个应用程序,可以共享地址并在任何导航应用程序中打开(如Google地图,Apple地图,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)
}

当我在模拟器上运行应用程序时,我会得到以下结果:
点击分享按钮后
After clicking the share button 为什么我没有像苹果地图或谷歌地图这样的应用程序建议?我也不明白为什么它建议我将其复制到联系人中。
提前致谢。

第三方应用程序不会出现在模拟器中(不包括您安装的应用程序)。请尝试在真实设备上运行您的代码。 - Tamás Sengel
我在自己的 iPhone 上运行它,但仍然没有建议像 Apple 地图、谷歌地图或 Waze 这样的导航应用程序。有什么建议吗?@the4kman - Mathias Schrooten
你找到解决方案了吗? - Sergey Didanov
你从未将任何数据(vcard字符串)写入URL。 - rmaddy
2个回答

3

使用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 应用中使用 share sheets 的正确方式,特别是在请求位置权限时的注意事项。该文章详细阐述了 share sheets 如何使用和配置,并且提供了一些实用的代码示例。

0

试试这个...

// if the device has google maps installed in it

if (UIApplication.shared.canOpenURL(NSURL(string:"comgooglemaps://")! as URL)) {

            UIApplication.shared.openURL(NSURL(string:
                "comgooglemaps://?saddr=&daddr=\(myLatitude),\(myLongitude)&directionsmode=driving")! as URL)

        }
// if google maps is not installed, try apple map

else if (UIApplication.shared.canOpenURL(NSURL(string:"http://maps.apple.com/maps")! as URL)) {
            // apple map
            let url = "http://maps.apple.com/maps?saddr=\(from.latitude),\(from.longitude)&daddr=\(to.latitude),\(to.longitude)"
            UIApplication.shared.openURL(URL(string:url)!)
        }

// if apple map is also not there, it will show an appStore link to download apple map application.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接