Skip to content

Instantly share code, notes, and snippets.

@ElegyD
Forked from naturaln0va/LocationVCard.swift
Last active March 18, 2019 20:06
Show Gist options
  • Save ElegyD/510a17904917a7e7326254b824da1b2f to your computer and use it in GitHub Desktop.
Save ElegyD/510a17904917a7e7326254b824da1b2f 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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment