Swift 4 - sharing location through UIActivityViewController

Viewed 3438

I'm trying to build an application where I can share an address and open that in any navigation app (Google maps, Apple maps, waze,...). Below is the code that I currently have (after going through pages of google search results including dozens of stackoverflow questions)

@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)
}

When I run the application on my simulator I get the following:

After clicking the share button
After clicking the share button

Why don't I get app suggestions like Apple maps or Google maps? I also don't get why it suggests me to copy it to contacts..

Thanks in advance.

2 Answers

To share location using 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
    }

How to use?

if let shareObject = self.activityItems(latitude: 52.033809, longitude: 6.882286) {
       //open UIActivityViewController 
}

Reference Link : https://new.josephduffy.co.uk/posts/ios-share-sheets-the-proper-way-locations

Try this..

// 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.
Related