1

UPDATE:

最終的に、 Android の場合apache cordova/phonegapは Eclipse を、iOS の場合は xcode を使用して実装することになりました。これは、私の好みのセットアップで機能する唯一のソリューションです。

プラグインとドキュメントをダウンロードするためのリンク: https://github.com/phonegap/phonegap-facebook-plugin

Previous post:

ハイブリッド アプリに Facebook ログインを実装したいと考えています。私はすでにFacebookのドキュメントでほとんど調査を行っていませんが、うまくいくものは見つかりませんでした. ヒントを教えていただければ、とても助かります。やり方を教えてくれる人には賞金をあげます。

Phonegap/cordova やその他のフレームワークを調べたいとは思いません。これらのフレームワークを学習するには多くの時間を必要とするからです。

Hybrid apps - like native apps, run on the device, and are written with web technologies (HTML5, CSS and JavaScript). Hybrid apps run inside a native container, and leverage the device’s browser engine (but not the browser) to render the HTML and process the JavaScript locally. A web-to-native abstraction layer enables access to device capabilities that are not accessible in Mobile Web applications, such as the accelerometer, camera and local storage.

誰かが解決策を持っていて、喜んで助けてくれるなら、私に知らせてください。

Javascript SDK を試しましたが、うまくいきません。

コード:

<div id="fb-root"></div>
<script>
 window.fbAsyncInit = function() {
  FB.init({
    appId      : 'xxxx', // App ID
    channelUrl : '//xxxx/channel.html', // Channel File
    status     : true, // check login status
    cookie     : true, // enable cookies to allow the server to access the session
    xfbml      : true  // parse XFBML
  });

  // Here we subscribe to the auth.authResponseChange JavaScript event. This event is fired
  // for any authentication related change, such as login, logout or session refresh. This means that
  // whenever someone who was previously logged out tries to log in again, the correct case below 
  // will be handled. 
  FB.Event.subscribe('auth.authResponseChange', function(response) {
    // Here we specify what we do with the response anytime this event occurs. 
    if (response.status === 'connected') {
      // The response object is returned with a status field that lets the app know the     current
      // login status of the person. In this case, we're handling the situation where they 
      // have logged in to the app.     
      testAPI();
    } else if (response.status === 'not_authorized') {
      // In this case, the person is logged into Facebook, but not into the app, so we call
      // FB.login() to prompt them to do so. 
      // In real-life usage, you wouldn't want to immediately prompt someone to login 
      // like this, for two reasons:
      // (1) JavaScript created popup windows are blocked by most browsers unless they 
      // result from direct interaction from people using the app (such as a mouse click)
      // (2) it is a bad experience to be continually prompted to login upon page load.
      FB.login();
    } else {
      // In this case, the person is not logged into Facebook, so we call the login() 
      // function to prompt them to do so. Note that at this stage there is no indication
      // of whether they are logged into the app. If they aren't then they'll see the Login
      // dialog right after they log in to Facebook. 
      // The same caveats as above apply to the FB.login() call here.
      FB.login();
    }
  });
  };

  // Load the SDK asynchronously
  (function(d){
   var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
   if (d.getElementById(id)) {return;}
   js = d.createElement('script'); js.id = id; js.async = true;
   /* js.src = "//connect.facebook.net/en_US/all.js"; */
   js.src="https://connect.facebook.net/en_US/all.js";
   ref.parentNode.insertBefore(js, ref);
  }(document));

  // Here we run a very simple test of the Graph API after login is successful. 
  // This testAPI() function is only called in those cases. 
  function testAPI() {
    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function(response) {
      console.log('Good to see you, ' + response.name + '.');
    });
  }
</script>

<!--
  Below we include the Login Button social plugin. This button uses the JavaScript SDK to
  present a graphical Login button that triggers the FB.login() function when clicked.

  Learn more about options for the login button plugin:
  /docs/reference/plugins/login/ -->

<!-- <fb:login-button show-faces="true" width="200" max-rows="1"></fb:login-button> -->

<!-- End script of Facebook Login -->
4

3 に答える 3

0

それ以外のFB.Event.subscribe

FB.getLoginStatusを使用する

FB.getLoginStatus(function(response) {
  if (response.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
    var uid = response.authResponse.userID;
    var accessToken = response.authResponse.accessToken;

    FB.api('/me', function(response) {
        console.log(response);
    }); 

  } else if (response.status === 'not_authorized') {
    // the user is logged in to Facebook, 
    // but has not authenticated your app
  } else {
    // the user isn't logged in to Facebook.
  }
 });

参照リンク: https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/

于 2013-11-11T20:02:06.420 に答える
0

私にとって非常にうまく機能している別のソリューションに出くわしました。さらに良いことに、ネイティブコードライブラリはありません! ここでの秘訣は、Facebook JavaScript SDK ライブラリをバイパスして、Facebook REST API エンドポイントを直接使用することです。

私は OpenFB JavaScript ライブラリ ( https://github.com/ccoenraets/OpenFB ) を使用して、呼び出しをすべて自分で作成するのではなく、この作業を簡単にしていますが、どちらの方法でも実行できます。以下に、それを機能させる方法の手順の概要を説明しました。

1) Facebook アプリを作成し、基本および詳細で URL 設定を更新して、Facebook またはローカル URL を使用したコールバックを許可します。次に、次のステップに備えて Facebook ID をコピーします

ここに画像の説明を入力

2) まだ行っていない場合は、Cordova をダウンロードしてインストールし、コマンド ラインにツールを配置します。次に、Sites フォルダーに移動し、次のコマンドを実行してプロジェクトを作成し、プラットフォームを追加します。

cordova create your-project-name
cordova platform add ios
cordova platform add android

3) ここで、Facebook ログインからのポップアップ ウィンドウを処理するために Cordova プラグインを追加する必要があります。プラグインを追加するには、次のコマンドを使用します。

cordova plugin add org.apache.cordova.inappbrowser

4) 次に、新しい Cordova プロジェクト内で OpenFB をダウンロードして構成する必要があります。この例では、彼らが提供するテスト ページを使用するだけなので、OpenFB Github ページからダウンロードし、ファイルを cordova プロジェクトの /www/ フォルダーに抽出します。その後、index.html を開き、次の行をステップ 1 の Facebook アプリ ID で編集します。

openFB.init({appId: 'YOUR_FB_APP_ID'});

5) これで、サンプルを実行し、ローカル ブラウザーのセットアップを使用してログインできるはずです。

6) iOS シミュレーターでテストするには、xCode をインストールしてからコマンドを実行する必要があります。

cordova emulate ios

Android エミュレーターでテストするには、Android SDK をインストールしてから、次のコマンドを実行する必要があります。

cordova emulate android

ケーブルで接続された iOS デバイスでテストするには、次のコマンドを実行します。

cordova run ios

ケーブルで接続された Android デバイスでテストするには、次のコマンドを実行します。

cordova run android
于 2015-08-06T20:24:36.937 に答える