JSPatch的一些使用注意点

JSPatch代码量不大,满满的都是神机妙算,到处都是黑科技。

主要的坑 – UIWebView相关

如果在JSPatch的代码使用前,app没有初始化过UIWebView,那会造成UIWebView中JS不能解析的问题,打开网页根本不能看。解决方案:

JSPatch使用前先alloc一个UIWebView,然后让ARC自动dealloc它就好了:

UIWebView *webView = [[UIWebView alloc]init];   //Useless. Just to load the UIWebview framework.
webView.frame = CGRectZero;

[JPEngine startEngine];

[self startCache:^(NSString * script) {
    [JPEngine evaluateScript:script];
}]; 

哪怕初始化过UIWebView,也不能在使用JSPatch代码的时候进行UIWebView的初始化(既不能相同runloop初始化UIWebView),否则会造成WebView不稳定,极容易crash:

JavaScriptCore    WTF::equal(WTF::StringImpl const*, unsigned char const*) + 116
JavaScriptCore    std::__1::pair<std::__1::pair<WTF::StringImpl**, bool>, unsigned int> WTF::HashTable<WTF::StringImpl*, WTF::StringImpl*, WTF::IdentityExtractor, WTF::StringHash, WTF::HashTraits<WTF::StringImpl*>, WTF::HashTraits<WTF::StringImpl*> >::fullLookupForWriting<WTF::HashSetTranslatorAdapter<WTF::CStringTranslator>, unsigned char const*>(unsigned char const* const&) + 268
JavaScriptCore    WTF::HashTableAddResult<WTF::HashTableIterator<WTF::StringImpl*, WTF::StringImpl*, WTF::IdentityExtractor, WTF::StringHash, WTF::HashTraits<WTF::StringImpl*>, WTF::HashTraits<WTF::StringImpl*> > > WTF::HashTable<WTF::StringImpl*, WTF::StringImpl*, WTF::IdentityExtractor, WTF::StringHash, WTF::HashTraits<WTF::StringImpl*>, WTF::HashTraits<WTF::StringImpl*> >::addPassingHashCode<WTF::HashSetTranslatorAdapter<WTF::CStringTranslator>, unsigned char const* const&, unsigned char const* const&>(unsigned char const* const&&&, unsigned char const* const&&&) + 200
JavaScriptCore    WTF::AtomicStringImpl::add(unsigned char const*) + 160
WebCore    WebCore::Settings::initializeDefaultFontFamilies() + 720
WebCore    WebCore::Settings::Settings(WebCore::Page*) + 484
WebCore    WebCore::Settings::create(WebCore::Page*) + 36
WebCore    WebCore::Page::Page(WebCore::PageConfiguration&&) + 184
WebKitLegacy    -[WebView(WebPrivate) _commonInitializationWithFrameName:groupName:] + 1712
WebKitLegacy    -[WebView(WebPrivate) _initWithFrame:frameName:groupName:] + 132
UIKit    -[UIWebDocumentView initWithWebView:frame:] + 332
UIKit    -[UIWebBrowserView initWithWebView:frame:] + 48
UIKit    -[UIWebBrowserView initWithFrame:] + 44
UIKit    -[UIWebView _webViewCommonInitWithWebView:scalesPageToFit:] + 244
UIKit    -[UIWebView initWithFrame:] + 76