iOS上的Instagram Oauth重定向

Bad*_*ate 3 oauth ios instagram instagram-api

我正在编写一个需要使用Oauth连接到Instagram的iOS应用程序.o-auth的标准iOS机制是提供带有自定义方案的重定向URL.例如,myapp:// oauth/redirect - 并在您的iOS应用程序中注册.除了Instagram的管理门户似乎在设置有效的重定向URI时阻止除http或https之外的任何方案,这一切都很好,很花哨.

例

有关如何使这项工作的任何想法?我是否被迫添加服务器组件来捕获/重定向?

Bad*_*ate 5

有两种方法可以在不使用自定义URL方案的情况下执行此操作,每种方案都有正面和负面的:

  1. 通用链接 - PROS:重定向到Safari,因此您无需自己构建Web浏览器.Safari重定向还允许使用用户iCloud钥匙串,这可以使您的用户更轻松地登录体验.缺点:设置它是一种熊,需要您自己的服务器和已发布的应用程序.似乎在模拟器中不起作用.
  2. 设置并呈现UIWebView,并webView:shouldStartLoadWithRequest:navigationType:从片段中捕获访问令牌,并阻止加载/重定向.这允许您使用任何您想要的URL作为重定向URL,因为它无关紧要:)

例:

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

项目清单