NoSixties NoSixties - 2 months ago 33
iOS Question

InstagramKit receivedValidAccessTokenFromURL doesn't work

I'm trying to use Shyambhat's InstagramKit (from the DEV branch) and I just can't get the

receivedValidAccessTokenFromURL
method working.

Currently I'm working with swift 3.0 the code looks as follows (for testing purposes)

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

engine = InstagramEngine.shared();
let iScope: InstagramKitLoginScope = [.comments, .followerList, .likes, .relationships];

let iAuthURL = engine.authorizationURL(for: iScope);

loginWebView.loadRequest(URLRequest(url: iAuthURL));
loginWebView.delegate = self;
}

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
let url = request.url;
try! engine.receivedValidAccessToken(from: url!)

return true;
}


I've set the following in my info.plist


InstagramAppClientId: 'myid'

InstagramAppRedirectURL: 'http://example.com'


For some reason instagram doesn't allow the redirect url to be app:// anymore.
When I put a debug on the request.url. This shows me the following

https://api.instagram.com/oauth/authorize/?client_id=myid&redirect_uri=http%3A//example.com&response_type=token&scope=comments%20relationships%20likes%20follower_list


I'm hoping someone here has experience with this library and can help me out.

Answer

I've figured it out. What I did is the following.

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        do {
            if let url = request.url {
                if (String(describing: url).range(of: "access") != nil){
                    try engine.receivedValidAccessToken(from: url)

                if let accessToken = engine.accessToken {
                    NSLog("accessToken: \(accessToken)")
                }
                }
            }
        } catch let err as NSError {
            print(err.debugDescription)
        }
        return true
    }

I check if the url contains the access token if it does then I'll let the engine actually get the access token. The error occurred because it was trying to get the access token while it wasn't available when you open the app up the first time.

Comments