Skip to content

Instantly share code, notes, and snippets.

@AnanthChristy
Forked from naturaln0va/LocationVCard.swift
Created August 1, 2016 04:46
Show Gist options
  • Select an option

  • Save AnanthChristy/9a13a6653ba16f8347e45f4855a71cf5 to your computer and use it in GitHub Desktop.

Select an option

Save AnanthChristy/9a13a6653ba16f8347e45f4855a71cf5 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
func locationVCardURLFromCoordinate(coordinate: CLLocationCoordinate2D) -> NSURL?
{
guard let cachesPathString = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true).first else {
print("Error: couldn't find the caches directory.")
return nil
}
guard CLLocationCoordinate2DIsValid(coordinate) else {
print("Error: the supplied coordinate, \(coordinate), is not valid.")
return nil
}
let vCardString = [
"BEGIN:VCARD",
"VERSION:3.0",
"N:;Shared Location;;;",
"FN:Shared Location",
"item1.URL;type=pref:http://maps.apple.com/?ll=\(coordinate.latitude),\(coordinate.longitude)",
"item1.X-ABLabel:map url",
"END:VCARD"
].joinWithSeparator("\n")
let vCardFilePath = (cachesPathString as NSString).stringByAppendingPathComponent("vCard.loc.vcf")
do {
try vCardString.writeToFile(vCardFilePath, atomically: true, encoding: NSUTF8StringEncoding)
}
catch let error {
print("Error, \(error), saving vCard: \(vCardString) to file path: \(vCardFilePath).")
}
return NSURL(fileURLWithPath: vCardFilePath)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment