web的操作

  • 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"];
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米、百度、阿里、京东、新浪、CVTE、乐视家的研发岗...
    时芥蓝阅读 42,573评论 11 349
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,272评论 19 139
  • 点击查看原文 Web SDK 开发手册 SDK 概述 网易云信 SDK 为 Web 应用提供一个完善的 IM 系统...
    layjoy阅读 14,778评论 0 15
  • 今天webryan给team做了一个关于HTTP cookie的分享,从各个方面给大家介绍一下大家耳熟能详的Coo...
    秒赞不是偶然阅读 12,795评论 0 20
  • ——最美省道行 云间的路 是刻在悬崖上的 翠绿的山 尤如系着洁白的哈达 满怀敬仰冲上去 再回看 会留下 深深的震撼...
    第一闲人阅读 1,558评论 0 1

友情链接更多精彩内容