Skip to content

Instantly share code, notes, and snippets.

@Manabu-GT
Last active September 27, 2016 11:07
Show Gist options
  • Save Manabu-GT/6f223ceea1d9a6facb9b745e942f70c9 to your computer and use it in GitHub Desktop.
Save Manabu-GT/6f223ceea1d9a6facb9b745e942f70c9 to your computer and use it in GitHub Desktop.
Share Location data with vCard format on iOS using UIActivityViewController
import CoreLocation
func shareLocation(coordinate:CLLocationCoordinate2D) -> Void {
guard let cachesPathString = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true).first else {
print("Error: couldn't find the caches directory.")
return
}
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)
let activityViewController = UIActivityViewController(activityItems: [NSURL(fileURLWithPath: vCardFilePath)], applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivityTypePrint,
UIActivityTypeCopyToPasteboard,
UIActivityTypeAssignToContact,
UIActivityTypeSaveToCameraRoll,
UIActivityTypePostToVimeo,
UIActivityTypePostToTencentWeibo,
UIActivityTypePostToWeibo]
presentViewController(activityViewController, animated: true, completion: nil)
} catch let error {
print("Error, \(error), saving vCard: \(vCardString) to file path: \(vCardFilePath).")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment