I'm building one Web application and two hybrid Cordova applications (iOS and android) which relies on web application, i.e. Cordova app calls iframe web application and it works well.
Now, I'm stuck on Facebook login API.
FB login API is implemented and works well on Web app, but on e.g. android Cordova app, because it isn't a web app, it doesn't have a domain, hence it doesn't have any page to return to within the "site" and FB API doesn't work.
Correct me if I'm wrong!
I find this plugin: https://github.com/jeduan/cordova-plugin-facebook4 with which we can use Facebook SDK in Cordova projects.
Now, if I implement this plugin, I will need to send fb.id, fb.name and fb.email from Cordova app to my Web app and I will have a cross-domain problem then.
What to do now? Can this be done using FB login API in Web application or by using Cordova plugin and what to do with cross-domain problem?
Finally manage to solve the problem by:
So let's go step-by-step:
1. installing cordova-plugin-inappbrowser:
openFB.api({
path: '/me',
params: { fields: "name, email" },
success: function(data) {
fb_data.id = data.id; // used to save my fb id
fb_data.name = data.name; // used to save my fb name
fb_data.email = data.email; // used to save my fb email
// select iframe content window and post cross-domain message with fb_data
$("iframe")[0].contentWindow.postMessage(fb_data, "*");
},
error: errorHandler
});
receiving cross-domain message in iframe (child) from cordova app (parent):
window.addEventListener("message", receiveMessage, false);
function receiveMessage(event) {
var origin = event.origin || event.originalEvent.origin; // For Chrome, the origin property is in the event.originalEvent object.
switch (event.data) {
case "cordova-not-loaded":
cordova_is_loaded = "no";
break;
case "cordova-is-loaded":
cordova_is_loaded = "yes";
break;
default:
if (origin == "file://") {
fb_data = event.data; // get facebook login data
}
}
}
Check wheather cordova is loaded in iframe app (child)
function handleCordovaLoaded() {
window.parent.postMessage("is-cordova-loaded", "*");
var interval = setInterval(function() {
if(typeof cordova_is_loaded != "undefined") {
clearInterval(interval);
// hide all parts of javascript social plugin if cordova is loaded
if(cordova_is_loaded == "yes") {
$(".all-your-facebook-elements-in-app").hide(); // hide all your javascript fb api login buttons, statuses etc.
}
}
}, 100);
}
receiving cross-domain message in cordova app (parent) from iframe (child)
window.addEventListener("message", receiveMessage, false);
function receiveMessage(event)
{
var origin = event.origin || event.originalEvent.origin; // For Chrome, the origin property is in the event.originalEvent object.
switch(event.data) {
case "is-cordova-loaded":
var cordova_loaded = typeof cordova == 'undefined' ? "cordova-not-loaded" : "cordova-is-loaded";
$("iframe")[0].contentWindow.postMessage(cordova_loaded, "*"); // get iframe content window and send cross-domain message with cordova_loaded status
break;
}
}
I hope this will help someone with the same problems.
I will be glad to help, so please ask if anything is unclear.
:: cheers ::
Josip