2018-08-15关于iOS JSContext 失效问题

这个时代,真正的技术博客太少了,都是到处抄袭,想找答案,找思路,搜索10个文章,9个是一样的,抄就不能加点自己理解吗。

前段时间app中接了网页支付的业务(会跳转到其他支付应用),没太怎么注意JSContext跳转后会失效的问题,后面改版才发现。

网上有几种方案,各有各的说法,但是应用到目前的项目中是不符合的,方案有二:
一,js判断是否拿到注入的类,如果拿不到js重新加载(???跟没说一样)
二,每次跳转出去就移除webView,跳转回来再创建webView(什么鬼,说的啥???移除webView就把webView移除栈区,新创建的页面怎么回到上一层??)

UIWebView Delegate 有4个方法,但是当前场景都用不上,归根结底是注入的时机问题,使用jsContext进行js注入的时候,如果网页跳转到其他页面,注入的js有一定几率会丢失.

添加分类 UIWebView+TS_JavaScriptContext
在js注入的类中实现TS_JavaScriptContext的代理方法

实现代码

.h

#import 
#import 

@protocol TSWebViewDelegate 

@optional

- (void)webView:(UIWebView *)webView didCreateJavaScriptContext:(JSContext*) ctx;

@end


@interface UIWebView (TS_JavaScriptContext)

@property (nonatomic, readonly) JSContext* ts_javaScriptContext;

@end

.m

#import "UIWebView+TS_JavaScriptContext.h"

#import 
#import 

static const char kTSJavaScriptContext[] = "ts_javaScriptContext";

static NSHashTable* g_webViews = nil;

@interface UIWebView (TS_JavaScriptCore_private)
- (void) ts_didCreateJavaScriptContext:(JSContext *)ts_javaScriptContext;
@end

@protocol TSWebFrame 
- (id) parentFrame;
@end

@implementation NSObject (TS_JavaScriptContext)

- (void) webView: (id) unused didCreateJavaScriptContext: (JSContext*) ctx forFrame: (id) frame
{
    NSParameterAssert( [frame respondsToSelector: @selector( parentFrame )] );
    
    // only interested in root-level frames
    if ( [frame respondsToSelector: @selector( parentFrame) ] && [frame parentFrame] != nil )
        return;
    
    void (^notifyDidCreateJavaScriptContext)() = ^{
        
        for ( UIWebView* webView in g_webViews )
        {
            NSString* cookie = [NSString stringWithFormat: @"ts_jscWebView_%lud", (unsigned long)webView.hash ];
            
            [webView stringByEvaluatingJavaScriptFromString: [NSString stringWithFormat: @"var %@ = '%@'", cookie, cookie ] ];
            
            if ( [ctx[cookie].toString isEqualToString: cookie] )
            {
                [webView ts_didCreateJavaScriptContext: ctx];
                return;
            }
        }
    };
    
    if ( [NSThread isMainThread] )
    {
        notifyDidCreateJavaScriptContext();
    }
    else
    {
        dispatch_async( dispatch_get_main_queue(), notifyDidCreateJavaScriptContext );
    }
}

@end


@implementation UIWebView (TS_JavaScriptContext)

+ (id) allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        g_webViews = [NSHashTable weakObjectsHashTable];
    });
    
    NSAssert( [NSThread isMainThread], @"uh oh - why aren't we on the main thread?");

    id webView = [super allocWithZone: zone];

    [g_webViews addObject: webView];
    
    return webView;
}

- (void) ts_didCreateJavaScriptContext:(JSContext *)ts_javaScriptContext
{
    [self willChangeValueForKey: @"ts_javaScriptContext"];
    
    objc_setAssociatedObject( self, kTSJavaScriptContext, ts_javaScriptContext, OBJC_ASSOCIATION_RETAIN);

    [self didChangeValueForKey: @"ts_javaScriptContext"];
    
    if ( [self.delegate respondsToSelector: @selector(webView:didCreateJavaScriptContext:)] )
    {
        id delegate = ( id)self.delegate;
        [delegate webView: self didCreateJavaScriptContext: ts_javaScriptContext];
    }
}

- (JSContext*) ts_javaScriptContext
{
    JSContext* javaScriptContext = objc_getAssociatedObject( self, kTSJavaScriptContext );
    return javaScriptContext;
}

@end

实现

//页面跳转回来JS注入
- (void)webView:(UIWebView *)webView didCreateJavaScriptContext:(JSContext*) ctx {
    ctx[@"JsBridge"] = self;
}

你可能感兴趣的:(2018-08-15关于iOS JSContext 失效问题)