美文网首页
web的操作

web的操作

作者: LeeDev | 来源:发表于2017-03-29 13:13 被阅读15次
  • cookie的设置
- (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];
    }
}


  • UserAgent设置 (web初始化的时候)
- (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"];
}

相关文章

网友评论

      本文标题:web的操作

      本文链接:https://www.haomeiwen.com/subject/xcveottx.html