Ionic is an open source and popular cross-platform mobile app framework, that helps to develop or build hybrid mobile apps quicklyly and easily. It saves a lot of time and money.
This article demonstrates how to authenticate with SharePoint & Office 365 from Ionic 3 mobile app using a native In-App Browser plugin. This is the base article for starting SharePoint from Ionic 3 mobile apps.
The following tools and requirements should be in place before starting this article.
Once we have installed Node.js, we can install other tools on the terminal or command line. Run the following commands step by step.
npm install -g ionic cordova
Once the installation is finished, let’s start creating an Ionic 3 app.
Create Ionic 3 app
Open your node.js terminal and open a specific location to create the Ionic 3 app. Start a new Ionic project using ionic start command,
ionic start TestApp
This command will take a few minutes because it installs all the dependencies and modules to a project.
Now, change the location to TestApp.
cd TestApp
Once you've changed the location to TestApp, add Android platform to your app.
ionic cordova platform add android
ionic cordova platform add browser (For Browser)
Now, run the project using the following commands.
ionic serve (For Browser)
ionic cordova run android (For Device)
Make sure you have connected device and settings and sdk tools has been set.
Now let’s start.
Introduction
SharePoint
SharePoint is a web-based collaborative platform that integrates with Microsoft Office. Launched in 2001, SharePoint is primarily sold as a document management and storage system, but the product is highly configurable and usage varies substantially between organizations.
In App Browser
In App Browser is a Cordova plugin that allows you to open a browser in your Cordova app or in our case Ionic app. This in app Browser can be used to open external URLs just like any normal web browser from your app.
Install and configure In App Browser and Splash screen
Follow below mentioned steps.
Step 1
Now we have the ionic app so we can move forward. First install an in app browser plugin to our ionic app using the following steps.
ionic cordova plugin add cordova-plugin-inappbrowser
npm install --save @ionic-native/in-app-browser
Second, we have used splash screen plugin for showing the loading part. If you don’t have it installed refer to the following steps. If you have already please skip this installation part.
ionic cordova plugin add cordova-plugin-splashscreen
npm install --save @ionic-native/splash-screen
Step 2
Now we have successfully installed plugins so we need to add In App Browser Object and Splash Screen Object to our app module file.
app.module.ts file
-
- import { InAppBrowser } from '@ionic-native/in-app-browser';
- import { SplashScreen } from '@ionic-native/splash-screen';
- @NgModule({
- providers: [
-
- InAppBrowser,
- SplashScreen
- ]
- })
- export class AppModule { }
Step 3
Now it’s time to integrate with the app component part in our app.
app.component.ts file
- import { InAppBrowser } from '@ionic-native/in-app-browser';
- import { SplashScreen } from '@ionic-native/splash-screen';
-
- constructor(
- public iab: InAppBrowser,
- public splashScreen: SplashScreen
- ) {
- this.login();
- }
- login() {
- this.splashScreen.show();
-
- const url = `https:
- + “2c3c3de8-aaaaa-bbbbbb-ccccc”+
- `&response_type=code&redirect_uri=`
- + encodeURI("urn:ietf:wg:oauth:2.0:oob") +
- `&response_mode=query&resource=`
- + encodeURI("https://example.sharepoint.com") + //here encoding resource url using default function
- `&state=12345`;
-
- const browser = this.iab.create(url, '_blank', {
- location: 'no',
- zoom: 'no',
- hardwareback: 'no',
- clearcache: 'yes'
- });
-
- browser.on("loadstart").subscribe((event) => {
- this.splashScreen.show();
- });
-
- browser.on("loadstop").subscribe((event) => {
- this.splashScreen.hide();
- browser.show();
- });
-
- browser.on("loaderror").subscribe((event) => {
-
- var result = event.url.split("code=");
- console.log("Authentication result", result);
-
- window["AuthResult"] = result[1].split('&')[0];
-
-
- localStorage.setItem('AuthCode', window["AuthResult"]);
- browser.close();
- });
- browser.on("exit").subscribe(
- (event) => {
-
- if (event) {
- if (event.type.toLowerCase() == 'exit') {
- if (localStorage.getItem("AuthCode") && localStorage.getItem("AuthCode") == 'cancel') {
- this.platform.exitApp();
- event.preventDefault();
- return true;
- }
- }
- }
- },
- (err) => {
- console.log("InAppBrowser Loadstop Event Error: " + err);
- }
- );
- }
Screenshots
For more reference, I have added full source code as attachments. For further details visit official website.
In App Browser
https://ionicframework.com/docs/native/in-app-browser/
Splash Screen
https://ionicframework.com/docs/native/splash-screen/
Summary
In this article, I discussed about how to authenticate SharePoint & Office 365 from ionic 3 mobile app using native In App Browser plugin.
This is the base article for starting to work with SharePoint using Ionic 3. Using this resulting Auth Code we will see in the next article how to get access token and refresh the token for SharePoint from Ionic 3. This is very important for performing Rest Api in SharePoint
If you have any questions/issues about this article, please let me know in the comments.