ionic tutorial - Cordova plugin facebook4 | Ionic Cordova Facebook - ionic - ionic development - ionic 2 - ionic framework
- Facebook is a popular free social networking website that allows registered users to create profiles, upload photos and video, send messages and keep in touch with friends, family and colleagues.
Cordova Facebook
- The Facebook Connect plugin to obtain access to the native FB application on iOS and Android.
- Requires Cordova plugin: cordova-plugin-facebook4. For more info, please see the Facebook Connect.
- This plugin is used for connecting to Facebook API. Before you start integrating Facebook, you need to create Facebook app.
- You will create web app and then skip the quick start screen. Then you need to add website platform on the settings page.
- You can use following code snippet for the site URL while in development.
- After that you need to add Valid OAuth redirect URIs on the settings/advanced page. Just copy the following two urls
ionic tutorial . extension , ionic framework , ionic , ionic framework book , ionic app example , ionic templates , ionic getting started
Installing Facebook Plugin
- We did all the steps above to tackle some issues that often appear when using this plugin. This plugin is hard to set up because there are a lot of steps involved and documentation doesn't cover all of them.
- There are also some known compatibility issues with other Cordova plugins so we will use Teleric verified plugin version in our app.
- We will start by installing browser platform to our app from the command prompt.
- Next we need to do is to add root element on top of the body tag in index.html.
- index.html
- Now we will add Cordova Facebook plugin to our app. You need to change APP_ID and APP_NAME to match the Facebook app you created before.
- Now open index.html and add the following code after your body tag. Again you need to make sure that the appId and version are matching the Facebook app you created.
- This will ensure that Facebook SDK is loaded asynchronously without blocking the rest of the app.
- index.html
- Angular Service
- Since we installed everything we need to create service that will be our connection to the Facebook.
- These things can be done with less code inside controller, but we try to follow the best practices so we will use Angular service. The following code is showing entire service. We will explain it later.
- services.js
- In the above service we are creating four functions. First three are self explanatory.
- The fourth function is used for connecting to Facebook graph API. It will return id and email from the Facebook user.
- We are creating promise objects to handle asynchronic JavaScript functions. Now we need to write our controller that will call those functions.
- We will call each function separately for better understanding, but you will probably need to mix some of them together to get desired effect.
ionic tutorial . extension , ionic framework , ionic , ionic framework book , ionic app example , ionic templates , ionic getting started
Controller Code
- You are probably wondering why didn't we returned Auth service directly from the function expressions (first four functions).
- The reason for this is that you will probably want to add some more behavior after the Auth function is returned.
- You might send some data to your database, change the route after login etc. This can be easily done by using JavaScript then() method to handle all the asynchronous operations instead of callbacks.
- Now we need to allow users to interact with the app. Our HTML will contain four buttons for calling four functions we created.
ionic tutorial . extension , ionic framework , ionic , ionic framework book , ionic app example , ionic templates , ionic getting started
HTML Code
- When user tap LOG IN button facebook screen will appear. The user will be redirected to the app after the login is succesfull.