PhoneGap.exec() 在 JS 和 Obj-C 之间传递对象
我发现在 JS 和 Obj-C 之间传递对象的唯一方法是使用 JSON.stringify() 编码 JS 对象并将 json 字符串传递给 PhoneGap.exec
PhoneGap.exec('Alarm.update',JSON.stringify(list));
对象:
NSString *jsonStr = [arguments objectAtIndex:0];
jsonStr = [jsonStr stringByReplacingOccurrencesOfString:@"\\\"" withString:@"\""];
jsonStr = [NSString stringWithFormat:@"[%@]",jsonStr];
NSObject *arg = [jsonStr JSONValue];
...并在 Obj-C 中重建 那正确吗?有更好/正确/官方的方法来做到这一点吗?
The only way I found to passing objects between the JS and Obj-C it's by encoding the JS object by using JSON.stringify() and pass the json string to PhoneGap.exec
PhoneGap.exec('Alarm.update',JSON.stringify(list));
... and rebuild the object in Obj-C:
NSString *jsonStr = [arguments objectAtIndex:0];
jsonStr = [jsonStr stringByReplacingOccurrencesOfString:@"\\\"" withString:@"\""];
jsonStr = [NSString stringWithFormat:@"[%@]",jsonStr];
NSObject *arg = [jsonStr JSONValue];
It's that correct ? there is a better/proper/official way for doing this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PhoneGap.exec 是为简单类型设计的。您的方法没问题,或者您可以只传递单个对象(仅适用于单个对象,请参阅页脚了解我们如何编组命令),并且它应该位于命令的选项字典中。然后在 Objective-C 端,使用键值编码自动用字典填充您的自定义对象。
例如
MyCustomObject* blah = [MyCustomObject new];
[blah setValuesForKeysWithDictionary:options];
如果您对 PhoneGap.exec 的工作原理感兴趣,请继续阅读...
* --------- *
对于 PhoneGap.exec,JavaScript 参数被编组到 URL 中。
对于 JS 命令:
PhoneGap.exec('MyPlugin.command', 'foo', 'bar', 'baz', { mykey1: 'myvalue1', mykey2: 'myvalue2' });
生成的命令 URL 为:
gap://MyPlugin.myCommand/foo/bar/baz/?mykey1=myvalue1&mykey2=myvalue2
这将在 Objective-C 端处理和转换。 foo、bar、baz 放在arguments 数组中,查询参数放在options 字典中。它将查找名为“MyPlugin”的类,并使用参数数组和选项字典作为参数来调用选择器“myCommand”。
有关更多信息,请参阅phonegap.js,查看PhoneGap.run_command
PhoneGap.exec was designed for simple types. Your way is ok, alternately you can just pass your single object in (would only work for a single object only, see footer about how we marshal the command), and it should be in the options dictionary for the command. Then on the Objective-C side, use key-value coding to automatically populate your custom object with the dictionary.
e.g.
MyCustomObject* blah = [MyCustomObject new];
[blah setValuesForKeysWithDictionary:options];
If you are interested in how PhoneGap.exec works, read on...
* --------- *
For PhoneGap.exec, the javascript arguments are marshaled into a URL.
For the JS command:
PhoneGap.exec('MyPlugin.command', 'foo', 'bar', 'baz', { mykey1: 'myvalue1', mykey2: 'myvalue2' });
The resulting command url is:
gap://MyPlugin.myCommand/foo/bar/baz/?mykey1=myvalue1&mykey2=myvalue2
This will be handled and converted on the Objective-C side. foo, bar, baz are put in the arguments array, and the query parameters are put in the options dictionary. It will look for a class called 'MyPlugin' and will call the selector 'myCommand' with the arguments array and options dictionary as parameters.
For further info, see phonegap.js, look at PhoneGap.run_command
我认为这是最好的方法,即使不是唯一的方法。
PhoneGap.exec 调用只是在幕后获取对象的 NSDictionary,所以我没有看到更好的方法来处理它。
大多数方法的结构如下
I think this is the best way to do it, if not the only way.
The PhoneGap.exec call just takes a NSDictionary of objects under the covers so I don't see of a better way to handle it.
most methods are structured like