Skip to content

Instantly share code, notes, and snippets.

@katiesmillie
Created October 26, 2016 20:06
Show Gist options
  • Save katiesmillie/4c571545fbbd4dc4ab752bcbb4bbcbbe to your computer and use it in GitHub Desktop.
Save katiesmillie/4c571545fbbd4dc4ab752bcbb4bbcbbe to your computer and use it in GitHub Desktop.
func tappedInstagram(sharingOptionsView: SharingOptionsView) {
Analytics().track(event: "Share Instagram")
guard let instagramURL = URL(string: "instagram://app") else {return}
if UIApplication.shared.canOpenURL(instagramURL) {
guard let imageToShare = makeCompositeToShare() else { return }
let imageData = UIImageJPEGRepresentation(imageToShare, 1.0)
do {
let fileURL = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("image.igo")
try imageData?.write(to: fileURL, options: .atomic)
documentInteractionController.url = fileURL
documentInteractionController.uti = "com.instagram.exclusivegram"
documentInteractionController.presentOpenInMenu(from: view.bounds, in: view, animated: true)
} catch {
print(error)
showAlertUnableToShareImage()
}
} else {
showAlertUnableToShareImage()
}
}
func showAlertUnableToShareImage() {
let alertController = UIAlertController(title: nil, message: "Unable to open image in Instagram ", preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(action)
present(alertController, animated: true, completion: nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment