Skip to content

Instantly share code, notes, and snippets.

@MotiurRahman
Last active December 29, 2015 16:59
Show Gist options
  • Save MotiurRahman/7700902 to your computer and use it in GitHub Desktop.
Save MotiurRahman/7700902 to your computer and use it in GitHub Desktop.
Android and IOS: Photo upload in Facebook with CommonJS Module
/* Hi after login the facebook, just click the upload photo button and photo will be
uploaded in facebook
*/
var fb = require('facebook');
fb.appid = APP_ID;
fb.permissions = ['publish_stream'];
fb.authorize();
// Don't forget to set your appid and requested permissions, else the login button
// won't be effective.
var win = Ti.UI.createWindow({
backgroundColor : 'white'
});
fb.addEventListener('login', function(e) {
if (e.success) {
alert('Logged in');
}
});
fb.addEventListener('logout', function(e) {
alert('Logged out');
});
// Add the button. Note that it doesn't need a click event listener.
win.add(fb.createLoginButton({
top : 20,
style : fb.BUTTON_STYLE_WIDE
}));
var f = Ti.Filesystem.getFile('motiur.jpg');
var blob = f.read();
var data = {
message : 'This is Me',
picture : blob
};
// Create a Button.
var upload = Ti.UI.createButton({
title : 'Upload Photo',
height : Ti.UI.SIZE,
width : Ti.UI.SIZE,
top : 100,
});
// Listen for click events.
upload.addEventListener('click', function() {
fb.requestWithGraphPath('me/photos', data, 'POST', function(e) {
if (e.success) {
alert("Success! From FB: " + e.result);
} else {
if (e.error) {
alert(e.error);
} else {
alert("Unkown result");
}
}
});
});
// Add to the parent view.
win.add(upload);
// Now post the photo after you've confirmed that authorize() succeeded
win.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment