【发布时间】:2018-02-12 07:55:10
【问题描述】:
我正在从 ionic-1 开发一个应用程序。我还实现了 facebook 登录插件。我关注了this steps。我没有错误,但是当我单击 facebook 按钮时,它不起作用,没有显示错误。我在每个错误捕获中都使用alert,但不会显示任何警报。
注意:我在真正的 android 设备上运行,因为他们说本机插件无法在浏览器中运行。
请帮助我,因为我正在处理我的论文。谢谢大家!
这是我的代码:
.controller('CreateAccountCtrl', ['$scope','$http','$state','$q','UserService','$stateParams', '$ionicLoading','$timeout','$ionicPopup',function($scope, $http,$state,$q, UserService,$stateParams,$ionicLoading,$timeout,$ionicPopup){
$scope.$on('$ionicView.beforeEnter', function(e){
$ionicLoading.show({
animation: 'fade-in', showBackdrop: true
});
});
$scope.$on('$ionicView.afterEnter', function(e){
$ionicLoading.hide();
$scope.formData = {};
$scope.showAlert = function(ttl,msg){
var alertPopup = $ionicPopup.alert({
title: ttl,
template: msg
});
};
});
// facebook
// This is the success callbak from the login method
var fbLoginSuccess = function(response){
if(!response.authResponse){
fbLoginError("Cannot find the authResponse");
return;
}
var authResponse = response.authResponse;
getFacebookProfileInfo(authResponse)
.then(function(profileInfo){
// store user data on local storage
UserService.setUser({
authResponse: authResponse,
userID: profileInfo.id,
name: profileInfo.name,
email: profileInfo.email,
picture: "http://graph.facebook.com/" + authResponse.userID + "/picture?type=large"
});
$ionicLoading.hide();
$state.go('app.home');
}, function(fail){
alert(fail);
// Fail get profile info
console.log('Profile Info Fail', fail);
});
};
// This is the fail callback from the login method
var fbLoginError = function(error){
console.log('fbLoginError', error);
alert(error);
$ionicLoading.hide();
};
// This method is to get the user profile info from the facebook api
var getFacebookProfileInfo = function(authResponse){
var info = $q.defer();
facebookConnectPlugin.api('/me?fields=email,name&access_token=' + authResponse.accessToken, null,
function(response){
alert(response);
console.log(response);
info.resolve(response);
},
function(response){
console.log(response);
alert(response);
info.reject(response);
}
);
return info.promise;
};
// This method is executed when the user press the "Login with faceboook" button
$scope.facebookSignIn = function(){
alert(1);
facebookConnectPlugin.getLoginStatus(function(success){
if(success.status === 'connected'){
// The user is logged in and has authenticated your app, and response.authResponse supplies
// the user's ID, a valid access token, a signed request, and the time the access token
// and signed request each expire
console.log('getLoginStatus', success.status);
alert(success.status);
// Check if we have our user saved
var user = UserService.getUser('facebook');
if(!user.userID){
getFacebookProfileInfo(success.authResponse)
.then(function(profileInfo){
UserService.setUser({
authResponse: success.authResponse,
userID: profileInfo.id,
name: profileInfo.name,
email: profileInfo.email,
picture : "http://graph.facebook.com/" + success.authResponse.userID + "/picture?type=large"
});
$state.go('app.home');
}, function(fail){
console.log('Profile info fail', fail);
alert(fail);
});
}else{
$state.go('app.home');
}
}else{
// If (success.status === 'not_authorized') the user is logged in to Facebook,
// but has not authenticated your app
// Else the person is not logged into Facebook,
// so we're not sure if they are logged into this app or not.
console.log('getLoginStatus', success.status);
alert(success.status);
$ionicLoading.show({
template: 'Logging in...'
});
facebookConnectPlugin.login(['email','public_profile'], fbLoginSuccess, fbLoginError);
}
});
};
}]);
在我的 html 中
<a class="facebook-sign-in button button-block button-facebook" ng-click="facebookSignIn()">
<i class="icon ion-social-facebook"></i>
Sign in using facebook
</a>
【问题讨论】:
-
所以你是说它永远不会在 $scope.facebookSignIn() 中触发 alert(1)?
-
@billy_comic 它触发警报(1)先生。当我单击按钮时,就是这样。它只是提醒:(请帮助我
-
好的。也许有几件事。我看不到您将 Facebook 插件注入控制器顶部的控制器的位置(注入 $ionicPopup 等的位置)。您是否将 src="" 包含在索引中的插件中?
-
没有。我没有包括来源。我认为它会被自动调用。
-
我会在那里放置一个断点,然后检查插件引用以确保它不会返回未定义。
标签: android angularjs ionic-framework ionic-native phonegap-facebook-plugin