Raising an Exception from Objective-C back to Javascript

爷,独闯天下 提交于 2019-12-11 02:17:12

问题


JavascriptCore permits us to expose our Objective-C classes to javascript via the JSExport protocol. Let's say, in one of the exposed Objective-C class's methods, a condition is encountered where it makes sense to raise an exception. Is there a way to create said exception and raise it such that the javascript code can process it as it would process a regular javascript exception?

I tried doing a regular old [NSException raise...] and that immediately caused a crash.

Am I relegated to detecting the raise condition, passing that condition back to javascript, and having the javascript do the throw?


回答1:


Objective-C JSContext object have exception property, that may be used to throw an exception to JavaScript. According comments in header file:

Before a callback is made from JavaScript to an Objective-C block or method, the prior value of the exception property will be preserved and the property will be set to nil. After the callback has completed the new value of the exception property will be read, and prior value restored. If the new value of exception is not nil, the callback will result in that value being thrown.



来源:https://stackoverflow.com/questions/29979041/raising-an-exception-from-objective-c-back-to-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!