通過要求標頭可以傳遞 一些web 想要的一些資訊,但是這個方法有些局限性,如果web 跳轉的時候頭的資訊會訊息,最好的方法就是自訂cookie 傳值
NSURL* url = [NSURL URLWithString:self.url];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
NSString *body = [NSString stringWithFormat: @"uuid=%@&sessionid=%@", uuid,sessionid];
[request setHTTPBody: [body dataUsingEncoding: NSUTF8StringEncoding]];
[request setHTTPMethod: @"POST"];
[self.myWebView loadRequest:request];
[self.view addSubview:self.myWebView];
自訂ccokie 傳值
// 定義 cookie 要設定的 host
NSURL* url = [NSURL URLWithString:self.url];
NSURL *cookieHost = [NSURL URLWithString:self.url];
NSDictionary *dic = @{@"uuid":uuid,@"sessionid":sessionid,@"feedid":self.feedid,@"softversion":softversion,@"devicetype":deviceId,@"touuid": self.touuid};
[dic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
// 設定 cookie
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:
[NSDictionary dictionaryWithObjectsAndKeys:
[cookieHost host], NSHTTPCookieDomain,
[cookieHost path], NSHTTPCookiePath,
key,NSHTTPCookieName,
obj,NSHTTPCookieValue,
nil]];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
}];
[self.myWebView loadRequest:request];//載入
[self.view addSubview:self.myWebView];