简体   繁体   中英

iOS Swift - Instagram API get response from WebView NSURL Request

I am using the following block allow users of my app to authorize with Instagram:

func authorizeInstagram() {

        let myWebView:UIWebView = UIWebView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))
        myWebView.delegate = self
        myWebView.loadRequest(NSURLRequest(URL: NSURL(string: "https://instagram.com/oauth/authorize/?client_id=xxxxxxxxxxxxxxxxxx&redirect_uri=xxxxxxxxxxx/&response_type=token")!))
        self.view.addSubview(myWebView)
 }

My question is, how can I get the reponse JSON from the webview NSURL request once user has completed auth?

in delegate method you can get the request and find it's URL.absoluteString ,what code you want will be in the string :

func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {

    let str = request.URL?.absoluteString
    print(str)
    return true
}

hope it be helpful :D

In the info.plist file you register the app under URL Types. Put the identifier as your bundle id, the URL Scheme as your redirect, and editor as the role.

When the user is done completing the auth in the app delegate will called back in the method:

func application(application: UIApplication,
  openURL url: NSURL,
  sourceApplication: String?,
  annotation: AnyObject?) -> Bool

Inside that you can parse through the response. Here an example in C#, don't have it in swift but shouldn't be to hard to translate. Hope that helps!

if (url.Scheme == "youappscheme")
{
    var queryParams = url.Query.Split('&');
    // grab the one you want
    return true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM