JavascriptCore 允许我们通过 JSExport 协议(protocol)将我们的 Objective-C 类公开给 javascript。比方说,在一个公开的 Objective-C 类的方法中,遇到了一个可以引发异常的条件。有没有办法创建所述异常并引发它,以便 javascript 代码可以像处理常规 javascript 异常一样处理它?
我尝试做一个常规的旧 [NSException raise...] 并立即导致崩溃。
我是否被降级为检测引发条件,将该条件传递回 javascript,并让 javascript 执行抛出?
Best Answer-推荐答案 strong>
Objective-C JSContext 对象具有异常属性,可用于向 JavaScript 抛出异常。根据头文件中的注释:
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.
关于ios - 从 Objective-C 引发异常回到 Javascript,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/29979041/
|