使用WebView在iOS中进行Instagram集成失败 [英] Instagram integration fails in iOS by using webView
问题描述
在我的应用程序中,我正在使用webView进行Instagram身份验证.首先显示了Instagram登录屏幕,成功登录后,我正在获取accessToken,然后使用accessToken获取了其他详细信息.
In my application i was doing Instagram authentication using webView .first it was showing Instagram login screen ,after successfully login i was fetching accessToken then i fetched other details using the accessToken.
这是我在viewDidLoad中调用的loadrequest方法
here is my loadrequest method which i am calling in viewDidLoad
func loadrequest(){
let authURL = String(format: "%@?client_id=%@&redirect_uri=%@&response_type=token&scope=%@", arguments: [INSTAGRAM_API.INSTAGRAM_AUTHURL,INSTAGRAM_API.INSTAGRAM_CLIENT_ID,INSTAGRAM_API.INSTAGRAM_REDIRECT_URI, INSTAGRAM_API.INSTAGRAM_SCOPE ])
let urlRequest = URLRequest.init(url: URL.init(string: authURL)!)
instaWebview.loadRequest(urlRequest)
}
这是我的webViewDelegate方法,我在其中调用checkRequestForcallBackURL方法
here is my webViewDelegate methods where i am calling checkRequestForcallBackURL method
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
return checkRequestForcallBackURL(request: request)
}
通过这种方法,我正在检查是否获取访问令牌
inthis method i am checking whether iam getting my accesstoken or not
func checkRequestForcallBackURL(request : URLRequest) ->Bool{
let requestURLString = (request.url?.absoluteString)! as String
if requestURLString.hasPrefix(INSTAGRAM_API.INSTAGRAM_REDIRECT_URI) {
let range: Range<String.Index> = requestURLString.range(of: "#access_token=")!
handleAuth(authToken: requestURLString.substring(from: range.upperBound))
return false;
}
return true
}
现在,在登录时询问其安全代码时,在提供安全代码后,它将我重定向到我不想要的Instagram App.我不知道如何进一步进行操作
Now while login its asking security code ,after giving security code it redirects me to Instagram App which i don't want .I don't know how to proceed further
推荐答案
我在自己的应用程序中实现了Instagram登录.我是做什么的.要获取instagram令牌,只需提供带有Webview的新ViewController并加载instagram登录页面即可.
I implement Instagram login in one my application. What i do is. for getting instagram token simply present new ViewController with webview and load instagram login page.
let authURL = String(format: "%@?client_id=%@&redirect_uri=%@&response_type=token&scope=%@&DEBUG=True", arguments: [INSTAGRAM_IDS.INSTAGRAM_AUTHURL,INSTAGRAM_IDS.INSTAGRAM_CLIENT_ID,INSTAGRAM_IDS.INSTAGRAM_REDIRECT_URI, INSTAGRAM_IDS.INSTAGRAM_SCOPE ])
let urlRequest = URLRequest.init(url: URL.init(string: authURL)!)
web_instaView.loadRequest(urlRequest)
然后使用webview委托方法
then in webview delegate method
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
return checkRequestForCallbackURL(request: request)
}
func checkRequestForCallbackURL(request: URLRequest) -> Bool {
let requestURLString = (request.url?.absoluteString)! as String
if requestURLString.hasPrefix(INSTAGRAM_IDS.INSTAGRAM_REDIRECT_URI) {
let range: Range<String.Index> = requestURLString.range(of: "#access_token=")!
handleAuth(authToken: requestURLString.substring(from: range.upperBound))
return false;
}
return true
}
func handleAuth(authToken: String) {
print("Instagram authentication token ==", authToken)
INSTAGRAM_IDS.INSTAGRAM_USER_TOCKEN = authToken
UserDefaults.standard.setValue(INSTAGRAM_IDS.INSTAGRAM_USER_TOCKEN, forKey: "INSTAGRAM_USER_TOCKEN")
NotificationCenter.default.post(name: NSNotification.Name("instaLoginDone"), object: nil)
self.dismiss(animated: true, completion: nil)
}
获取instagram令牌后,我简单地关闭了ViewController并在本地保存Token.比在后台模式下获取用户信息,并显示加载程序或您想要的任何内容.
after getting instagram token, i simple dismiss that ViewController and save Token locally. Than fetch user info in background mode and show loader or whatever you want.
希望这会对您有所帮助.:)
Hope this will help you. :)
这篇关于使用WebView在iOS中进行Instagram集成失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!