iOS开发之WebView怎么加载post请求并且传参数

如题所述

由于种种因素,现在国内的移动开发大都是原生和html5混合开发, 那么iOS端的UIWebView和WKWebView就启到了非常重要的作用! 而从我们原生页面跳转的web页面的时候我们往往需要像前端传递参数,今天我们要说的就是:webView怎么加载post请求并传递参数!

代码如下:

    UIWebView *webView = [[UIWebView alloc] init];  

    NSString *bodyShare = [NSString stringWithFormat: @"hID=%@", userID];  

    NSMutableURLRequest * requestShare = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:self.urlStr]];  

    [requestShare setHTTPMethod: @"POST"];  

    [requestShare setHTTPBody: [bodyShare dataUsingEncoding: NSUTF8StringEncoding]];  

    [webView loadRequest:requestShare];  



    当然与之相对应的还有get请求来传递参数的,代码如下:

    UIWebView *webView = [[UIWebView alloc] init];  

    self.urlStr = [NSString stringWithFormat:@"%@/tokenredirect?ostype=iphone&token=%@&time=%@",kServerPrefixURL,token,time];  

    NSURLRequest * requestShare = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:self.urlStr]];  

    [webView loadRequest:requestShare];  

温馨提示:答案为网友推荐,仅供参考
相似回答