我怎么能在我的cordova应用程序中看到facebook页面?

时间:2014-05-28 13:53:54

标签: javascript ios facebook cordova facebook-like

实际上我需要在我的cordova ios应用程序中实现FB。所以我在我的cordova应用程序中添加了Cordova FB connect插件。我使用了以下代码。

<div class="fb-like" data-href="https://www.facebook.com/testentertainer" data-layout="button" data-action="like" data-show-faces="false" data-share="false"></div>

它正在创建一个类似的按钮,它很好。当我第一次点击按钮时,弹出fb登录页面。输入凭据后,请求不会返回到我的页面。它显示白色空白页面和应用程序挂在那里自己。如果我重新打开我的应用程序,那么我可以喜欢,因为我已经登录。现在喜欢和不同的功能正在工作,但我无法得到这些事件的回调。

我试过以下

FB.Event.subscribe('edge.create', function(href, widget) {
       console.log("in edge create");
       alert('You just liked the page!');
});
FB.Event.subscribe('edge.remove', function(href, widget) {
       alert('You just unliked the page!');
});

但它没有用。

FIDDLE

2 个答案:

答案 0 :(得分:0)

Facebook登录页面是弹出窗口还是打开Facebook应用程序?如果是弹出窗口,则必须先安装InAppBrowser插件。

答案 1 :(得分:0)

同样的情况发生在我身上并且非常烦人,看起来fb片段并不适合使用inappbroswer,但是现在我没有浪费任何时间我利用图形api,因为我正在制作一个angular(和typescript)应用程序我已经为Facebook做了一个指令,并从那里处理事件

 <facebook-like url="{{vm.currentUrl}}"></facebook-like>

指令模板

 <div ng-switch="vm.likeStatus">
<div ng-switch-when="like" ng-click="vm.setLike(true)">

    <span class="fbLike"><img src="content/images/like.png" class="img-responsive"/></span>
</div>
<div ng-switch-when="unlike" >
    <span ng-click="vm.setLike(false)">

        <span class="fbLiked"><img src="content/images/liked.png" class="img-responsive" /></span>
    </span>

</div>
<div ng-switch-when="loading"><img src="content/images/loader.gif" class="img-responsive" /></div>
<div ng-switch-when="login" ng-click="vm.login()">login</div>
<div ng-switch-default></div>
</div>

指令定义

 namespace portal.directives {

export class facebookLike {

    constructor() {
        let directive: ng.IDirective = <ng.IDirective>{};
        directive.restrict = 'E';
        directive.scope = {
            url:"@"
        };
        directive.templateUrl = "views/directives/facebookLike.html";
        directive.controller = 'facebookLikeController';
        return directive;

      }
   }
}

指令控制器

   namespace portal.controllers {

export class facebookLikeController {

    public likeStatus: string;
    public postId: string;
    public setLike = (value: boolean) => {
        this.likeStatus = "loading";

        if (value) {
            this.facebookService.setLike(this.$scope.url).then((postId) => {
                this.likeStatus = 'unlike';
                this.postId = postId;
            });
        }
        else {
            this.facebookService.setUnlike(this.postId).then(() => {
                this.likeStatus = 'like';
                this.postId = void 0;
            });
        }
    };
    public login = () => {
        this.userService.socialLogin(socialProviders.facebook).then(() => {
            this.$state.forceReload();
        });
    };

    static $inject = ['$element', '$scope', 'facebookService', 'userService', '$state','localStorageHandler'];
    constructor(public $element: JQuery, public $scope, public facebookService: services.facebookService, public userService: services.userService, public $state, localStorageHandler) {

        facebookService.isObjectLiked($scope.url).then((res) => {
            this.likeStatus = res ? 'unlike' : 'like'; // if object is like then show unlike button and vice versa
            this.postId = res;
        }, () => {
            //user is not logged in via facebook,may be via other carrier
            let tokenDetails: IUser = localStorageHandler.get(storageNames.socialLoginDetails);
            if (!tokenDetails)
                this.likeStatus = 'login';
            });

        $scope.vm = this;

     }
    }
 }

脸书服务

   export class facebookService {

          public fireGraphApi = (endPoint, verb): ng.IPromise<any>=> {

        let config: IRequestConfig = {
            method: verb,
            url: "https://graph.facebook.com/v2.4/" + endPoint,
            showLoader: false
        }

        return this.$http(config).then((res: any) => {
            return res.data;
        });


    };

    get isUserLoggedIn(): ng.IPromise<any> {
        let tokenDetails: IUser = this.localStorageHandler.get(storageNames.socialLoginDetails);
        if (tokenDetails && tokenDetails.social_provider == socialProviders.facebook) {
            let url = "me?access_token=" + tokenDetails.access_token;
            return this.fireGraphApi(url, 'GET');
        }
        else
            return this.$q.reject();
    }

    public isObjectLiked = (objectUrl: string): ng.IPromise<any> => {
        let def = this.$q.defer();
        let tokenDetails: IUser = this.localStorageHandler.get(storageNames.socialLoginDetails);
        this.isUserLoggedIn.then(() => {
            //user is logged in 
            let url = "me/og.likes?access_token=" + tokenDetails.access_token + '&object=' + objectUrl;
            this.fireGraphApi(url, 'GET').then((res) => {
                if (res && res.data.length == 0) {
                    // not yet liked
                    def.resolve(void 0);
                }
                else {
                    //liked and show unlike button                     
                    def.resolve(res.data[0].id);
                }

            });
        }, () => {
            //rejected when user not logged in 
            def.reject();
        });

        return def.promise;
    };


    public setLike = (objectUrl: string): ng.IPromise<string> => {
        return this.isUserLoggedIn.then(() => {
            let url: string;
            let tokenDetails: IUser = this.localStorageHandler.get(storageNames.socialLoginDetails);
            url = "me/og.likes?access_token=" + tokenDetails.access_token + '&object=' + objectUrl;

            return this.fireGraphApi(url, 'POST').then((res) => {
                return res.id;
            });
        });
    };

    public setUnlike = (postId: string): ng.IPromise<any> => {
        return this.isUserLoggedIn.then(() => {
            let url: string;
            let tokenDetails: IUser = this.localStorageHandler.get(storageNames.socialLoginDetails);           
                url = postId + "?access_token=" + tokenDetails.access_token;

            return this.apiService.fireGraphApi(url, "DELETE").then((res) => {
                return res;
            });
        });
    }


    static $inject = ['$http', '$q', 'localStorageHandler', 'apiService'];
    constructor(public $http, public $q, public localStorageHandler: services.localStorageHandler, public apiService: services.apiService) {

        }
   }