2017-05-07 52 views
0

我使用swifter搶訪問令牌回調IOS不返回認證

swifter.authorize(with: URL(string: "callback://")!, presentFrom: self, success: { accessToken, response in 
     print("HELLO") 
     print(accessToken) 
     // ... 
    }, failure: { error in 
     // ... 
    }) 

我可以成功登錄,因爲應用程序打開了SFSafariViewController,但當時我沒有直接回我的應用程序,成功回調是永遠不會被調用

這裏是爲更快的代碼,我看到presentFrom被假設是SFSafariViewController,但沒有被觸發

public func authorize(with callbackURL: URL, presentFrom presentingViewController: UIViewController? , success: TokenSuccessHandler?, failure: FailureHandler? = nil) { 
    self.postOAuthRequestToken(with: callbackURL, success: { token, response in 
     var requestToken = token! 
     NotificationCenter.default.addObserver(forName: .SwifterCallbackNotification, object: nil, queue: .main) { notification in 
      NotificationCenter.default.removeObserver(self) 
      presentingViewController?.presentedViewController?.dismiss(animated: true, completion: nil) 
      let url = notification.userInfo![CallbackNotification.optionsURLKey] as! URL 

      let parameters = url.query!.queryStringParameters 
      requestToken.verifier = parameters["oauth_verifier"] 

      self.postOAuthAccessToken(with: requestToken, success: { accessToken, response in 
       self.client.credential = Credential(accessToken: accessToken!) 
       success?(accessToken!, response) 
       }, failure: failure) 
     } 

     let authorizeURL = URL(string: "oauth/authorize", relativeTo: TwitterURL.oauth.url) 
     let queryURL = URL(string: authorizeURL!.absoluteString + "?oauth_token=\(token!.key)")! 

     if #available(iOS 9.0, *) , let delegate = presentingViewController as? SFSafariViewControllerDelegate { 
      let safariView = SFSafariViewController(url: queryURL) 
      safariView.delegate = delegate 
      presentingViewController?.present(safariView, animated: true, completion: nil) 
     } else { 
      UIApplication.shared.openURL(queryURL) 
     } 
    }, failure: failure) 
} 
爲SFSafariViewController的delgate方法
+0

你艾威找到解決這個?我現在也在爲此付出努力 –

回答

0

它只是等待並說「重定向你回到應用程序」,但從來沒有真正回到應用程序?

您需要這兩個功能添加到AppDelegate中

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { 
    Swifter.handleOpenURL(url) 
    return true 
} 

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool { 
    Swifter.handleOpenURL(url as URL) 

    return true 
} 

也嘗試一下本作進一步的幫助:How to Authorize Twitter with Swifter