phonegap构建和Facebook插件 [英] phonegap build and Facebook Plugin
本文介绍了phonegap构建和Facebook插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在Phonegap Build工作,想要为应用程序添加一个Facebook登录。
但插件似乎不起作用
这是我收到的错误:
Cordova Facebook Connect插件在init上失败!
有关更多信息,我的phonegap版本是:3.1.0
,我使用Hydration(也许给你一个想法)
这里是我使用的简单代码:
< html>
< head>
< / head>
< body>
< button onclick =login()>登录< / button>
< button onclick =me()> Me< / button>
<! - < button onclick =getSession()>获取会话< / button> - >
< button onclick =getLoginStatus()>获取登录名< / button>
< button onclick =logout()>注销< / button>
< button onclick =facebookWallPost()> facebookWallPost< / button>
< button onclick =publishStoryFriend()> friendstory< / button>
< div id =data> loading ...< / div>
<! - < script src =http:// localhost:8080 / target / target-script-min.js#anonymous>< / script>
< div id =fb-root>< / div> - >
<! - cordova - >
< script src =cordova.js>< / script>
<! - cordova facebook plugin - >
< script src =cdv-plugin-fb-connect.js>< / script>
<! - facebook js sdk - >
< script src =facebook-js-sdk.js>< / script>
< script>
<! - 这些是通过弹出窗口显示给用户的通知,如果上述JS文件不存在于同一目录中 - >
如果((typeof cordova =='undefined')&((typeof Cordova =='undefined'))alert('Cordova变量不存在,检查你是否正确包含cordova.js)
if(typeof CDV =='undefined')alert('CDV变量不存在,检查你是否正确地包含了cdv-plugin-fb-connect.js)
if(typeof FB =='undefined')alert('FB变量不存在,检查你是否已经包含了Facebook JS SDK文件。
FB.Event.subscribe('auth.login',function(response){
alert('auth.login event');
});
FB.Event.subscribe('auth.logout',function(response){
alert('auth.logout event');
});
FB.Event.subscribe('auth.sessionChange',function(response){
alert('auth.sessionChange event');
});
FB.Event.subscribe('auth.statusChange',function(response){
alert('auth.statusChange event');
});
/ * function getSession(){
alert(session:+ JSON.stringify(FB.getSession()));
}
* /
函数getLoginStatus(){
FB.getLoginStatus(function(response){
if(response.status =='connected'){
alert('登录');
} else {
alert('not logged in');
}
});
}
var friendIDs = [];
var fdata;
function me(){
FB.api('/ me / friends',{fields:'id,name,picture'},function(response){
if(response.error ){
alert(JSON.stringify(response.error));
} else {
var data = document.getElementById('data');
fdata = response.data ;
console.log(fdata:+ fdata);
response.data.forEach(function(item){
var d = document.createElement('div');
d.innerHTML =< img src =+ item.picture +/>+ item.name;
data.appendChild(d);
});
}
var friends = response.data;
console.log(friends .length);
(var k = 0; k < friends.length&&& k < 200; k ++){
var friend = friends [k];
var index = 1;
friendIDs [k] = friend.id;
// friendsInfo [k] = friend;
}
console.log(friendId:+ friendID);
});
}
函数logout(){
FB.logout(function(response){
alert('logged out');
});
}
function login(){
FB.login(
function(response){
if(response.session){
alert(登录);
} else {
alert('not logged in');
}
},
{scope:email}
);
}
函数facebookWallPost(){
console.log('Debug 1');
var params = {
method:'feed',
name:'Facebook Dialogs',
link:'https://developers.facebook.com/docs/reference/对话框/',
图片:'http://fbrell.com/f8.jpg',
caption:'参考文档',
描述:'对话框提供一个简单,一致的界面应用程序与用户进行接口。'
};
console.log(params);
FB.ui(params,function(obj){console.log(obj);});
}
函数publishStoryFriend(){
randNum = Math.floor(Math.random()* friendIDs.length);
var friendID = friendIDs [randNum];
if(friendID == undefined){
alert('请点击我的按钮先获得朋友列表)
} else {
console.log(friend id:+ friendID);
console.log('打开friendID的对话框',friendID);
var params = {
method:'feed',
to:friendID.toString(),
name:'Facebook Dialogs',
link:'https: //developers.facebook.com/docs/reference/dialogs/',
图片:'http://fbrell.com/f8.jpg',
caption:'参考文档',
描述:'对话框为应用程序提供一个简单,一致的界面,用于与用户进行交互。'
};
FB.ui(params,function(obj){console.log(obj);});
}
}
document.addEventListener('deviceready',function(){
try {
alert('Device ready ready!设置你的app_id低于这个警报');
FB.init({appId:[myAppID],nativeInterface:CDV.FB,useCachedDialogs:false});
document.getElementById('data' ).innerHTML =;
} catch(e){
alert(e);
}
},false);
< / script>
< div id =log>< / div>
< / body>
< / html>
这里是我的config.xml文件:
<?xml version =1.0encoding =UTF-8?>
< widget xmlns =http://www.w3.org/ns/widgetsxmlns:gap =http://phonegap.com/ns/3.3.0id =com.phonegap。 polimiSajjadversion =0.0.3>
< preference name =phonegap-versionvalue =3.1.0/>
< name> Sajjad App v0.0.4< / name>
< description>只是一个测试应用程序< / description>
< author href =sajjadsalehi.webadua.comemail =aghsajjy@yahoo.com> sajjad salehi< / author>
< preference name =orientationvalue =portrait/>
< preference name =fullscreenvalue =true/>
< preference name =exit-on-suspendvalue =false/>
< preference name =android-installLocationvalue =auto/>
< gap:plugin name =org.apache.cordova.device/>
< gap:plugin name =org.apache.cordova.camera/>
< gap:plugin name =org.apache.cordova.device-motion/>
< gap:plugin name =org.apache.cordova.geolocation/>
< gap:plugin name =com.phonegap.plugins.facebookconnect>
< param name =APP_IDvalue =[myAppID]/>
< param name =APP_NAMEvalue =Polimi Students Market/>
< / gap:plugin>
< icon src =icon.jpg/>
< / widget>
解决方案
在呼叫任何Facebook之前,在页面加载时执行以下代码功能。
FB.init({
appId:your facebook id,
nativeInterface:CDV .FB,
useCachedDialogs:false,
status:true
});
Maybe it's a stupid question, but I can not solve it! I'm working on Phonegap Build, and want to add a facebook login for the app. but plugin seems not to work !
here is the error I receive : "Cordova Facebook Connect plugin fail on init!"
for more information my phonegap version is : 3.1.0 and I use Hydration (Maybe gives you an idea) and here is the simple code I use:
<html>
<head>
</head>
<body>
<button onclick="login()">Login</button>
<button onclick="me()">Me</button>
<!--<button onclick="getSession()">Get session</button>-->
<button onclick="getLoginStatus()">Get login</button>
<button onclick="logout()">Logout</button>
<button onclick="facebookWallPost()">facebookWallPost</button>
<button onclick="publishStoryFriend()">friendstory</button>
<div id="data">loading ...</div>
<!--<script src="http://localhost:8080/target/target-script-min.js#anonymous"></script>
<div id="fb-root"></div>-->
<!-- cordova -->
<script src="cordova.js"></script>
<!-- cordova facebook plugin -->
<script src="cdv-plugin-fb-connect.js"></script>
<!-- facebook js sdk -->
<script src="facebook-js-sdk.js"></script>
<script>
<!-- These are the notifications that are displayed to the user through pop-ups if the above JS files does not exist in the same directory-->
if ((typeof cordova == 'undefined') && (typeof Cordova == 'undefined')) alert('Cordova variable does not exist. Check that you have included cordova.js correctly');
if (typeof CDV == 'undefined') alert('CDV variable does not exist. Check that you have included cdv-plugin-fb-connect.js correctly');
if (typeof FB == 'undefined') alert('FB variable does not exist. Check that you have included the Facebook JS SDK file.');
FB.Event.subscribe('auth.login', function(response) {
alert('auth.login event');
});
FB.Event.subscribe('auth.logout', function(response) {
alert('auth.logout event');
});
FB.Event.subscribe('auth.sessionChange', function(response) {
alert('auth.sessionChange event');
});
FB.Event.subscribe('auth.statusChange', function(response) {
alert('auth.statusChange event');
});
/*function getSession() {
alert("session: " + JSON.stringify(FB.getSession()));
}
*/
function getLoginStatus() {
FB.getLoginStatus(function(response) {
if (response.status == 'connected') {
alert('logged in');
} else {
alert('not logged in');
}
});
}
var friendIDs = [];
var fdata;
function me() {
FB.api('/me/friends', { fields: 'id, name, picture' }, function(response) {
if (response.error) {
alert(JSON.stringify(response.error));
} else {
var data = document.getElementById('data');
fdata=response.data;
console.log("fdata: "+fdata);
response.data.forEach(function(item) {
var d = document.createElement('div');
d.innerHTML = "<img src="+item.picture+"/>"+item.name;
data.appendChild(d);
});
}
var friends = response.data;
console.log(friends.length);
for (var k = 0; k < friends.length && k < 200; k++) {
var friend = friends[k];
var index = 1;
friendIDs[k] = friend.id;
//friendsInfo[k] = friend;
}
console.log("friendId's: "+friendIDs);
});
}
function logout() {
FB.logout(function(response) {
alert('logged out');
});
}
function login() {
FB.login(
function(response) {
if (response.session) {
alert('logged in');
} else {
alert('not logged in');
}
},
{ scope: "email" }
);
}
function facebookWallPost() {
console.log('Debug 1');
var params = {
method: 'feed',
name: 'Facebook Dialogs',
link: 'https://developers.facebook.com/docs/reference/dialogs/',
picture: 'http://fbrell.com/f8.jpg',
caption: 'Reference Documentation',
description: 'Dialogs provide a simple, consistent interface for applications to interface with users.'
};
console.log(params);
FB.ui(params, function(obj) { console.log(obj);});
}
function publishStoryFriend() {
randNum = Math.floor ( Math.random() * friendIDs.length );
var friendID = friendIDs[randNum];
if (friendID == undefined){
alert('please click the me button to get a list of friends first');
}else{
console.log("friend id: " + friendID );
console.log('Opening a dialog for friendID: ', friendID);
var params = {
method: 'feed',
to: friendID.toString(),
name: 'Facebook Dialogs',
link: 'https://developers.facebook.com/docs/reference/dialogs/',
picture: 'http://fbrell.com/f8.jpg',
caption: 'Reference Documentation',
description: 'Dialogs provide a simple, consistent interface for applications to interface with users.'
};
FB.ui(params, function(obj) { console.log(obj);});
}
}
document.addEventListener('deviceready', function() {
try {
alert('Device is ready! Make sure you set your app_id below this alert.');
FB.init({ appId: "[myAppID]", nativeInterface: CDV.FB, useCachedDialogs: false });
document.getElementById('data').innerHTML = "";
} catch (e) {
alert(e);
}
}, false);
</script>
<div id="log"></div>
</body>
</html>
and here is my config.xml file:
<?xml version="1.0" encoding="UTF-8" ?>
<widget xmlns = "http://www.w3.org/ns/widgets" xmlns:gap = "http://phonegap.com/ns/3.3.0" id = "com.phonegap.polimiSajjad" version = "0.0.3">
<preference name="phonegap-version" value="3.1.0" />
<name>Sajjad App v0.0.4</name>
<description>just a test app</description>
<author href="sajjadsalehi.webadua.com" email="aghsajjy@yahoo.com">sajjad salehi</author>
<preference name="orientation" value="portrait" />
<preference name="fullscreen" value="true" />
<preference name="exit-on-suspend" value="false" />
<preference name="android-installLocation" value="auto" />
<gap:plugin name="org.apache.cordova.device" />
<gap:plugin name="org.apache.cordova.camera" />
<gap:plugin name="org.apache.cordova.device-motion" />
<gap:plugin name="org.apache.cordova.geolocation" />
<gap:plugin name="com.phonegap.plugins.facebookconnect">
<param name="APP_ID" value="[myAppID]" />
<param name="APP_NAME" value="Polimi Students Market" />
</gap:plugin>
<icon src="icon.jpg" />
</widget>
解决方案
Execute the following code on page load before calling any facebook functions.
FB.init({
appId: "your facebook id",
nativeInterface: CDV.FB,
useCachedDialogs: false,
status: true
});
这篇关于phonegap构建和Facebook插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文