菜鸟教程小白 发表于 2022-12-12 22:17:36

ios - FBSDKLoginKit 在登录和确认页面后不重定向我的应用程序


                                            <p><p>我正在使用 Swift 3.0 集成 Facebook SDK 4.15.1。</p>

<p>我已按照链接中提到的所有步骤进行操作
<a href="https://developers.facebook.com/docs/ios/getting-started" rel="noreferrer noopener nofollow">Getting started with iOS</a> ,我可以登录 Facebook 并使用 Safari 浏览器进行授权,但之后该控件没有返回到我的应用程序并显示空白页面。但是当我按下完成按钮时,委托(delegate)方法 <code>didCompleteWith 结果:</code> 被调用并出现错误 <code>OAuthException</code>。在我的应用程序中也永远不会调用 <code>OpenURL</code> 方法。我还彻底验证了我的属性列表文件和应用程序 ID,包标识符或应用程序 ID 没有错误。 </p>

<p>在设备 < iOS 9.0 中不显示空白页。</p>

<p>感谢任何帮助。</p>

<p>以下是我在 Swift 3.0 中用于登录和获取访问 token 的代码,</p>

<pre><code>AppDelegate.swift
    import UIKit
    import CoreData
    import FBSDKCoreKit
    import FBSDKLoginKit
    import FBSDKShareKit

    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate{

   var window: UIWindow?
   func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: ?) -&gt; Bool {   
            FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)   
            return true
      }

    func application(application: UIApplication,
                     openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -&gt; Bool {
      return FBSDKApplicationDelegate.sharedInstance().application(application, open: url as URL!, sourceApplication: sourceApplication, annotation: annotation)   
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
      FBSDKAppEvents.activateApp()
    }
</code></pre>

<p>ViewController.swift</p>

<pre><code>import UIKit
import FBSDKLoginKit
import FBSDKCoreKit

class ViewController: UIViewController, GIDSignInUIDelegate, FBSDKLoginButtonDelegate{
    public func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
   }

      let loginView : FBSDKLoginButton = FBSDKLoginButton()
      self.view.addSubview(loginView)
      loginView.center = self.view.center
      loginView.readPermissions = [&#34;public_profile&#34;, &#34;email&#34;, &#34;user_friends&#34;]
      loginView.delegate = self   
}
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
    if error == nil {
      print(&#34;Success&#34;)

      let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: &#34;me&#34;, parameters: [&#34;fields&#34;:&#34;id,interested_in,gender,birthday,email,age_range,name,picture.width(480).height(480)&#34;])
      graphRequest.start(completionHandler: { (connection, result, error) -&gt; Void in

            if ((error) != nil)
            {
                print(&#34;Error: \(error)&#34;)
            }
            else
            {
                print(&#34;fetched user: \(result)&#34;)
            }
      })
    }else{
      print(&#34;Failure&#34;)
    }
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我已通过在属性列表中添加以下传输安全 key 来解决此问题。</p>

<pre><code>&lt;key&gt;NSAppTransportSecurity&lt;/key&gt;
&lt;dict&gt;
    &lt;key&gt;NSExceptionDomains&lt;/key&gt;
    &lt;dict&gt;
      &lt;key&gt;facebook.com&lt;/key&gt;
      &lt;dict&gt;
            &lt;key&gt;NSIncludesSubdomains&lt;/key&gt;
            &lt;true/&gt;
            &lt;key&gt;NSExceptionRequiresForwardSecrecy&lt;/key&gt;
            &lt;false/&gt;
      &lt;/dict&gt;
      &lt;key&gt;fbcdn.net&lt;/key&gt;
      &lt;dict&gt;
            &lt;key&gt;NSIncludesSubdomains&lt;/key&gt;
            &lt;true/&gt;
            &lt;key&gt;NSExceptionRequiresForwardSecrecy&lt;/key&gt;
            &lt;false/&gt;
      &lt;/dict&gt;
      &lt;key&gt;akamaihd.net&lt;/key&gt;
      &lt;dict&gt;
            &lt;key&gt;NSIncludesSubdomains&lt;/key&gt;
            &lt;true/&gt;
            &lt;key&gt;NSExceptionRequiresForwardSecrecy&lt;/key&gt;
            &lt;false/&gt;
      &lt;/dict&gt;
    &lt;/dict&gt;
&lt;/dict&gt;
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - FBSDKLoginKit 在登录和确认页面后不重定向我的应用程序,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/39701394/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/39701394/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - FBSDKLoginKit 在登录和确认页面后不重定向我的应用程序