请求完成后获取数据

时间:2015-05-06 16:28:23

标签: ios swift wkwebview

就我而言,主要问题是我在用户注册后无法获取数据。

在我的Windows手机应用程序中,我曾经通过触发事件LoadCompleted来获取数据。

此代码显示我之前是如何完成的:

private void AuthBrowser_LoadCompleted(object sender, NavigationEventArgs e)
    {
        string responceData = e.Uri.OriginalString;
        if (responceData.Contains("access_token"))
        {
            if (settings.Contains("IsRegistered"))
            {
                settings["IsRegistered"] = true;
            }
            else
            {
                settings.Add("IsRegistered", true);
            }
            var parameters = responceData.Split('#')[1].Split('&');
            var accessToken = parameters[0].Substring(parameters[0].IndexOf("=", StringComparison.Ordinal)).Remove(0, 1);
            if (settings.Contains("AccessToken"))
            {
                settings["AccessToken"] = accessToken;
            }
            else
            {
                settings.Add("AccessToken", accessToken);
            }
            var expiresIn = parameters[1].Substring(parameters[1].IndexOf("=", StringComparison.Ordinal)).Remove(0, 1);
            var uID = parameters[2].Substring(parameters[2].IndexOf("=", StringComparison.Ordinal)).Remove(0, 1);
            if (settings.Contains("UserId"))
            {
                settings["UserId"] = uID;
            }
            else
            {
                settings.Add("UserId", uID);
            }
            if (settings.Contains("IsRegistered"))
            {
                settings["IsRegistered"] = true;
            }
            else
            {
                settings.Add("IsRegistered", true);
            }
            NavigationService.Navigate(new Uri("/Menu.xaml", UriKind.Relative));
        }
    }

但现在在我的IOS应用程序中,我不明白该怎么做。

我使用过WKWebView。

这是我的代码示例:

import UIKit
import WebKit

class ViewController: UIViewController {

    @IBOutlet var containerView : UIView! = nil
    var webView: WKWebView?
    var uiWebView: UIWebView?
    override func loadView() {
        super.loadView()
        self.uiWebView = UIWebView()
        self.webView = WKWebView()
        self.view = self.webView!

    }

    override func viewDidLoad() {
        super.viewDidLoad()

        var url = NSURL(string:"https://oauth.vk.com/authorize?client_id=4537563&" +
            "redirect_uri=http://api.vk.com/blank.html&display=wap&scope=9999999&response_type=token")
        var req = NSURLRequest(URL:url!)

        self.webView!.loadRequest(req)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

感谢。

2 个答案:

答案 0 :(得分:0)

UIWebView的委托协议为您提供了许多跟踪请求状态的方法:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIWebViewDelegate_Protocol/

答案 1 :(得分:0)

尝试使用WKNavigationDelegate

viewDidLoad

中设置委托
class ViewController: UIViewController, WKNavigationDelegate {

  override func viewDidLoad() {
    super.viewDidLoad()
    webView.navigationDelegate = self

    var url = NSURL(string:"https://oauth.vk.com/authorize?client_id=4537563&" +
        "redirect_uri=http://api.vk.com/blank.html&display=wap&scope=9999999&response_type=token")
    var req = NSURLRequest(URL:url!)

    self.webView!.loadRequest(req)
  }
}

在加载完成后使用以下内容获取数据

func webView(webView: WKWebView!, didFinishNavigation navigation: WKNavigation!) {
    // your request has been completed, get the data here
}