Skip to content

Instantly share code, notes, and snippets.

@xmhafiz
Last active May 11, 2017 04:07
Show Gist options
  • Select an option

  • Save xmhafiz/cf269068467488efde761bf1bd1d1d9a to your computer and use it in GitHub Desktop.

Select an option

Save xmhafiz/cf269068467488efde761bf1bd1d1d9a to your computer and use it in GitHub Desktop.
To simplify open Facebook page based on page id and name using swift

To simplify open Facebook page based on page id and name

Tested for :

  • iOS 9 and above
  • Xcode 8
  • Swift 3

References: stackoverflow

Add Scheme to .plist

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>fb</string>
</array>

Usage function created in below file

@IBAction func handleOpenFb(_ sender: Any) {
    let pageId = "434174436675167"
    let pageName = "apple"

    openFacebook(pageId: pageId, pageName: pageName)
}
import UIKit
extension UIViewController {
func openFacebook(pageId: String, pageName: String) {
guard let facebookURL = URL(string: "fb://page/?id=\(pageId)") else {
return
}
if (UIApplication.shared.canOpenURL(facebookURL)) {
UIApplication.shared.openURL(facebookURL)
} else {
guard let webpageURL = URL(string: "http://facebook.com/\(pageName)/") else {
return
}
UIApplication.shared.openURL(webpageURL)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment