Skip to content

Instantly share code, notes, and snippets.

@naturaln0va
Created February 4, 2016 19:32
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save naturaln0va/e1fed3f1d32ecf951aac to your computer and use it in GitHub Desktop.
Save naturaln0va/e1fed3f1d32ecf951aac 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)
}
@farzadshbfn
Copy link

I think you should return nil in catch block too.

@clbmiami2004
Copy link

I'm getting this error, can you please help? *** Value of type '[String]' has no member 'joinWithSeparator' ***. This would be on line 24 on your code shown above. I'm basically doing this because I need to be able to send the location to other users but I can't find the way of doing it.

@Martifan
Copy link

I'm getting this error, can you please help? *** Value of type '[String]' has no member 'joinWithSeparator' ***. This would be on line 24 on your code shown above. I'm basically doing this because I need to be able to send the location to other users but I can't find the way of doing it.

.joined(separator: "\n")

@rvgeerligs
Copy link

It's now been renamed to joined(separator: "\n")

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