web的操作

2017-03-29  本文已影响15人  LeeDev
- (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"];
}

上一篇下一篇

猜你喜欢

热点阅读