Eri-Sklii
Eri-Sklii

Reputation: 589

Open Facebook profile in app

I am trying to make that when the user press Facebook button, it opens the Facebook application, or in browser.

Import code:

import Foundation

Extension code:

extension UIApplication {
    class func tryURL(urls: [String]) {
        let application = UIApplication.sharedApplication()
        for url in urls {
            if application.canOpenURL(NSURL(string: url)!) {
                application.openURL(NSURL(string: url)!)
                return
            }
        }
    }
}

Function code:

func fimageTapped()
{
    UIApplication.tryURL([
        "fb://profile/1357163924", // App
        "http://www.facebook.com/1357163924" // Website if app fails
        ])
}

I get error at line:

class func tryURL(urls: [String]) {

Error message:

Invalid redeclaration of "tryURL"

This is where i found the code from: Xcode swift link to facebook page

Anyone has any idea what is wrong here?

Upvotes: 1

Views: 3974

Answers (4)

oscar castellon
oscar castellon

Reputation: 3138

For Swift 4

            let webURL: NSURL = NSURL(string: "https://www.facebook.com/ID")!
            let IdURL: NSURL = NSURL(string: "fb://profile/ID")!

            if(UIApplication.shared.canOpenURL(IdURL as URL)){
                // FB installed
                UIApplication.shared.open(webURL as URL, options: [:], completionHandler: nil)
            } else {
                // FB is not installed, open in safari
                UIApplication.shared.open(webURL as URL, options: [:], completionHandler: nil)
            }

Upvotes: 0

whoswho
whoswho

Reputation: 202

Swift 3:

Important: UIApplication is declared in UIKit so make sure you import UIKit aswell.

    import Foundation
    import UIKit

extension UIApplication {
    class func tryURL(_ urls: [String]) {
        let application = UIApplication.shared
        for url in urls {
            if application.canOpenURL(URL(string: url)!) {
            application.open(URL(string: url)!, options: [:], completionHandler: nil)

            return

            }
        }
    }
}

Furthermore:

application.openURL(NSURL(string: url)!)

have in Swift 3 changed to open and with completionHandler:

application.open(URL(string: url)!, options: [:], completionHandler: nil)

Next in your viewController: declare your facebook app and web-urls above viewDidLoad and create a function or IBAction func where you do your UIapplication.tryURL:

import UIKit

class ViewController: UIViewController {

let fbAppUrl = "facebook://profile/yourFBprofileID"
let fbWebUrl = "https://www.facebook.com/yourFBprofileID"

override func viewDidLoad() {
    super.viewDidLoad()

}

func openFacebook() {

    UIApplication.tryURL([fbAppUrl, fbWebUrl])

    }

}

First iOS will try to open the fbAppUrl but if the facebook app is not installed on the device it will open fbWebUrl in Safari instead.

Upvotes: 0

Saul Diaz
Saul Diaz

Reputation: 51

In Swift 3 use this:

let facebookID: String = "000000000000"
let facebookName: String = "name"
let facebookURLID = URL(string: "fb://profile/\(facebookID)")!
let facebookURLWeb = URL(string: "https://m.facebook.com/\(facebookName)?id=\(facebookID)")!

if(UIApplication.shared.canOpenURL(facebookURLID)){
    UIApplication.shared.openURL(facebookURLID)
} else {
    UIApplication.shared.openURL(facebookURLWeb)
}

Remember that from iOS 9 and 10 you need to declare in the Info.plist:

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

With this the queries for the Facebook schemes will not fail with a permission error:

"This app is not allowed to query for scheme fb"

Upvotes: 5

Eri-Sklii
Eri-Sklii

Reputation: 589

Used this, worked fine:

var fbURLWeb: NSURL = NSURL(string: "https://www.facebook.com/ID")!
        var fbURLID: NSURL = NSURL(string: "fb://profile/ID")!

        if(UIApplication.sharedApplication().canOpenURL(fbURLID)){
            // FB installed
            UIApplication.sharedApplication().openURL(fbURLID)
        } else {
            // FB is not installed, open in safari
            UIApplication.sharedApplication().openURL(fbURLWeb)
        }

Upvotes: 0

Related Questions