Skip to content

Instantly share code, notes, and snippets.

@souzainf3
Forked from ElegyD/LocationVCard.swift
Created May 28, 2018 02:18
Show Gist options
  • Select an option

  • Save souzainf3/3c7b0b072c28da7dbb905ee4b875dffb to your computer and use it in GitHub Desktop.

Select an option

Save souzainf3/3c7b0b072c28da7dbb905ee4b875dffb to your computer and use it in GitHub Desktop.
Create a location vCard for use in UIActivityViewController like in Apple's Maps app.
import CoreLocation
static 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
}
@souzainf3
Copy link
Author

souzainf3 commented May 28, 2018

Use

let coordinate = CLLocationCoordinate2D(latitude: 52.520007, longitude: 13.404954)
let vCardURL = LocationVCard.vCardUrl(from: coordinate, with: "Berlin")
let activityViewController = UIActivityViewController(activityItems: [vCardURL], applicationActivities: nil)
present(activityViewController, animated: true, completion: nil)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment