Created
February 4, 2016 19:32
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
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
.joined(separator: "\n")