2016-10-28 45 views
2

我正在編寫需要使用Oauth連接到Instagram的iOS應用程序。用於o-auth的標準iOS機制是提供具有自定義方案的重定向URL。例如,myapp:// oauth/redirect - 並在iOS應用程序中註冊。除了Instagram的管理門戶似乎阻止任何方案,但在設置有效的重定向URI時使用http或https時,這一切都很好,並且很花哨。iOS上的Instagram Oauth重定向

Example

如何使這項工作有什麼想法?我是否被迫添加一個服務器組件來捕獲/重定向?

+2

你可以使用一個通用的鏈接列表項的https:/ /developer.apple.com/ios/universal-links/? – Paulw11

+0

@ Paulw11不理想,因爲你仍然需要一個服務器來啓用通用鏈接......我會給它一個機會,它有可能不起作用,因爲不清楚POST參數是否通過你設置了一個通用鏈接。我們拭目以待。 – BadPirate

+1

使用WebView重定向到http:// localhost /並使用webview委託方法讀取access_token,您必須使用客戶端隱式的oauth – krisrak

回答

4

有兩種方法可以這樣做,而不使用自定義URL方案,每一個正面和負面:

  1. Universal Links - 的觀光:重定向到Safari瀏覽器,這樣你就不必建立一個網絡瀏覽器自己。 Safari重定向還允許使用用戶iCloud keychain,這可以使用戶更輕鬆地使用登錄體驗。 CONS:設置它是一種熊,並且需要您自己的服務器和已發佈的應用程序。似乎沒有在模擬器中工作。
  2. 設置並呈現一個UIWebView,並在webView:shouldStartLoadWithRequest:navigationType:中捕獲來自該片段的訪問令牌,並阻止加載/重定向。這可以讓你使用你想要作爲重定向的網址的,因爲它並不重要:)

例子:

import UIKit 

class OAuthVC : UIViewController, UIWebViewDelegate { 
    @IBOutlet var webView : UIWebView? 

    public var success : ((URLRequest) -> Void)? 
    public var presentVC : UIViewController? 

    func handle(_ url: URL) { 
     view.tag = 1 // Make sure our view and thus webview is loaded. 
     webView!.loadRequest(URLRequest(url: url)) 
    } 

    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool { 
     if request.url?.fragment?.contains("access_token") == true { 
      success!(request) 
      dismiss(animated: true, completion: nil) 
      return false 
     } 
     return true 
    } 

    func webViewDidFinishLoad(_ webView: UIWebView) { 
     // If we stop on a page before the redirect closes us, user interaction is required. Present. 
     presentVC?.present(self, animated: true, completion: nil) 
    } 
} 

+0

我可以在哪裏調用句柄函數() –

+0

@FrankLindstrom - 顯示OAuthVC,一旦可見,就使用認證請求URL調用句柄。 – BadPirate