Facebook无法在应用程序Swift 3中打开页面

Facebook无法在应用程序Swift 3中打开页面

问题描述:

我在我的网站上有一个指向我的Facebook的链接,该链接在手机的浏览器中打开.

I have a link to my facebook, on my website, which opens in the phone's browser.

如果已安装,我希望它默认在facebook应用程序中打开.

I would like it to open in the facebook app by default, if the app is installed.

我试图放置此代码,但是它不起作用.始终仅通过野生动物园而不是通过Facebook应用程序打开Facebook页面.如果您能帮助我的话.

I tried to put this code but it does not work. Opens the facebook page always and only via safari and not through the facebook app. If you can help me please.

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

    }

    }

我可以确认您使用的代码确实有效.我将其放置在一个新项目中,将其安装在手机上,并按如下所示设置Info.plist值:

I can confirm that the code you are using does work. I placed it in a new project, installed it on my phone, and set the Info.plist values as follows:

我的代码如下:

出于某些原因,您的代码可能无法正常工作.

There are a few reasons that your code may not have worked.

  • 您可能需要清理并重建项目,以确保您在Info.plist中所做的更改加载.
  • 您可能需要验证是否已将LSApplicationQueriesSchemes添加到Info.plist,并且这是应用程序要加载的plist.
  • 您必须在真实的​​设备上运行此程序,因为您的模拟器不应安装Facebook.
  • 验证是否已安装Facebook并保持最新状态.
  • 确保您正在运行最新版本的Xcode和iOS.
  • 如果其他所有方法均失败,请删除该应用,然后重新生成并重新安装.
  • You may need to clean and rebuild your project to make sure that your changes in Info.plist load.
  • You may need to verify that you added the LSApplicationQueriesSchemes to Info.plist and that that is the plist that the application is set up to load.
  • You must run this on a real device as your simulator should not have Facebook installed.
  • Verify that Facebook is installed and up-to-date.
  • Make sure that you are running the most up-to-date version of Xcode and iOS.
  • If all else fails, delete the app and rebuild and reinstall it.

您可以通过以下方法检查您的Info.plist是否正确加载了查询方案:转到项目编辑器的信息"选项卡,并确保使用指定的选项确保LSApplicationQueriesSchemes在那里:

You can check that your Info.plist is correctly loading the Queries Schemes by going to the Project Editor's Info tab and looking to make sure that LSApplicationQueriesSchemes is there with the options specified: