Facebook 未在应用程序中打开页面 swift 3

Facebook not open page in app swift 3

我在我的网站上有一个 link 到我的 facebook,它可以在 phone 的浏览器中打开。

如果安装了该应用程序,我希望它默认在 Facebook 应用程序中打开。

我试着输入这段代码,但它不起作用。始终且仅通过 safari 打开 facebook 页面,而不是通过 facebook 应用程序。如果你能帮助我,请。

@IBAction func supporto(_ sender: UIBarButtonItem) {
    let Username =  "831831923611308" 
    let appURL = NSURL(string: "fb://profile/\(Username)")!
    let webURL = NSURL(string: "https://facebook.com/\(Username)")!
    let application = UIApplication.shared

    if application.canOpenURL(appURL as URL) {

        if #available(iOS 10.0, *) {
            application.open(appURL as URL)
        } else {
            // Fallback on earlier versions
        }
    } else {
        if #available(iOS 10.0, *) {
            application.open(webURL as URL)
        } else {
            // Fallback on earlier versions
        }

    }

    }

我可以确认您使用的代码确实有效。我把它放在一个新项目中,安装在我的 phone 上,并设置 Info.plist 值如下:

我的代码如下:

有几个原因可能导致您的代码无法正常工作。

  • 您可能需要清理并重建您的项目,以确保您在 Info.plist 中的更改能够加载。
  • 您可能需要验证您是否将 LSApplicationQueriesSchemes 添加到 Info.plist 并且这是应用程序设置加载的 plist。
  • 您必须 运行 在真实设备上执行此操作,因为您的模拟器不应该安装 Facebook。
  • 确认 Facebook 已安装并且是最新的。
  • 确保您使用的是 运行最新版本的 Xcode 和 iOS。
  • 如果一切都失败了,请删除应用程序并重建并重新安装它。

您可以检查您的 Info.plist 是否正确加载了查询方案,方法是转到项目编辑器的信息选项卡并查看以确保 LSApplicationQueriesSchemes 存在并具有指定的选项:

If you call the “canOpenURL” method on a URL that is not in your whitelist, it will return “NO”, even if there is an app installed that has registered to handle this scheme. A “This app is not allowed to query for scheme xxx” syslog entry will appear.

If you call the “openURL” method on a URL that is not in your whitelist, it will fail silently. A “This app is not allowed to query for scheme xxx” syslog entry will appear.

所以你必须在 info.plist

中将 fb 放入 LSApplicationQueriesSchemes