Share via


Getting user data on OneDrive (iOS)

This topic describes how to get a user's data on OneDrive, from your iOS app.

In this article
Prerequisites
Step 1: Requesting info
Step 2: Requesting info about the user's friends
Step 3: Working with info

To access a user's info from Microsoft OneDrive, your app must check for two things. First, it must confirm that the user has signed in successfully. Second, it must request and receive the user's consent, to work with his or her info. For details about these steps, see Signing users in and Obtaining user consent.

Prerequisites

This topic assumes that your app has initialized the Live SDK successfully, the user has signed in successfully, and the user has consented to the wl.basic scope, which grants access to basic info about the user and his or her contacts.

Step 1: Requesting info

There are two different ways to work with a user's info.

  • The user ID of a person or contact.

  • The me shortcut that provides access to the signed-in user.

This code example shows how to request info about a specific person identified by the user ID, which is represented in this code example as USER_ID. In your code, replace USER_ID with the actual ID string of the user or contact for which you want to get info.

- (IBAction)getMe:(id)sender
{
    [self.liveClient getWithPath:@"USER_ID"
                        delegate:self
                       userState:@"getUserID"];
}

- (void) liveOperationSucceeded:(LiveOperation *)operation
{
    if ([operation.UserState isEqual:@"getUserID"]) {
        [self.infoLabel setText:[NSString stringWithFormat:@"@", [operation.result objectForKey:@"first_name"]]];
    }
}

- (void) liveOperationFailed:(NSError *)error operation:(LiveOperation *)operation
{
    if ([operation.userState isEqual:@"getUserID"]) {
        [self.infoLabel setText:[NSString stringWithFormat:@"%@: %@", @"Error getting name", [error localizedDescription]]];
    }
}

This code example shows how to use the me shortcut to request info about the signed-in user.

...
[self.liveClient getWithPath:@"me"
                    delegate:self
                   userState:@"getMe"];
...

Step 2: Requesting info about the user's friends

This code example shows how to use the me shortcut to get a list of the signed-in user's friends.

...
[self.liveClient getWithPath:@"me/friends"
                    delegate:self
                   userState:@"getFriends"];
...

Assuming that your app has permission to access data about the user's friends, you can get that info by using the user ID of a particular friend. In this example, the string value shown as the parameter to getWithPath was in the collection returned by the preceding example. In your code, you would use a value returned by the preceding call and not a string constant as shown in this example.

...
[self.liveClient getWithPath:@"contact.c1678ab4000000000000000000000000"
                    delegate:self
                   userState:@"getContact"];
...

Step 3: Working with info

To work with info, use methods of the LiveConnectClient interface, such as getWithPath, postWithPath, putWithPath, and deleteWithPath for GET, POST, PUT, and DELETE calls, respectively. For example, here's how to get the first name of a user's friend.

- (void) getContact
{
    [self.liveClient getWithPath:@"me/contacts" 
                        delegate:self];
}

- (void) liveOperationSucceeded:(LiveOperation *)operation
{
    NSArray *contacts = [operation.result objectForKey:@"data"];
    NSDictionary *firstContact = [contacts objectAtIndex:0];
    self.displayLabel.text = [firstContact objectForKey:@"first_name"];
}

See also

Other resources

iOS reference