Skip to content

Instantly share code, notes, and snippets.

@iAmrSalman
Last active April 8, 2018 09:40
Show Gist options
  • Save iAmrSalman/bc8a3ce017b597473694bc347968f79d to your computer and use it in GitHub Desktop.
Save iAmrSalman/bc8a3ce017b597473694bc347968f79d to your computer and use it in GitHub Desktop.
[Sociable] protocol to handle social deeplinking #swift #protocol
import UIKit
import MessageUI
protocol Sociable: MFMailComposeViewControllerDelegate, UINavigationControllerDelegate {
func openInstagramAccount(withID id: String)
func openTwitterAccount(withID id: String)
func openFacebookAccount(withID id: String)
func openSnapchatAccount(withID id: String)
func sendEmail(to email: String, withDefaultMessage message: String)
func call(_ mobileNumber: String)
}
extension Sociable where Self: UIViewController {
//MARK: - Actions
func openFacebookAccount(withID id: String) {
deeplink(to: "fb://facebook.com/\(id)/", or: "https://www.facebook.com/\(id)/")
}
func openTwitterAccount(withID id: String) {
deeplink(to: "twitter://user?screen_name=\(id)", or: "https://twitter.com/\(id)")
}
func openSnapchatAccount(withID id: String) {
deeplink(to: "snapchat://add/\(id)", or: "https://www.snapchat.com/add/\(id)/")
}
func openInstagramAccount(withID id: String) {
deeplink(to: "instagram://user?username=\(id)", or: "https://instagram.com/\(id)/")
}
func call(_ mobileNumber: String) {
guard let telURL = URL(string: "tel://\(mobileNumber)") else { return }
if UIApplication.shared.canOpenURL(telURL) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(telURL)
} else {
UIApplication.shared.openURL(telURL)
}
}
}
func sendEmail(to email: String, withDefaultMessage message: String) {
if MFMailComposeViewController.canSendMail() {
let composer = MFMailComposeViewController()
composer.mailComposeDelegate = self
composer.setToRecipients([email])
composer.setMessageBody(message, isHTML: false)
self.present(composer, animated: true, completion: nil)
} else {
error(withMessage: NSLocalizedString("Can't send Email", comment: "Error with sending email"))
}
}
//MARK: - Helpers
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith
result: MFMailComposeResult, error: Error?) {
switch result {
case MFMailComposeResult.cancelled:
print("Mail cancelled")
break
case MFMailComposeResult.saved:
print("Mail saved")
break
case MFMailComposeResult.sent:
print("Mail sent")
break
case MFMailComposeResult.failed:
guard let error = error else { return }
self.error(withMessage: error.localizedDescription)
}
dismiss(animated: true, completion: nil)
}
private func deeplink(to deeplink: String, or alternative: String) {
guard let deeplinkURL: URL = URL(string: deeplink) else { return }
guard let webURL: URL = URL(string: alternative) else { return }
if UIApplication.shared.canOpenURL(deeplinkURL) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(deeplinkURL)
} else {
UIApplication.shared.openURL(deeplinkURL)
}
} else {
if #available(iOS 10.0, *) {
UIApplication.shared.open(webURL)
} else {
UIApplication.shared.openURL(webURL)
}
}
}
private func error(withMessage message: String) {
let alert = UIAlertController(title: NSLocalizedString("Error", comment: "Error alert title"), message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Error alert action title"), style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment