This questions focuses on IOS mobile and I'm using Ti SDK 3.5.1 (latest GA) and the iOS emulator (8.1)
After getting a copy of the sample app https://github.com/mokesmokes/facebook-titanium-sample and getting the 2 modules (iOS module Android module) I was able to get it working pointing to my Facebook App.
Then, I wanted to try a slightly more complex example since I will need to use this module from different parts of the app on different windows for different reasons. My app.js looks like this:
(function() { var mainWin = Titanium.UI.createWindow({ backgroundColor: 'red', title: 'Main' }); var navWin = Titanium.UI.iOS.createNavigationWindow({ window: mainWin }); var button = Titanium.UI.createButton({ title: 'Open Child Window', color:'blue', top: '30dp', width: '200dp', height: '50dp', backgroundColor: 'white' }); button.addEventListener('click', function(){ var ChildWin = require('/child/win'); navWin.openWindow(new ChildWin(), {animated:true}); }); mainWin.add(button); navWin.open(); })();And the child window code sitting in Resources/child/win.js looks like this:
function ApplicationWindow() { var self = Titanium.UI.createWindow({ title:'Facebook Test', backgroundColor: 'white' }); self.addEventListener('close',function(){ //Remove fb listeners fb.removeEventListener('login',fbLogin); fb.removeEventListener('logout',fbLogout); }); var fb; // this name discrepancy will be fixed in a future revision if (Ti.Platform.osname == 'android') { fb = require('com.ti.facebook'); self.fbProxy = fb.createActivityWorker({lifecycleContainer: self}); } else { fb = require('com.facebook'); } fb.permissions = ['public_profile', 'email', 'user_friends']; var fbLogin = function(e){ if (e.success) { button.title = 'Logout'; alert('login success: ' + JSON.stringify(e.data)); // Note: the user may decline email and friends.... Ti.API.info('Actual permissions: ' + JSON.stringify(fb.permissions)); } else if (e.cancelled) { button.title = 'Login'; alert ('login cancelled'); } else if (e.error) { button.title = 'Login'; var alertMessage; if (Ti.Platform.osname != 'android') { if (e.error.indexOf('OTHER:') !== 0){ alertMessage = e.error; } else { //alert('Please check your network connection and try again.'); alertMessage = 'Please check your network connection and try again'; } } else { alertMessage = e.error; } alert(alertMessage); } else { alert('Please check your network connection and try again'); } }; var fbLogout = function(){ button.title = 'Login'; alert('Logout event'); var fburl = 'https://login.facebook.com'; var fbclient = Titanium.Network.createHTTPClient(); fbclient.clearCookies(fburl); }; fb.addEventListener('login', fbLogin); fb.addEventListener('logout', fbLogout); if (Ti.Platform.osname == 'android'){ fb.initialize(4000); } else { fb.initialize(6000, false); // pass true for system login, less reliable than app login but much faster especially on iPhone 4/4S/5 } var button = Ti.UI.createButton({ top: '30dp', width: '100dp', height: '50dp', backgroundColor: 'blue', color: 'white' }); self.add(button); button.addEventListener('click', function() { if (fb.loggedIn) { fb.logout(); } else { fb.authorize(); } }); if (fb.loggedIn) { button.title = 'Logout'; } else { button.title = 'Login'; } return self; } module.exports = ApplicationWindow;The code here is essentially the same as the sample app except I have moved it to a child window of a navigation window. I have added listener removal as a necessaity on close of the window. I also added the clearance of cookies to logout just to make sure.
I am getting behaviour that I am not expecting though. If you go to the second page from the main page and alternatively click Login and Logout - it appears to work like the original sample app. Once you click back though, something seems to go astray. If after you have logged out, you click back, and then move between the pages over and over, you suddenly get "logged in" again even though you are not actually logged in. You will also receive a logout message just after opening the window. I regularly see the following lines in the console as well:
.... 2015-03-24 20:33:13.562 TestApp[32060:205751] FBSDKLog: An existing state change handler was assigned to the session and will be overwritten. .... 2015-03-24 20:33:13.606 TestApp[32060:205751] FBSDKLog: errorBehavior should be set before requests were added. Prior requests will not use the supplied behavior. ....So, this is a shout out to those that have used this module in a complex setting to see if you can please tell me where I am going wrong - or is there a trick that I don't know about?
If you require further info - I am happy to provide it.