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.

Set up and run the Office 365 iOS SDK Samples

One of the best things about the Office 365 REST APIs is that it comes with an iOS SDK. That means you don't have to get down to the level of constructing HTTP calls - it's all abstracted away for you in the SDK.

Let's see how the SDK works by downloading and running a sample.

1. Go to the Office 365 SDK for iOS GitHub repository and clone it. You can either do that using the command line, or via the Mac desktop client.

Clone by clicking "Clone in Desktop" on the lower-right of this screenshot.

Clone by clicking "Clone in Desktop" on the lower-right of this screenshot.

2. After you've cloned the repository, cd into it using the terminal. Then, cd into the Samples folder. You'll notice there are three Samples: 

This is because there are currently three services that are under the Office 365 umbrella - OneDrive for Business (also known as SharePoint), Exchange, and Discovery (which is more of a tool to help you find the right endpoint for the service, than a service itself). In this tutorial, let's focus on the simple-drive-app sample. 

3. cd into simple-drive-app sample. Then, run the following command:

pod install

Please make sure that you have cocoapods installed already. This is a must-have for any Mac developer, so head on over to their website and learn how to set it up if you aren't already familiar with using it.

This is what you'll see if after cocoapods has successfully downloaded the dependencies.

4. Open the workspace:

XCode should launch and you'll see this:

5. Run the app on a simulator or device. In this case, I'm running it on a simulator. You'll see that it asks you for these things: Authority Url, Redirect Url and Client Id.

6. In order to get these, we must first register an app in Azure AD.

This tutorial assumes that you already know what Azure AD is, and that you have an Azure account as well as an Office 365 Developer Tenant. Here's a step-by-step walkthrough on how to set up an Office 365 Developer Tenant.

Go to, and click on Portal at the top. Enter your login for Azure, and select your Active Directory that you want this app to be registered to. Let's say we want to test it on my Office 365 tenant, so in this case, I select that tenant. You'll be able to select your own tenant here based on what you created previously.

Once you've select your Active Directory, click on "Applications" as shown below:

At the bottom, press "Add"

Select "Add an application my organization is developing" and on the next page, enter any name for your application. In this case, I'm using "iOS-Drive-Sample". Select "Native Client Application" since it's going to run on a mobile device.

On the next page, type anything you want into the Redirect URI box. I chose "" as my redirect URI.

Press OK. Go to "Configure" and you'll see the following:

First we want to press the green "Add application" button below and select "Office 365 SharePoint Online"

Then after we press the OK button, we'll want to just select all the permissions that we need for this sample app. Select "Read users' files" and "Edit or delete users' files" for now.

Press "Save" so the changes are saved.

7. Go back to the app running in the simulator, and under "Authority URL", paste the following: Replace YOURTENANT with the tenant address of your developer tenant. E.g. if your developer tenant is, your Authority URL is

For redirect URL and client ID, paste the ones that are shown on the Azure AD Application dashboard.

Press Save, and then press "Files" on the bottom tab bar after you're done filling up these, and you'll see our good 'ol login screen.

Sign in with your developer tenant account. E.g. ""

Screen Shot 2015-01-05 at 2.44.23 PM.png

8. Something's gone wrong - you'll see a very annoying loading indicator, and your files don't show up. Uh-oh, let's back up a bit and try to fix what's going on.

If you go back to Xcode and look at the Output, you'll see this:

Strange, why is it making a GET request to "" when that's not your OneDrive for Business (i.e. SharePoint) endpoint?

Fortunately, it's just changing a single string. Do a search in your workspace for "teeudev1" and you'll quickly realize that BaseController.m has that URL hard-coded in it. 

Replace teeudev1 with your tenant address. So if your developer tenant is "", you'll want to replace that highlighted text in the image above with ""

9. Run the app again. Select "Files" in the bottom tab bar and you should see all your files displayed!

Note: If it didn't work, press "Clear Credentials" under Settings and try logging in again.

I hope this helps you get the sample project set up and configured. 

You can press the "+" on the top right to create and upload a new text file.

Screen Shot 2015-01-05 at 2.59.02 PM.png

The files that are showing are the same files that are in your root folder in OneDrive for Business. You can access OneDrive for Business by going to the Office portal page and clicking on "OneDrive" at the top.

I hope this was helpful - if you have any questions, leave them in the Comments below and I'll try to answer them.