簡體   English   中英

iOS Swift-Instagram API從WebView NSURL請求獲取響應

[英]iOS Swift - Instagram API get response from WebView NSURL Request

我正在使用以下塊允許我的應用程序的用戶通過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)
 }

我的問題是,一旦用戶完成身份驗證,如何從webview NSURL請求中獲取響應JSON?

在委托方法中,您可以獲取請求並找到它的URL.absoluteString ,所需的代碼將在字符串中:

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

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

希望對您有所幫助:D

在info.plist文件中,您可以在URL Types下注冊該應用程序。 將標識符作為您的捆綁包ID,將URL方案作為您的重定向,並將編輯器作為角色。

當用戶完成應用程序委托中的auth后,將在方法中回調:

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

在其中,您可以解析響應。 這里是C#中的示例,不要急於理解,但不難翻譯。 希望有幫助!

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM