Using the Office 365 iOS SDK in Swift

In June 2014, Apple released what I consider to be one of its best products ever - a new language, Swift. Swift is fast, concise and extremely fun to write. But what about the hundreds of thousands of libraries out there written in Objective-C, including the Office 365 SDK for iOS?

In this article, I'll show you how to set up the Office 365 SDK for iOS in Swift. By the end of this guide, you will have successfully set up a new Swift project that connects to Office 365 and lists all your OneDrive for Business files in a table. Here's where you can find the accompanying code sample.

There is a guide written by Richard diZerega titled Developing iOS Apps with Azure and Office 365 APIs. It's a very detailed and great piece, but the key difference between his guide and mine is that mine uses the objects in the Office 365 iOS SDK to interact with the API, while his takes the approach of constructing calls using NSMutableURLRequest. I highly prefer to and recommend that you always use the SDK's abstractions when they're provided, and I hope that this guide will allow you to see how to do so with Swift.

Create a new project in Xcode. Let's call ours O365Demo. Then, go to the terminal and create a podfile. If this doesn't work for you, you probably don't have Cocoapods installed. This is the standard dependency-management tool for iOS development; head on over here to learn how to set it up.

Next, open the podfile you just created with Xcode. To put this in code, here are the three commands you have to type into the terminal:

Next, add two lines of code into your podfile to specify that you'll be using the Active Directory Authentication Library (ADAL) by Azure, and the Office 365 iOS SDK. Your podfile should look like this when you're done:

Next step is to install the pods, then open the workspace (close the project it's still open in Xcode). Go to the terminal and type this:

Add libPods-ADALiOS.a under Linked Frameworks and Libraries as shown in the screenshot below:

Don't worry that libPods-ADALiOS.a is in red. You can ignore the red highlighting for now - it will work.

Create a new Objective C file in your O365Demo project. Name it anything. You will get prompted to add a Bridging Header file:

After doing so, delete the Objective-C file that was created but leave the bridging header alone. (i.e. delete the highlighted file in the screenshot below)

Add these import statements to the bridging header file:

Now, let's go set up our actual app. We want to create a sample app with two screens - the first screen will ask you to login to Office 365, and after you login, you can press the "Get Files" button. This will then bring you to the second screen, which will show you a list of all your files in your root folder in OneDrive for Business (i.e. SharePoint).

Create a Table View Controller in Main.storyboard, and create a Button (with the label "Get Files") in the original View Controller. Control click on the button, and drag-and-drop to the table view controller to create a "Show" segue to the Table View Controller. Your storyboard should look like this:

This shows the table view, when the "Get Files" button is tapped.

Next, create a new Swift file and name it "FilesTableViewController". This will be our subclass of TableViewController. Replace any existing code in your FilesTableViewController.swift file with the following:

Make sure that the table view controller in your storyboard is the same one that you just subclassed. To do so, click on the table view controller in the Storyboard file and change its class to "FilesTableViewController" in the Inspector.

Notice I created an array which holds objects of type MSSharePointItem. This array is going to hold information about my files from SharePoint. Till now, I haven't made any API calls to the Office 365 server to populate it, so it'll remain empty. Run it and you will see that the app runs successfully - but nothing shows up in the table yet.

Now, let's actually add code to authenticate to Active Directory and get the Office 365 files. 

Go to ViewController.swift. We're going to add code that automatically signs the user in. Replace the contents of ViewController.swift with the following:

Note that I'm able to make a call to the Office 365 Objective-C library with the bridging header that we created earlier. This allowed me to write a bunch of method calls to the library in Swift. If you're not sure what the method signature of an Objective-C method is in Swift, use intellisense/autocomplete to help you.

Before you run this, though, you have to create an app in Azure AD. Read this article that I recently published for a step-by-step on how to do this (you'll want to start from Step 6 in that article). Once you have the app created, replace the variables in the code above with ones specific to your tenant and app in Azure AD.

Now, go back to FilesTableViewController.swift. Let's add code here to actually make an API request to the Office 365 Files API endpoint and return a list of our files. As I said earlier, what's great about using an SDK is that you don't have to construct HTTP requests by yourself; instead you can use the objects that the SDK provides to abstract this underlying implementation away.

Add the following chunk of code to after self.tableview.registerClass in the viewDidLoad method of FilesTableViewController.swift:

Run the app and you'll get a sign-in screen:

After you sign in, click "Get Files", and a list of your files will show.

That's it! Once again, if you want to download the project, it's on GitHub. If you have any questions, leave them in the Comments below.