- (void)setCustomCookie {
NSHTTPCookieStorage * storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSString * domain = ; //不包括http
NSString *customer_id ;
NSString * customer_token ;
//设置customer_id
{
NSMutableDictionary *properties = [[NSMutableDictionary alloc] init];
[properties setValue:customer_id forKey:NSHTTPCookieValue];
[properties setValue:@"customer_id" forKey:NSHTTPCookieName];
[properties setValue:domain forKey:NSHTTPCookieDomain];
//1年的过期时间
[properties setValue:[NSDate dateWithTimeIntervalSinceNow:24*60*60*30*12] forKey:NSHTTPCookieExpires];
[properties setValue:@"/" forKey:NSHTTPCookiePath];
NSHTTPCookie *cookie = [[NSHTTPCookie alloc] initWithProperties:properties];
[storage setCookie:cookie];
}
//设置customer_token
{
NSMutableDictionary *properties = [[NSMutableDictionary alloc] init];
[properties setValue:customer_token forKey:NSHTTPCookieValue];
[properties setValue:@"customer_token" forKey:NSHTTPCookieName];
[properties setValue:domain forKey:NSHTTPCookieDomain];
//1年的过期时间
[properties setValue:[NSDate dateWithTimeIntervalSinceNow:24*60*60*30*12] forKey:NSHTTPCookieExpires];
[properties setValue:@"/" forKey:NSHTTPCookiePath];
NSHTTPCookie *cookie = [[NSHTTPCookie alloc] initWithProperties:properties];
[storage setCookie:cookie];
}
NSLog(@"cookies = %@",storage.cookies);
}
- (void)removeAllCookie {
NSHTTPCookieStorage * storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in storage.cookies) {
[storage deleteCookie:cookie];
}
}
- (void)registerUserAgent:(UIWebView *)webView {
NSString *originUserAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
NSString * customUserAgent = @"App IOS";
NSString *newUserAgent = [NSString stringWithFormat:@"%@;%@",originUserAgent,customUserAgent];
NSDictionary *dictionary = @{@"UserAgent":newUserAgent};
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self hideHud];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
JH_PLJSContextObject *jsContext = [[JH_PLJSContextObject alloc]init];
jsContext.delegate = self;
context[@"delegate"] = jsContext;
self.context = context;
self.titleView.title = [_webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。