Signing users in to OneDrive (iOS)

Let users sign in to their Microsoft OneDrive from your iOS apps.

In this article
Prerequisites
Register your app and configure its settings
Specify a redirect domain and get a client secret
Specify consent page settings
Referencing the iOS API with Objective-C
Sign the user in
Summary

The user must be signed in to his or her Microsoft account for your iOS app to work with the user's info on OneDrive. You help the user do this by adding sign-in functionality to your app in the form of a button or a hyperlink.

Prerequisites

We assume that you know how to create iOS apps, and can write code in Objective-C. You also need the following:

  • You must download and install the Live SDK for iOS.

  • You must have an app to which you want to add sign-in functionality.

  • The Live SDK iOS API requires Xcode 4.1 or later. Xcode 4.1 runs on Mac OS X Snow Leopard (version 10.6) and OS X Lion (version 10.7), while Xcode 4.2 runs only on OS X Lion (version 10.7).

Register your app and configure its settings

  1. Go to the Live SDK app management site.

  2. If prompted, sign in with your Microsoft account credentials.

  3. If you are not immediately prompted to provide the app's display name and primary language, click Create application.

  4. Type the app's display name and select the app's primary language.

  5. Read the Terms of use and the Privacy and Cookies statement, and then click I accept. A client ID is created and displayed in App Settings. It should look something like this: 00000000603E0BFE.

  6. To specify that your app is a mobile client app, in API Settings, click Yes under Mobile or desktop client app.

Specify a redirect domain and get a client secret

  1. In the Live SDK app management site, select your app and click Edit settings > API Settings.

  2. Under Redirect URLs, type the redirect domain you will be redirecting users to

  3. Click App Settings. On the application summary page, the client secret is displayed. It should look something like this:

    qXipuPomaauItsIsmwtKZ2YacGZtCyXD

    Note

    To change your client secret, click App Settings > Create a new client secret > OK. Both the old and new client secrets will continue to work until you activate the new client secret from API Settings in the Live SDK app management site.

After users sign in from your app to a service that is compatible with Microsoft account, the Allow access page (also called the consent page) is displayed, which explains to the user the types of info that your app wants to access. To customize the items in the consent page, go to your Live SDK app management site and specify the following items.

Consent page

App management site setting

Your app's logo

Basic Information page, Application logo image

Your app's domain name, which appears in several places in the consent page

API Settings page, Root Domain box

Your app's name, which appears in several places in the consent page

Basic Information page, Application name box

Your app's terms-of-use hyperlink

Basic Information page, Terms of service URL box

Your app's privacy statement hyperlink

Basic Information page, Privacy URL box

Note

If the Terms of service URL and Privacy URL settings are not specified, the text | APPLICATION_NAMEterms and privacy statement does not appear in the consent page.

Referencing the iOS API with Objective-C

You can reference the iOS API in two ways. We recommend that you reference the iOS API framework. However, you can reference the iOS API source code directly if you need to modify it or want to see how it works.

To reference the iOS API framework

  1. Open your iOS project in Xcode, if you haven't already done so.

  2. In the Project Navigator, click your project.

  3. In the project editor, in the Targets area, click your project.

  4. Click Build Phases.

  5. Expand Link Binary With Libraries, and then click Add items (+).

  6. Click Add Other.

  7. Go to and select the LiveSDK.framework folder within the downloaded Live SDK for iOS, and then click Open.

  8. In the Project Navigator, drag the LiveSDK.framework node and drop it into Build Phases tab's Copy Bundle Resources table in the project editor.

  9. For each code file from which you want to reference the iOS API framework, add this import statement: #import "LiveSDK/LiveConnectClient.h".

Note

If you reference the iOS API framework, you will be able to browse the framework's type and member definitions from your Xcode project. However, you may have difficulty browsing the underlying implementation code of the types and members because this code is stored in binary format within the framework.

To reference the iOS API source code directly

  1. Open your iOS project in Xcode, if you haven't already done so.

  2. Click File > Add Files to "Your Project Name".

  3. Go to and select the Library folder within the downloaded Live SDK for iOS, and then click Add.

  4. For each code file from which you want to reference the API source code, add this import statement: #import "LiveConnectClient.h".

Note

If you reference the iOS API source code directly, you will be able to browse the underlying implementation code of the framework's types and members easily from your Xcode project. However, any changes that you make to this source code could result in design-time or run-time errors that are difficult to debug.

If you reference the iOS API source code directly and you use Xcode 4.2 or later, you must disable Automatic Reference Counting (ARC). This is because the iOS API source code uses manual reference counting to enable the greatest number of developers using Xcode to reference the iOS API source code directly. If you want to use ARC, you must reference the iOS API framework by following the steps in the preceding section, or you must do an ARC conversion on the iOS API source code. To do an ARC conversion in Xcode:

  1. Follow the steps listed earlier in this section.

  2. On the Xcode menu, click Xcode > Preferences and select the Continue building after errors check box.

  3. On the Xcode menu, click Edit > Refactor > Convert to Objective-C ARC.

  4. Follow the on-screen directions.

Sign the user in

Here's the basic code that shows how to reference the iOS APIs, invite a user to sign in, and get the signed-in user's permission to get his or her basic profile info.

ViewController.h

#import <UIKit/UIKit.h>
#import "LiveSDK/LiveConnectClient.h"

@interface ViewController : UIViewController<LiveAuthDelegate, LiveOperationDelegate, 
    LiveDownloadOperationDelegate, LiveUploadOperationDelegate>
@property (strong, nonatomic) LiveConnectClient *liveClient;
@property (strong, nonatomic) IBOutlet UILabel *infoLabel;
@end

ViewController.m (where APP_CLIENT_ID is your app's client ID)

#import "ViewController.h"

@implementation ViewController
@synthesize liveClient;
@synthesize infoLabel;

NSString* APP_CLIENT_ID=@"000000004406774C";

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.liveClient = [[LiveConnectClient alloc] initWithClientId:APP_CLIENT_ID 
                                                         delegate:self 
                                                        userState:@"initialize"];
}

- (void)authCompleted:(LiveConnectSessionStatus) status 
              session:(LiveConnectSession *) session 
            userState:(id) userState
{
    if ([userState isEqual:@"initialize"])
    {
        [self.infoLabel setText:@"Initialized."];
        [self.liveClient login:self 
                        scopes:[NSArray arrayWithObjects:@"wl.signin", nil] 
                      delegate:self 
                     userState:@"signin"];
    }
    if ([userState isEqual:@"signin"])
    {
        if (session != nil)
        {
            [self.infoLabel setText:@"Signed in."];        
        }
    }
}

- (void)authFailed:(NSError *) error 
         userState:(id)userState
{
    [self.infoLabel setText:[NSString stringWithFormat:@"Error: %@", [error localizedDescription]]];
}
@end

Summary

You have just seen how to sign your users in to their OneDrive account. Next, you might want to give them the ability to work with their OneDrive files and folders in your app. To do this, see File and folder properties.