iOS開發之WebView怎麼載入post請求並且傳引數
阿新 • • 發佈:2019-02-12
由於種種因素,現在國內的移動開發大都是原生和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];
這樣大家就可以看出來其實UIWebView載入資料的方式和http請求資料的方式是一樣, get請求是都是將上行引數拼接到連結的後面, post請求都是放到body裡面進行引數傳遞的!