{"id":149,"date":"2016-03-17T17:46:19","date_gmt":"2016-03-17T09:46:19","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=149"},"modified":"2016-08-16T11:09:48","modified_gmt":"2016-08-16T03:09:48","slug":"oc%e4%b8%adjavascriptcore%e4%bd%bf%e7%94%a8%e7%9a%84%e4%b8%a4%e4%b8%aa%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/project\/2016\/03\/17\/174619149.html","title":{"rendered":"OC\u4e2dJavaScriptCore\u4f7f\u7528\u7684\u4e24\u4e2a\u95ee\u9898"},"content":{"rendered":"<p>1\u3001iOS7\u53ca\u4ee5\u540e\u7684\u7248\u672c\u4e2d\u63d0\u4f9b\u4e86JavaScriptSore\u6846\u67b6\uff0c\u6781\u5927\u7684\u65b9\u4fbf\u4e86OC\u4e0eJS\u7684\u4ea4\u4e92<br \/>\n2\u3001\u57fa\u672c\u4f7f\u7528\u53c2\u8003\uff1a <a href=\"http:\/\/www.yusian.com\/blog\/project\/2016\/03\/17\/170631.html\" target=\"_blank\">Object-C\u4e0eJavaScript\u4ea4\u4e92\u7684\u4e24\u79cd\u65b9\u5f0f<\/a><br \/>\n3\u3001\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u5b58\u5728\u4e00\u4e9b\u95ee\u9898\uff0c\u8fd9\u91cc\u62ff\u4e24\u4e2a\u6700\u4e3a\u5e38\u89c1\u7684\u95ee\u9898\u8fdb\u884c\u5206\u6790<br \/>\n4.1\u3001\u5982\u679c\u6ca1\u6709JS\u8c03\u7528\u65f6\u6ca1\u6709\u8be5\u65b9\u6cd5\uff0c\u5982\u679c\u6355\u83b7\u5f02\u5e38<br \/>\nA\u3001JS\u8c03\u7528OC\u65b9\u6cd5\u65f6\uff0c\u5982\u679c\u4e0d\u5b58\u5728\u8be5\u65b9\u6cd5\u4e0d\u4f1a\u9020\u6210\u7a0b\u5e8f\u5d29\u6e83\uff0c\u53ea\u662f\u6ca1\u4efb\u4f55\u53cd\u5e94\u800c\u5df2\uff0c\u54ea\u91cc\u53ef\u4ee5\u770b\u51fa\u6765\u5462\uff1f<br \/>\nB\u3001\u5176\u5b9eJSContext\u5df2\u7ecf\u63d0\u4f9b\u4e86\u4e00\u4e2a\u65b9\u6cd5\uff0c\u53ea\u662f\u5e73\u65f6\u6ca1\u6709\u7528\uff0cJSContext\u6709\u6761\u5c5e\u6027<\/p>\n<pre lang=\"objc\" line=\"1\">\/*!\n@property\n@discussion If a call to an API function results in an uncaught JavaScript exception, the\n <code>exceptionHandler<\/code> block will be invoked. The default implementation for the\n exception handler will store the exception to the exception property on\n context. As a consequence the default behaviour is for unhandled exceptions\n occurring within a callback from JavaScript to be rethrown upon return.\n Setting this value to nil will result in all uncaught exceptions thrown from\n the API being silently consumed.\n*\/\n@property (copy) void(^exceptionHandler)(JSContext *context, JSValue *exception);<\/pre>\n<p><!--more--><br \/>\nC\u3001\u8fd9\u6761\u5c5e\u6027\u5c31\u662f\u7528\u6765\u6355\u83b7\u5f02\u5e38\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7set\u65b9\u6cd5\u6765\u6355\u83b7\u5f02\u5e38<\/p>\n<pre lang=\"objc\" line=\"1\">- (void)webViewDidFinishLoad:(UIWebView *)webView\n{\n    [SAApp setNetworkActivityIndicatorVisible:NO];\n    [self.JSContext evaluateScript:@\"webView.check()\"];\n}\n- (JSContext *)JSContext\n{\n    if (self-&gt;_JSContext == nil){\n        self-&gt;_JSContext = [self.webView valueForKeyPath:@\"documentView.webView.mainFrame.javaScriptContext\"];\n        [self-&gt;_JSContext setObject:self.javaScript forKeyedSubscript:@\"webView\"];\n        [self-&gt;_JSContext setExceptionHandler:^(JSContext *context, JSValue *value) {\n            context.exception = value;\n            SALog(@\"JS\u9519\u8bef\u4fe1\u606f****\\n%@****\", value);\n        }];\n    }\n    return self-&gt;_JSContext;\n}<\/pre>\n<p>D\u3001\u5982\u679c\u8c03\u7528\u4e86\u4e0d\u5b58\u5728\u7684check()\u65b9\u6cd5\u5219\u8f93\u51fa\u4ee5\u4e0b\u5f02\u5e38\u4fe1\u606f\uff1a<\/p>\n<pre lang=\"objc\" line=\"1\">2016-03-17 17:39:00.755 fisher[8500:262650] __26-[SAWebViewCtrl JSContext]_block_invoke-155\u884c:JS\u9519\u8bef\u4fe1\u606f****\nTypeError: webView.check is not a function. (In 'webView.check()', 'webView.check' is undefined)****<\/pre>\n<p>4.2\u3001UIWebView\u52a0\u8f7d\u7684\u7b2c\u4e00\u4e2a\u9875\u9762JS\u8c03\u7528\u672c\u5730\u65b9\u6cd5\u6ca1\u6709\u95ee\u9898\uff0c\u4f46\u5982\u679c\u8df3\u8f6c\u5230\u4e86\u4e00\u4e2a\u65b0\u7684\u9875\u9762\u540e\uff0cJS\u8c03\u7528\u672c\u5730\u65b9\u6cd5\u5931\u6548\uff1f<\/p>\n<p>A\u3001\u5982\u4e0a\u4f8b\u6240\u793a\uff0c\u5982\u679c\u6211\u4eec\u5982\u679c\u8fdb\u5165\u4e86\u4e0b\u4e00\u4e2a\u9875\u9762\uff0c\u518d\u8c03\u7528\u672c\u5730\u65b9\u6cd5\u5c31\u4f1a\u6709\u5982\u4e0b\u9519\u8bef\u4fe1\u606f<\/p>\n<pre lang=\"objc\" line=\"1\">2016-03-17 17:44:31.465 fisher[8537:267499] __26-[SAWebViewCtrl JSContext]_block_invoke-152\u884c:JS\u9519\u8bef\u4fe1\u606f****\nReferenceError: Can't find variable: webView****<\/pre>\n<p>B\u3001\u4ece\u4ee5\u4e0a\u4ee3\u7801\u6765\u770b\uff0cJSContext\u662f\u4e00\u79cd\u61d2\u52a0\u8f7d\u6a21\u5f0f\uff0c\u5982\u679c\u4f7f\u7528\u4e86JSContext\u5219\u4f1a\u521d\u59cb\u5316\u5e76\u5c06\u5176\u4ee5&#8221;webView&#8221;\u8fd9\u4e2a\u63cf\u8ff0\u6ce8\u5165\u5230\u5168\u5c40JS\u4e2d\u53bb\uff0c\u6240\u4ee5\u52a0\u8f7d\u7684\u7b2c\u4e00\u4e2a\u9875\u9762\u662f\u597d\u4f7f\u7684\uff0c\u53ef\u9875\u9762\u8df3\u8f6c\u4e86\u4e3a\u4ec0\u4e48\u5c31\u4e0d\u884c\u4e86\uff1f\uff01<br \/>\nC\u3001\u6211\u4eec\u60f3\u60f3\u7f51\u9875\u7684\u52a0\u8f7d\u65b9\u5f0f\uff0c\u6709JS\u57fa\u7840\u7684\u7ae5\u978b\u5e94\u8be5\u77e5\u9053\uff0cJS\u662f\u53ef\u4ee5\u76f4\u63a5\u5199\u5728\u4e2d\u4ee5<script><\/script>\u6807\u7b7e\u5b58\u5728\u7684\uff0c\u6216\u8005\u901a\u8fc7link\u8fdb\u6765\u7684\uff1b<br \/>\nD\u3001\u6211\u4eec\u7684\u6ce8\u5165\u53ef\u4ee5\u7406\u89e3\u6210\u662f\u5728\u9875\u9762\u52a0\u8f7d\u5b8c\u6bd5\u540e\u4e34\u65f6\u5199\u5230\u8fd9\u4e2a\u6807\u7b7e\u4e2d\u53bb\u7684\uff0c\u90a3\u4e48\u9875\u9762\u5237\u65b0\u4e86\u6216\u8df3\u8f6c\u5230\u65b0\u9875\u9762\u4e86\u4f1a\u600e\u6837\uff1f<br \/>\nE\u3001\u7b54\u6848\u662f\u5f53\u524d\u7f51\u9875\u6e90\u4ee3\u7801\u4e2d\u4e4b\u524d\u7684JS\u5df2\u7ecf\u6ca1\u6709\u4e86\uff0c\u6211\u4eec\u6ce8\u5165\u7684\u90a3\u6bb5JS\u4ee3\u7801\u88ab\u5237\u65b0\u6389\u4e86\uff0c\u6240\u4ee5\u8981\u91cd\u65b0\u6ce8\u5165\uff01<br \/>\nF\u3001\u4e0d\u9700\u8981\u91cd\u65b0\u521b\u5efaJSContext\u5bf9\u8c61\uff0c\u53ea\u9700\u8981\u6267\u884c\u6ce8\u5165\u5373\u53ef\uff0c\u6700\u597d\u662f\u5c06\u6ce8\u5165\u65b9\u6cd5\u5199\u5728\u4ee3\u7406\u65b9\u6cd5\u4e2d<\/p>\n<pre lang=\"objc\" line=\"1\">- (void)webViewDidFinishLoad:(UIWebView *)webView\n{\n    [self.JSContext setObject:self.javaScript forKeyedSubscript:@\"webView\"];\n}<\/pre>\n<p>G\u3001\u5927\u529f\u9020\u6210\uff0c\u4e0d\u4fe1\u8bd5\u8bd5\u770b<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001iOS7\u53ca\u4ee5\u540e\u7684\u7248\u672c\u4e2d\u63d0\u4f9b\u4e86JavaScriptSore\u6846\u67b6\uff0c\u6781\u5927\u7684\u65b9\u4fbf\u4e86OC\u4e0eJS\u7684\u4ea4\u4e92 2\u3001\u57fa\u672c\u4f7f\u7528\u53c2\u8003\uff1a Object-C\u4e0eJavaScript\u4ea4\u4e92\u7684\u4e24\u79cd\u65b9\u5f0f 3\u3001\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u5b58\u5728\u4e00\u4e9b\u95ee\u9898\uff0c\u8fd9\u91cc\u62ff\u4e24\u4e2a\u6700\u4e3a\u5e38\u89c1\u7684\u95ee\u9898\u8fdb\u884c\u5206\u6790 4.1\u3001\u5982\u679c\u6ca1\u6709JS\u8c03\u7528\u65f6\u6ca1\u6709\u8be5\u65b9\u6cd5\uff0c\u5982\u679c\u6355\u83b7\u5f02\u5e38 A\u3001JS\u8c03\u7528OC\u65b9\u6cd5\u65f6\uff0c\u5982\u679c\u4e0d\u5b58\u5728\u8be5\u65b9\u6cd5\u4e0d\u4f1a\u9020\u6210\u7a0b\u5e8f\u5d29\u6e83\uff0c\u53ea\u662f\u6ca1\u4efb\u4f55\u53cd\u5e94\u800c\u5df2\uff0c\u54ea\u91cc\u53ef\u4ee5\u770b\u51fa\u6765\u5462\uff1f B\u3001\u5176\u5b9eJSContext\u5df2\u7ecf\u63d0\u4f9b\u4e86\u4e00\u4e2a\u65b9\u6cd5\uff0c\u53ea\u662f\u5e73\u65f6\u6ca1\u6709\u7528\uff0cJSContext\u6709\u6761\u5c5e\u6027 \/*! @property @discussion If a call to an API function results in an uncaught JavaScript exception, the exceptionHandler block will be invoked. The default implementation for the exception handler will store the exception to the exception property on context. As a consequence the default behaviour is for unhandled [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[20],"class_list":["post-149","post","type-post","status-publish","format-standard","hentry","category-project","tag-basics"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/149","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=149"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/149\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}