Bad*_*ate 3 oauth ios instagram instagram-api
我正在编写一个需要使用Oauth连接到Instagram的iOS应用程序.o-auth的标准iOS机制是提供带有自定义方案的重定向URL.例如,myapp:// oauth/redirect - 并在您的iOS应用程序中注册.除了Instagram的管理门户似乎在设置有效的重定向URI时阻止除http或https之外的任何方案,这一切都很好,很花哨.
有关如何使这项工作的任何想法?我是否被迫添加服务器组件来捕获/重定向?
有两种方法可以在不使用自定义URL方案的情况下执行此操作,每种方案都有正面和负面的:
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)
项目清单