Instagram integration not working on iOS using webView

In my application, I did Instagram authentication using webView. At first it showed the Instagram login screen, after a successful login I downloaded accessToken, then I got other data using accessToken.

here is my loadrequest method which i call 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) } 

Here are my webViewDelegate methods where I call the checkRequestForcallBackURL method

  func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool { return checkRequestForcallBackURL(request: request) } 

In this method, I check if iam gets 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 } 

Now, when the login requests a security code, after providing the security code, it redirects me to the Instagram application that I do not want. I do not know how to proceed further

+3
source share
3 answers

I am implementing Instagram login in one application. What am I doing. to get the token, instagram just introduces a new ViewController with a web page for viewing and loading the 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) 

then in the 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) } 

after receiving the instagram token, I simply reject the ViewController and save the token locally. Then select the user information in the background and show the bootloader or whatever you want.

Hope this helps you. :)

0
source

I use WKWebView , and after the user enters their security code, I implement WKWebViewDelegate decidePolicyFor and look for the https://www.instagram.com redirect, which I do not allow, and instead send them back to the original authentication URL. which now, since they entered the code, works.

 public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { if let url = navigationAction.request.url, url.absoluteString == "https://www.instagram.com/" { decisionHandler(.cancel) webView.load(scheme.authenticationURlRequest) return } } 
0
source

Good news, now I think instagram has changed its policy, now we get the code and accesstoken

0
source

Source: https://habr.com/ru/post/1274569/


All Articles