Skip to content

Instantly share code, notes, and snippets.

@mehmetfarhan
Created April 27, 2019 09:36
Show Gist options
  • Save mehmetfarhan/93550396467ca17266bce0449dd51329 to your computer and use it in GitHub Desktop.
Save mehmetfarhan/93550396467ca17266bce0449dd51329 to your computer and use it in GitHub Desktop.
SocialNetwork
//
// SocialNetwork.swift
// SocialNetwork
//
// Created by Mohammad Farhan on 4/27/19.
// Copyright © 2019 SocialNetwork. All rights reserved.
//
import UIKit
protocol ViewPresentable: AnyObject {
func open(socialNetwork: SocialNetwork)
func presentSafari(with url: String)
}
extension ViewPresentable {
func open(socialNetwork: SocialNetwork) {
let application = UIApplication.shared
let socialNetworkURL = socialNetwork.url
if let scheme = URL(string: socialNetworkURL.scheme),
application.canOpenURL(scheme) {
application.open(scheme,
options: [:],
completionHandler: nil)
} else {
presentSafari(with: socialNetworkURL.page)
}
}
func presentSafari(with url: String) {
self.present(safari: url)
}
}
enum SocialNetwork {
case facebook(String)
case twitter(String)
case instagram(String)
case snapchat(String)
case pinterest(String)
case youtube(String)
case linkedIn(String)
case tumblr(String)
var url: (scheme: String, page: String) {
switch self {
case let .facebook(pageId):
return (scheme: "fb://profile/\(pageId)", page: "https://www.facebook.com/\(pageId)")
case let .twitter(username):
return (scheme: "twitter:///user?screen_name=\(username)", page: "https://twitter.com/\(username)")
case let .instagram(username):
return (scheme: "instagram://user?username=\(username)", page:"https://www.instagram.com/\(username)")
case let .snapchat(id):
return (scheme: "snapchat://add/\(id)", page: "")
case let .pinterest(id):
return (scheme: "pinterest://user/\(id)", page: "")
case let .youtube(id):
return (scheme: "vnd.youtube://user/\(id)", page: "")
case let.linkedIn(id):
return (scheme: "linkedin://profile?id=\(id)", page: "")
case let .tumblr(id):
return (scheme: "tumblr://x-callback-url/\(id)", page: "")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment