Skip to content

Instantly share code, notes, and snippets.

@milon120203
Created March 3, 2018 15:49
Show Gist options
  • Save milon120203/6f6a6240d83353e0ee4f83c8355293b4 to your computer and use it in GitHub Desktop.
Save milon120203/6f6a6240d83353e0ee4f83c8355293b4 to your computer and use it in GitHub Desktop.
Facebook apps code for Appcelerator ti.facebook module in iOS
var fb = require('facebook');
fb.appid=137966330358106;
var win = Titanium.UI.createWindow({
title:'Facebook Test',
backgroundColor: 'white'
});
fb.permissions = ['public_profile', 'email', 'user_friends','user_location', 'user_posts','user_photos'];
fb.authorize();
var button = fb.createLoginButton({
top: '30dp',
width: '100dp',
height: '50dp',
backgroundColor: 'blue',
color: 'white'
});
win.add(button);
button.addEventListener('click', function() {
if (fb.loggedIn) {
fb.logout();
} else {
fb.authorize();
}
});
// this name discrepancy will be fixed in a future revision
if (Ti.Platform.osname == 'android') {
fb = require('facebook');
win.fbProxy = fb.createActivityWorker({lifecycleContainer: win});
} else {
fb = require('facebook');
}
fb.addEventListener('login', 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');
}
});
fb.addEventListener('logout', function() {
button.title = 'Login';
alert('Logout event');
});
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
}
if (fb.loggedIn) {
button.title = 'Logout';
} else {
button.title = 'Login';
}
win.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment