Petar Vasilev Petar Vasilev - 4 months ago 85
iOS Question

postMessage in PhoneGap not working - iframe to parent messaging

I've build a PhoneGap app which which makes use of an iframe which is bundled with the app and I'm am trying to pass e message from the iframe to the parent which doesn't seem to be working when I run the app on an actual iPad; however it works fine when I run the app in the browser on the same device.

Here is the code I'm using inside the iframe to send a message, note that I'm using HammerJS to capture some events:

var domain = 'http://' + document.domain;

$('body').hammer().on("swipe", "", function(event) {
var message = event.gesture.direction;
parent.postMessage(message,domain); //send the message and target URI
});


and the code I'm using to get the message:

window.addEventListener('message',function(event) {
alert(event.data);
},false);

Answer

And the answer is to use "file://" as the domain name so the code will look like this:

var domain = 'file://';

$('body').hammer().on("swipe", "", function(event) {
    var message = event.gesture.direction;
    parent.postMessage(message,domain); //send the message and target URI
});