美文网首页
js &cookie

js &cookie

作者: 王家小雷 | 来源:发表于2017-12-14 16:39 被阅读5次

    js-- (void)webViewDidFinishLoad:(UIWebView *)webView
    {
    JSContext *contentJS = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    __weak typeof(self)weakSelf =self;
    contentJS[@"console"][@"log"] = ^(JSValue * msg) {
    JSValue *jsValue= msg;
    NSString *str = [jsValue toString];

        NSLog(@"H5  log : %@", msg);
    };
    contentJS[@"console"][@"warn"] = ^(JSValue * msg) {
        NSLog(@"H5  warn : %@", msg);
    };
    contentJS[@"console"][@"error"] = ^(JSValue * msg) {
        NSLog(@"H5  error : %@", msg);
    };
    contentJS[@"Function"] = ^() {
        
        NSArray *thisArr = [JSContext currentArguments];
        
        for (JSValue *jsValue in thisArr) {
            
            NSLog(@"=======%@",jsValue);
            
        }
        //
        //        //JSValue *this = [JSContext currentThis];
        //
        //        //NSLog(@"this: %@",this);
        //
        NSLog(@"js调用oc---------The End-------");
    };
    
    
    NSLog(@"webViewDidFinishLoad");
    

    }

    pragma mark -- 获取cookie

    • (void)getCookie:(NSURL )url
      {
      NSHTTPCookieStorage
      cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
      NSArray *cachCookies = [cookieJar cookiesForURL:url];

      NSMutableString cookieString = [[NSMutableString alloc] init];
      for (NSHTTPCookie
      cookiew in [cookieJar cookies]) {
      [cookieString appendFormat:@"%@=%@;",cookiew.name,cookiew.value];
      }
      NSString *str=cookieString;
      }

    pragma mark -- 清楚cookie

    • (void)clearCookie:(NSURL *)url
      {
      NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];

      //清除所有cookie
      NSArray *cachCookies = [cookieStorage cookiesForURL:url];
      for (int i = 0; i < [cachCookies count]; i++) {
      NSHTTPCookie *cookie = (NSHTTPCookie *)[cachCookies objectAtIndex:i];
      [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
      }
      }

    相关文章

      网友评论

          本文标题:js &cookie

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