Xcode快速链接到Facebook页面 [英] Xcode swift link to facebook page

查看:139
本文介绍了Xcode快速链接到Facebook页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有按钮的应用程序,该按钮应该打开一个Facebook页面.它检查用户是否已安装Facebook,并应在应用程序中打开页面.如果未安装,则只需打开带有Safari的页面即可.但是,它不起作用.如果用户安装了Facebook,我怀疑它与错误的地址有关:

I have an app with a button that is suppose to open a Facebook page. It checks to see if the user has Facebook installed and should open the page in the app. If it is not installed, then it simply opens the page with Safari. It is not working however. I suspect it has something to do with the wrong address for it, if the user has Facebook installed:

推荐答案

问题与您的Facebook URL的格式有关,请注意该格式.我使用此扩展名来打开网址.您可以按希望它们尝试打开的顺序为其提供一个URL数组,并首先尝试第一个,如果失败,则转到第二个,依此类推:

The problem is with the format of your Facebook url so notice the format. I use this extension to open urls. You provide it with an array of urls in the order you want them to try to be opened and it tries the first one first and if it fails it goes to the second one and so on:

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
            }
        }
    }
}

使用:

UIApplication.tryURL([
            "fb://profile/116374146706", // App
            "http://www.facebook.com/116374146706" // Website if app fails
            ])

[更新] Swift 4:

[Update] for Swift 4:

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

然后:

UIApplication.tryURL(urls: [
                "fb://profile/116374146706", // App
                "http://www.facebook.com/116374146706" // Website if app fails
                ])

iOS 10/Swift 5的[更新]

[Update] for iOS 10 / Swift 5

extension UIApplication {
    class func tryURL(urls: [String]) {
        let application = UIApplication.shared
        for url in urls {
            if application.canOpenURL(URL(string: url)!) {
                if #available(iOS 10.0, *) {
                    application.open(URL(string: url)!, options: [:], completionHandler: nil)
                }
                else {
                    application.openURL(URL(string: url)!)
                }
                return
            }
        }
    }
}

这篇关于Xcode快速链接到Facebook页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆