SharePoint Integration with iOS Application

Hello! I was working on a project which involved linking an iOS application to Sharepoint and perform CRUD operations on the Contacts. There are quite some blogs and sample code available for using Mail/OneDrive etc. but there isn’t any blog that helps you with the Contacts (atleast untill now ;))  In the next two blogs, I will try to elaborate on how to do that. Thanks for visiting!

Prerequisites:

  1. A valid SharePoint site login credentials.
  2. CocoaPods installed in your Mac. (Refer my previous blog on CocoaPods: How To)

Before the application can access the sharepoint site, we will have to configure few things in the sharepoint site. If you are not sure how to go ahead with that, follow the instructions provided in the below.

Configuring iOS application in SharePoint

  1. Configuring: Developing iOS Apps with Azure and Office 365 APIs [Section: Configuring the Application in Azure AD. Steps: 1-12].
  2. Obtain Client Authentication parameters: Developing iOS Apps with Azure and Office 365 APIs [Section: Client Authentication to Azure AD].

Documentation: Azure Documentation

Building the iOS Application:

Step 1: Create a sample iOS application.
[Here, I created with the following options: Xcode -> New Project -> iOS -> Application -> Single View Application]
– I called mine: office365Demo.

Step 2: Setup CocoaPods and link Office365 and ADALiOS libraries.
Follow Steps 4 to 7 from the blog: CocoaPods: How To to setup CocoaPods and to link the two required libraries: Office365 and ADALiOS.

Step 3: Verify the project dependancies.
Open the Xcode workspace file (in this case office365Demo.xcworkspace) to open the project and build the application. The application should build successfully.

Step 4: Implementing Azure Authentication in iOS App.
Authentication requires us to specify the below parameters. Use the below code to define macros or set constants wherever you require in the project.

In this example, let’s say, i’d specify this and implement the auth in the main view controller. If you have selected a single view application in Xcode like me, you’ll find a viewController.m file which is the view controller that is launched on running the application. Here, I am adding the below lines in the viewController.m file.

// Specifying Auth Parameters
#define Authority @"https://login.windows.net/mytenant.onmicrosoft.com"
#define ResourceID @"https://mytenant.sharepoint.com"
#define RedirectURI @"http://testOffice365.com&quot"
#define ClientID @"xxx000xx-x0x0-x0x0-x0x0-xxx000xxx000"

NOTE: Replace ‘mytenant’ with your tenant name and set the correct value for Client ID obtained while Configuring iOS application in SharePoint. If you don’t have any of the parameters, check the blogs mentioned in the first section.

Next, we will have to include the libraries required for user authentication.

// Including Libraries for user authentication
#import <ADALiOS/ADAuthenticationContext.h>
#import <office365-lists-sdk/OAuthentication.h>

For authenticating the user, the login screen is provided by the framework. We are not going the complicated way, i.e., doing a custom UI for login. So, all we have to do is to call the provided user authentication API. For convenience and for modularity, we will add the below method to the class.

- (void)authentcateUserWithAuthority:(NSString *)iAuthority resourceID:(NSString *)iResourceID redirectURI:(NSString *)iRedirectURI andClientID:(NSString *)iClientID {
    // Create an instance of ADAuthenticationContext
    ADAuthenticationContext *anAuthContext;
    ADAuthenticationError *anError;
    anAuthContext = [ADAuthenticationContext authenticationContextWithAuthority:iAuthority error:&anError];

    if (!anError) {
        // Acquire a token (prompts user to sign in)
        [anAuthContext acquireTokenWithResource:iResourceID
                                       clientId:iClientID
                                    redirectUri:[NSURL URLWithString:iRedirectURI]
                                completionBlock:^(ADAuthenticationResult *result) {
                                    // handle authentication error or success with
                                    // result.error or result.accessToken ...
                                    if (result.error) {
                                        NSLog(@"Error getting token: %@", result.error.localizedDescription);
                                        [[[UIAlertView alloc] initWithTitle:@"Error" message:result.error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
                                    } else {
                                        NSLog(@"Authenticaton Successful!");
                                        [[[UIAlertView alloc] initWithTitle:@"Success" message:@"Login Successful" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
                                    }
                                }];
    } else {
        NSLog(@"Error setting Auth context: %@", anError.localizedDescription);
        [[[UIAlertView alloc] initWithTitle:@"Error" message:anError.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
    }
}

We can call the above method from viewController’s viewDidLoad as below. (If there are multiple view presentation animations happening, this may cause a crash. In such a case, you can call the above method from viewDidAppear: method.)

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self authentcateUserWithAuthority:Authority resourceID:ResourceID redirectURI:RedirectURI andClientID:ClientID];
}

This means, the user authentication will be done once the view loads. (An auth token is saved by the framework and user need not enter his/her credentials every time.) The API internally checks if it has a valid token. If not, the login screen is displayed to the user. This is a webpage that works just like the sharepoint web login.

LoginScreen

Login using a valid credentials for the specified parameters. Once login is successful, User will see an alert saying “Login Successful”.

Congrats! Time for a little celebration! 🙂

References:

  1. Office Dev Center
  2. The Office 365 iOS SDK codebase
  3. Microsoft Open Technologies
  4. Microsoft Azure Active Directory Authentication Library (ADAL) for iOS and OSX
  5. Overview of developing on the Office 365 platform
  6. Developing iOS Apps with Azure and Office 365 APIs
  7. Sample Code: Office 365 Connect app for iOS
Advertisements

2 thoughts on “SharePoint Integration with iOS Application

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s