Share via


Downloading and uploading files on OneDrive (iOS)

Your apps can use the Live SDK for iOS to download or upload files on Microsoft OneDrive.

In this article
Prerequisites
Upload a file
Download a file
Update files

Prerequisites

We assume that the user has already signed in and consented to the wl.skydrive scope for reading file or folder properties. If you have not added user sign-in to your iOS apps, see Signing users in (iOS).

We assume that you are familiar with Objective-C, and can write iOS apps.

Upload a file

To upload a file, photo, video, or audio, use code like this. You can upload file sizes up to 100 megabytes. In this example, the uploadToPath method is called with the path, file name, file data, and delegate to start the file upload. If the file was uploaded successfully, liveOperationSucceeded is called to save the upload result, and display file properties of the uploaded file to the user.

Note

Before uploading a file, have your app check to make sure that there is enough available OneDrive storage space. For example, your app could get the size of the file in bytes and then compare it to the number of available storage bytes LiveUploadOperationDelegate liveOperationSucceeded method returned in the OneDrive quota call. For more info, see Get a user's total and remaining OneDrive storage quota.

-(void) uploadFile
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Fire-Phoenix" ofType:@"jpg"];
    NSData *fileToUpload = [[[NSData alloc]initWithContentsOfFile:path] retain];
    [self.liveClient uploadToPath:@"me/skydrive"
                         fileName:@"Favorite pic.jpg" 
                             data:fileToUpload
                         delegate:self];
}

- (void) liveOperationSucceeded:(LiveOperation *)operation
{
    NSString *fileId = [operation.result objectForKey:@"id"];
    NSString *fileName = [operation.result objectForKey:@"name"];
    NSString *fileDescription = [operation.result objectForKey:@"description"];
    NSString *fileSize = [operation.result objectForKey:@"size"];
    self.displayLabel.text = [NSString stringWithFormat:
                              @"File Properties:\n"
                              @"\tId. %@\n"
                              @"\tName. %@\n"
                              @"\tDescription. %@\n"
                              @"\tSize. %@\n"
                              , fileId, 
                              fileName, 
                              fileDescription, 
                              fileSize];
}

Download a file

Users can get the contents of a file, photo, video, or audio. The wl.skydrive scope is required.

Note

You can instruct the user's web browser to prompt the user to save the file that's downloading instead of having the browser try to download and display the file directly in its own window. To do this, add the download=true query-string parameter after /content (for example, FILE_ID/content?download=true). This call adds the Content-Disposition: attachment header to the response.

-(void) downloadFile
{
    [self.liveClient downloadFromPath:@"file.95d463b9efcca0e1.95D463B9EFCCA0E1!113/content" 
                             delegate:self];
}

- (void) liveOperationSucceeded:(LiveOperation *)operation
{
    LiveDownloadOperation * downloadOperation = (LiveDownloadOperation *)operation;
    UIImage *downloadedPicture = [UIImage imageWithData:downloadOperation.data];
    imageView.image = downloadedPicture;
}

Update files

To change contents for an existing file, photo, video, or audio, use the code in the preceding Upload a file section, plus the LiveUploadOverwriteOption enumeration with LiveUploadOverwrite as the value.