In this module, you add Facebook integration to your application. You allow users to login with Facebook, view their profile, and share their favorite sessions on their feed.
In this tutorial, you use OpenFB to perform the integration. OpenFB is a micro-library that lets you integrate your JavaScript applications with Facebook. It works for both browser-based and Cordova/PhoneGap apps. It also doesn't have any dependency: You don't need the Facebook plugin when running in Cordova. You also don't need the Facebook SDK. More information here.
ngOpenFB is just an Angular wrapper around the OpenFB library. It lets you use OpenFB "the Angular way": as an Angular service (called ngFB) instead of a global object, and using promises instead of callbacks.
Open conference/www/js/app.js, and add ngOpenFB as a dependency to the starter module:
.controller('AppCtrl', function ($scope, $ionicModal, $timeout, ngFB) {
Add the fbLogin function in the AppCtrl controller (right after the doLogin function):
$scope.fbLogin = function () {
ngFB.login({scope: 'email,read_stream,publish_actions'}).then(
function (response) {
if (response.status === 'connected') {
console.log('Facebook login succeeded');
$scope.closeLogin();
} else {
alert('Facebook login failed');
}
});
};
Test the application.
Make sure ionic serve (your local web server) is still running. If it's running but you closed your app page in the browser, you can reload the app by accessing the following URL: http://localhost:8100. If it's not running, open a command prompt, navigate (cd) to the ionic-tutorial directory and type:
ionic serve
In the application, open the side menu and select Login
Click the Login with Facebook button
Enter your Facebook credentials on the Facebook login screen, and authorize the application
Open the browser console: you should see the Facebook login succeeded message
The next time you login, you won't be asked for your credentials again since you already have a valid token. To test the login process again, simply logout from Facebook. The OpenFB library has additional methods to logout and revoke permissions that are beyond the scope of this tutorial.
Step 4: Display the User Profile
Create a template for the user profile view. In the conference/www/templates directory, create a new file named profile.html and implement it as follows:
Make sure ionic serve (your local web server) is still running. If it's running but you closed your app page in the browser, you can reload the app by accessing the following URL: http://localhost:8100. If it's not running, open a command prompt, navigate (cd) to the ionic-tutorial directory and type:
ionic serve
Open the side menu and select Login
Login with Facebook
Open the side menu and select Profile
Step 5: Publish to your feed
Open controllers.js, and inject ngFB in the SessionCtrl definition:
.controller('SessionCtrl', function ($scope, $stateParams, Session, ngFB) {
Add a share() function to the SessionCtrl controller:
$scope.share = function (event) {
ngFB.api({
method: 'POST',
path: '/me/feed',
params: {
message: "I'll be attending: '" + $scope.session.title + "' by " +
$scope.session.speaker
}
}).then(
function () {
alert('The session was shared on Facebook');
},
function () {
alert('An error occurred while sharing this session on Facebook');
});
};
Open session.html in the templates directory and add an ng-click handler to the Share button: invoke the share function:
Make sure ionic serve (your local web server) is still running. If it's running but you closed your app page in the browser, you can reload the app by accessing the following URL: http://localhost:8100. If it's not running, open a command prompt, navigate (cd) to the ionic-tutorial directory and type:
ionic serve
Open the side menu and select Login
Login with Facebook
Open the side menu, select Sessions, and select a session in the list
Click/Tap the Share button
Check your feed on Facebook
Step 6: Test Facebook integration on device (optional)
Add the InAppBrowser plugin used by OpenFB when running in Cordova. On the command line, navigate to the ionic-tutorial/conference directory and type:
Run and test your application on an iOS or Android device or emulator
This blog post was written by RajeshKumar - at Jempoints. For more on how to design and clarifications on Digital and Mobility contact us at www.jempoints.com
In this module, you use the Ionic CLI (command line interface) to create a new application based on the sidemenu starter app.
Steps
Open a new terminal window (Mac) or a command window (Windows), and navigate (cd) to the ionic-tutorialdirectory
Using the Ionic CLI, create an application named conference based on the sidemenu starter app:
ionic start conference sidemenu
Navigate to the conference folder
cd conference
Start the application in a browser using ionic serve.
ionic serve
NOTE: Because of cross domain policy issues (specifically when loading templates), you have to load the application from a server (using the http protocol and not the file protocol). ionic serve is a lightweight local web server with live reload.
In the application, open the side menu ("hamburger" icon in the upper left corner) and select Playlists. Select a playlist in the list to see the details view (not much to see at this point).
In the next modules, you will replace the playlists with a list of conference sessions retrieved from the server using the REST services you experimented with in the previous module.
Open the side menu again and select Login. Click the Login button to close the window (Login is not implemented in the starter app).
In the last module of this tutorial you will implement Login using Facebook.
step 4: Creating the Session Service
In the sidemenu starter app, the playlists are hardcoded in controllers.js. In this module, you create a Session service that uses the Angular resource module (ngResource) to retrieve the conference sessions using REST services.
Steps
In the conference/www/js directory, create a file named services.js
In services.js, define a module named starter.services with a dependency on ngResource:
In that module, define a service named Session that uses the Angular resource module to provide access to the REST services at the specified endpoint:
angular.module('starter.services', ['ngResource'])
.factory('Session', function ($resource) {
return $resource('http://localhost:5000/sessions/:sessionId');
});
In a real-life application, you would typically externalize the server parameters in a config module.
The starter.services module you just created has a dependency on the Angular resource module which is not included by default. Open index.html and add a script tag to include angular-resource.min.js (right after ionic-bundle.js):
Add a script tag to include the services.js file you just created (right after app.js):
<script src="js/services.js"></script>
step 5: Creating the Session Controllers
AngularJS controllers act as the glue between views and services. A controller often invokes a method in a service to get data that it stores in a scope variable so that it can be displayed by the view. In this module, you create two controllers: SessionsCtrl manages the session list view, and SessionCtrl manages the session details view.
Step 1: Declare starter.services as a Dependency
The two controllers you create in this module use the Session service defined in the starter.services module. To add starter.services as a dependency to the starter.controller module:
Open conference/www/js/controllers.js
Add starter.services as a dependency to make the Session service available to the controllers:
Replace it with a controller named SessionsCtrl that retrieves the list of conference sessions using the Session service and stores it in a scope variable named sessions:
Replace it with a controller named SessionCtrl that retrieves a specific session using the Session service and stores it in a scope variable named session:
In this module, you create two templates: sessions.html to display a list of sessions, and session.html to display the details of a particular session.
Step 1: Create the sessions template
In the conference/www/templates directory, rename playlists.html (plural) to sessions.html
In this module, you add two new routes (states) to the application: app.sessions loads the session list view, and app.session loads the session details view.
Step 1: Define the app.sessions route
Open app.js in conference/www/js
Delete the app.playlists state
Replace it with an app.sessions state defined as follows:
Make sure ionic serve (your local web server) is still running.
If it's running but you closed your app page in the browser, you can reload the app by loading the following URL: http://localhost:8100
If it's not running, open a command prompt, navigate (cd) to the ionic-tutorial directory and type:
ionic serve
In the conference application, open the side menu ("Hamburger" icon in the upper left corner) and selectSessions. Select a session in the list to see the session details.
This blog post was written by RajeshKumar - at Jempoints. For more on how to design and clarifications on Digital and Mobility contact us at www.jempoints.com
If you are like me and just starting to work with the Ionic Framework and don’t already have a machine setup to do Android, iOS, Node, etc development then many of the guides out there leave out a number of steps that you need to do in order to get everything working. Even being a windows user I was able to pretty easily get Ionic working on a Mac. The only difference between the Windows setup and the OSx setup is that you can only add iOS to Ionic on a Mac.
in terminal type source ~/.bash_profile to load the ~/.bash_profile changes.
type android from terminal. If everything is working correct it will open up the Android SDK Manager.
Install API 22 with SDK Platforms and Arm Image checked
Configure Android Emulator
In terminal type android avd. If everything is working correct it will open up the Android AVD Manager.
Click the create button and add at least 1 device to emulate
Configure Genymotion Setup
After Genymotion is installed, open up the Genymotion UI and click on the Add Button.
Then click the Sign in button and follow the login instructions to login with the account that you create as part of the Genymotion download.
After you are logged in, select from the Android Version drop down 4.4.4
From the Device model drop down select a device type
Then select a device from the available list
Click the Next button.
Click the Next button and wait for the device to download
Click the Finish button.
iOS Setup Steps
Install Xcode from app store. This will take awhile since it is ~2 gigs in size.
Once install is completed, open xcode and accept the license
Install the IOS Simulator that Ionic will use.
npm install -g ios-sim
Verifying that everything works
Open terminal
Navigate the directory where you store you development projects (I use /users/[Your Username]/projects)
From c:\projects type: ionic start todo blank
cd into c:\projects\todo (directory was created by the ionic start command)
Run the commands:
ionic platform add android
ionic platform add ios
Run the command:
ionic build android
ionic build ios
If using emulator run the command:
Note: The Genymotion emulator is seen as a device and not an emulator.
ionic emulate android
ionic emulate ios
If running on a device run the command:
For Android, if using the Genymotion emulator, make sure to start the Genymotion device you downloaded before running the command below. You can start the device by selecting the device that you downloaded and clicking the start button.
For iOS: In order to run on an iOS device you must go through the Apple provisioning process.