问题
I am using iframe within PhoneGap and PhoneGap Build for building. Is it possible to access PhoneGap functionality e.g. the camera function inside the iframe?
Thanks!
回答1:
History does not work the way it should. history.back() and history.forward() don't work and we have to hack it (with history states) to make it work (sorta). We were able to get around it by setting the usePolling property to true.
Conflicts between the main window and the iframe window for everything (URLs, events, etc)
There were an issue regarding the ability to select text in the iframe content; I believe Anis found a solution / workaround for it.
Can't use XmlHttpRequests to set document data (it messes up with CSS/js etc).
Read on: https://github.com/phonegap/phonegap/wiki/iFrame-Usage
回答2:
Yes, it is very possible.
Your problem is caused by the conflicts between the main window and the iframe window.
Please refer to this link: https://github.com/phonegap/phonegap/wiki/iFrame-Usage
I have successfully implemented this using cordova plugin contact android.
To make it possible, you need to do these:
- Create the communication between parent window and the child/iframe using postMessage. Parent and child listen each other.
- Send/trigger the message from child/iframe to parent to call the cordova plugin.
- Call the cordova plugin from parent window.
- Create the global function in parent window to be called from the cordova code/side (Java in android).
- Find out the java file in cordova that (call the callback function and parse the result back). Clue: callbackContext.success(parameter);. From here, we need to call the global function created in parent window.
- Call the global function created in parent window from the java file of cordova. Here is the snippet of calling js code from java code:
your_web_view.loadUrl("javascript:function_name(parameter_result_from_cordova)");
- Send back the message gotten in parent to the iframe/child using postMessage.
This should work. Let me know if you find any issue.
来源:https://stackoverflow.com/questions/18298301/accessing-phonegap-inside-an-iframe