iOS webview与JS的交互;以及修改cookie、header头

一、webview与Js的交互

#import "webView.h"
#import <JavaScriptCore/JavaScriptCore.h>//系统支持库
@protocol JSObjcDelegate <JSExport>//定义webJS交互的协议
-(void)goToNextActivity:(NSString *)urlStr;//JS需要调用的方法,参数可有可无,根据需要设定
@end
@interface webView ()
<UIWebViewDelegate,JSObjcDelegate>//遵守协议
@property (nonatomic,strong)JSContext * jsContext;//创建JSContext对象 我把它当作上下文对象,用于连接JS和OC

@end

@implementation webView

-(instancetype)initWithFrame:(CGRect)frame url:(NSURL *)url{

    self = [super initWithFrame:frame];
    if (self) {
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        self.delegate = self;
        [self loadRequest:request];
    }
    return self;
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{

    self.jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];

    self.jsContext[@"myObj"] = self;//这里把self赋值给self.jsContext的myObj对象,那么在JS中就可以通过myObj调用oc方法了
    self.jsContext.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
        context.exception = exceptionValue;
        NSLog(@"异常信息:%@", exceptionValue);
    };
}

//webview调用方法
-(void)goToNextActivity:(NSString *)urlStr{
    NSLog(@"goToNextActivity str:%@",urlStr);
}
@end

二、cookie

这里是每次请求的时候都进行设定,所以方法写在了web开始加载的方法里面

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

    NSDictionary *dic = @{@"TOKEN":[DEFAULTS objectForKey:@"token"],@"UID":[DEFAULTS objectForKey:@"userID"]};

    [dic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        // 设定 cookie
        NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:[NSDictionary dictionaryWithObjectsAndKeys:                                 [request.URL host], NSHTTPCookieDomain,
[request.URL path], NSHTTPCookiePath,
key,NSHTTPCookieName,
obj,NSHTTPCookieValue,
nil]];
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
//        NSLog(@"cookie = %@",cookie);
    }];
    return YES;
}

三、header

同样是写在了webview开始加载的方法里面

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

    NSMutableURLRequest *mutableRequest = [request mutableCopy];
//这里加了一些判断,避免死循环,因为修改完成后webview需要重新加载request (其中DEFAULTS 是NSUserDefaults存了一些后台程序返回的内容)
    if (!request.allHTTPHeaderFields[@"CITY"]) {

        [mutableRequest addValue:[DEFAULTS objectForKey:@"city"] forHTTPHeaderField:@"CITY"];

        [self loadRequest:request];
    }else if(![[DEFAULTS objectForKey:@"city"] isEqualToString:request.allHTTPHeaderFields[@"CITY"]]) {
        [mutableRequest setValue:[DEFAULTS objectForKey:@"city"] forHTTPHeaderField:@"CITY"];

        request = [mutableRequest copy];
        [self loadRequest:request];
    }
    return YES;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值