In my Ionic project, I'm having a randomly occuring problem when trying to get a user's facebook friends. This is the code in my controller:
$ionicPlatform.ready(function () {
console.log('SPLASHCTRL: FB: ionic is ready');
console.log('SPLASHCTRL:FB: starting getLoginStatus()');
$cordovaFacebook.getLoginStatus()
.then(function(success) {
//checking if succes
if(success.status === "connected"){
console.log("SplashCtrl:FB: User is FB Authenticated; staying at splash");
$cordovaFacebook.api("me/friends", ["user_friends"])
.then(
function(success) {
console.log("SPLASHCTRL: FB: GOT FBAPP FRIENDS SUCCESFULLY: " + JSON.stringify(success));
$scope.fbAppFriends = success.data;
console.log('SPLASHCTRL: FB: SETTING LOADING TO FALSE');
$scope.loadingFbFriends = false;
// success
},
function (error) {
console.log('SPLASHCTRL: FB: ERROR GETTING FRIENDS' + JSON.stringify(error));
}
);
} else{
console.log('SPLASHCTRL: FB: User response is not connected');
$state.go('intro');
}
},
function (error) {
console.log('SPLASHCTRL: FB: Error getting login status: ' + JSON.stringify(error));
});
});
when runnning this, I'm getting the log saying
"SplashCtrl:FB: User is FB Authenticated; staying at splash"
Indicating everything's fine, but after that half of the time, nothing happens, making it look like $cordovaFacebook.api
isn't called.
As it turns out, everytime the function isn't called, I have an error saying:
FB.getLoginStatus() called before FB.init()
So the strange thing here is that this error comes up from time to time, without the code being different.
Anyone else experiencing a similar problem?
You should be using ngCordova's Facebook plugin
ngCordova is a library for Ionic that acts as an interface between ionic and the standard cordova plugins.
From the documentation:
module.controller('MyCtrl', function($scope, $cordovaFacebook) {
$cordovaFacebook.login(["public_profile", "email", "user_friends"])
.then(function(success) {
// success
}, function (error) {
// error
}
);
$cordovaFacebook.getLoginStatus()
.then(function(success) {
// success
}, function (error) {
// error
}
);
});