问题描述
我尝试创建Cordova / Phonegap应用程序并添加Facebook插件,但提醒(type of facebookConnectPlugin);显示未定义:
sudo npm install -g cordova
cordova create hello com.example.hello HelloWorld
cd hello
cordova platform add ios
cordova -d plugin add https://github.com/phonegap/phonegap-facebook-plugin --variable APP_ID =1415347585409217--variable APP_NAME =Test
vi www / index.html#Add:alert(typeof facebookConnectPlugin);到最后的< script>
cordova emulate ios
预期:模拟器在警报对话框中显示对象 / p>
实际:模拟器在警报对话框中显示未定义。
任何人都知道我在做什么?
更新:感谢Devgeeks对他的回答。
这是我需要做的其他几件事情,以使其工作:
1)安装较新的develop插件:
cordova -d插件添加https://github.com/phonegap/phonegap-facebook-plugin#develop --variable APP_ID =1415347585409217--variable APP_NAME =Test
2)下面的开始标签:
<! - fb-root是FB API需要的。 - >
< div id =fb-root>< / div>
3)如果您希望FB登录在浏览器中工作/facebookConnectPlugin.js到您的应用程序。然后将它包含在结束标记之前:
< script src =facebookConnectPlugin.js>< / script&
然后将以下内容添加到您的部分:
< script>
window.fbAsyncInit = function(){
if(!window.cordova){
//初始化 - 仅在浏览器中测试时执行。
facebookConnectPlugin.browserInit(308939305080);
}
}
< / script>
如果您得到给定URL不允许由应用程序配置:一个或多个给定URL不允许由应用程序的设置它必须匹配的网站URL或Canvas URL,或域必须是应用程序的域之一的子域。编辑facebookConnectPlugin.js并将sdk.js更改为sdk / debug.js 。
4)如果您在构建Android应用程序时收到以下内容:
sdk.dir缺失。确保使用android更新项目生成local.properties或通过env注入它var
请尝试运行以下操作(从):
cp platforms / android / local.properties platforms / android / FacebookLib
添加:alert(typeof facebookConnectPlugin);到最后< script>
这是表现正常的,相信与否。
在 deviceready
事件触发时,插件不可用:
尝试检查生成的 www / js / index的
而不是立即触发的标记? onDeviceReady
中的 facebookConnectPlugin
js
I tried creating a Cordova/Phonegap app and adding the Facebook plugin, but alert(typeof facebookConnectPlugin); shows undefined:
sudo npm install -g cordova
cordova create hello com.example.hello HelloWorld
cd hello
cordova platform add ios
cordova -d plugin add https://github.com/phonegap/phonegap-facebook-plugin --variable APP_ID="1415347585409217" --variable APP_NAME="Test"
vi www/index.html # Add: alert(typeof facebookConnectPlugin); to the last <script>
cordova emulate ios
Expected: The emulator shows "Object" in the alert dialog.
Actual: The emulator shows "undefined" in the alert dialog.
Anyone know what I am doing wrong?
UPDATE: Thanks to Devgeeks for his answer below.
Here are a couple of other things I needed to do to get it working:
1) Install the newer "develop" branch of the plugin:
cordova -d plugin add "https://github.com/phonegap/phonegap-facebook-plugin#develop" --variable APP_ID="1415347585409217" --variable APP_NAME="Test"
2) Add the following below the opening tag:
<!-- fb-root is needed by the FB API. -->
<div id="fb-root"></div>
3) If you want FB login to work in the browser (for testing), copy www/js/facebookConnectPlugin.js into your app. Then include it before the closing tag:
<script src="facebookConnectPlugin.js"></script>
Then add the following to your section:
<script>
window.fbAsyncInit = function () {
if (!window.cordova) {
// Initialize - only executed when testing in the browser.
facebookConnectPlugin.browserInit(308939305080);
}
}
</script>
If you get "Given URL is not allowed by the Application configuration.: One or more of the given URLs is not allowed by the App's settings. It must match the Website URL or Canvas URL, or the domain must be a subdomain of one of the App's domains.", edit facebookConnectPlugin.js and change sdk.js to sdk/debug.js .
4) If you get the following while building the app for Android:
sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through an env var
try running the following (from Issue 432):
cp platforms/android/local.properties platforms/android/FacebookLib
Add: alert(typeof facebookConnectPlugin); to the last <script>
That is behaving as expected, believe it or not.
Plugins are not available till the deviceready
event has fired: http://docs.phonegap.com/en/3.5.0/cordova_events_events.md.html#deviceready
Try checking for the facebookConnectPlugin
in the onDeviceReady
of the generated www/js/index.js
instead of an immediately fired tag?
这篇关于Cordova / Phonegap:无法使Facebook Phonegap插件工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!