Shakti
Shakti

Reputation: 996

Instagram integration fails in iOS by using webView

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.

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)

    }

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
    }

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

Upvotes: 4

Views: 3054

Answers (3)

Shakti
Shakti

Reputation: 996

Good news ,Now i think instagram changed its policy ,now we are getting code and accesstoken

Upvotes: 0

shakked
shakked

Reputation: 811

I'm using a WKWebView and after the user enters their security code, I implement WKWebViewDelegate decidePolicyFor and look for the https://www.instagram.com redirect which I don't allow and instead send them back to the original authentication URL which now, since they've 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
    }
}

Upvotes: 0

Pratik Prajapati
Pratik Prajapati

Reputation: 1105

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)

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)
}

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. :)

Upvotes: 1

Related Questions