Skip to content

OneDrive/onedrive-sample-sync-ios

Repository files navigation

page_type products languages extensions
sample
office-onedrive
office-365
swift
contentType technologies createdDate scenarios
samples
Azure AD
3/9/2016 11:02:34 AM
Mobile

OneDrive Sync Sample for iOS

The OneDrive Sync Sample for iOS shows how to detect and sync file changes between a client app and the Microsoft OneDrive service in Office 365 for both online and offline scenarios.

Files

It demonstrates the following:

  • Ability to sync between a client app files list and a user’s OneDrive for Business account by using the OneDrive view.delta API. The app will detect any modifications, including added, modified, and deleted files and folders. Any changes will be reported to the app UI. For example, if someone deletes a file from the user’s drive on a laptop elsewhere, the client app will reflect the changes upon syncing.
  • Allows the user to create an app folder and text file by using the OneDrive API for an organizational account in Office 365.

Note: This app is written in Swift and doesn't support Microsoft account authentication. For more information on the view.delta API see View changes for a OneDrive Item and its children.

Prerequisites

Note: If you already have a subscription, the link to sign up for a developer subscription sends you to a page with the message Sorry, you can’t add that to your current account. In that case, use an account from your current Office 365 subscription.

  • A Microsoft Azure Tenant to register your application. Azure Active Directory (AD) provides identity services that applications use for authentication and authorization. A trial subscription can be acquired here: Microsoft Azure.

Important: You will also need to ensure your Azure subscription is bound to your Office 365 tenant. To do this, see the Active Directory team's blog post, Creating and Managing Multiple Windows Azure Active Directories. The section Adding a new directory will explain how to do this. You can also see Set up your Office 365 development environment and the section Associate your Office 365 account with Azure AD to create and manage apps for more information.

Running this sample in Xcode

  1. Clone this repository.

  2. Use CocoaPods to import the Active Directory Authentication Library (ADAL) iOS dependency:

      pod 'ADALiOS', '= 1.2.5'
    

This sample app already contains a podfile that will get the ADAL components (pods) into the project. Simply navigate to the project From Terminal and run:

    pod install

For more information, see Using CocoaPods in Additional Resources

  1. Open AuthenticationConstants.swift under Library/Authentication. You'll see that the ClientID, RedirectUri, and tenant name values can be added to the top of the file. Supply the necessary values here:

     // You will set your application's clientId, redirect URI, and tenant name. 
     static let ClientId    = "[ENTER_YOUR_CLIENT_ID]"
     static let RedirectUri = NSURL(string: "[ENTER_YOUR_REDIRECT_URI]")
     static let Authority   = "https://login.microsoftonline.com/common"
     static let ResourceId  = "https://YOUR_TENANT_NAME-my.sharepoint.com/"
    
  2. Open Info.plist For the key OneDrive base API URL you'll need to supply your tenant name: https://YOUR_TENANT_NAME-my.sharepoint.com/_api/v2.0.

  3. Run the sample.

After authenticating you'll reach a page that lists the names of files and folders stored in the app.

This list can be synced with the contents within the user's OneDrive for Business account by tapping the blue circular arrow icon in the bottom app bar. A blue checkmark will appear next to an item that is new or has been modified (like a file name change or the addition of a new file/folder). The blue plus icon will allow you to create a file or folder in OneDrive for Business. After adding a new file or folder, the app will sync and the addition of these items will be reported in the list with a checkmark next to them (new). The red circular arrow will delete all view.delta sync tokens. Again, for more information on the view.delta API see View changes for a OneDrive Item and its children.

Handling View.delta

The api view.delta is handled in OneDriverManager.swift under syncUsingViewDelta method. It accepts a sync token and the result is handled asynchronously by the completion block.

func syncUsingViewDelta(syncToken syncToken:String?,
        completion: (OneDriveManagerResult, newSyncToken: String?, deltaArray: [DeltaItem]?) -> Void) 

If the number of items returned in the response is truncated: This happens if the number of items is too large or the 'top' query is used to purposely limit the number of items. In this case, paging can be used to fetch all items. This sample uses recursion to achieve paging. Basically, if view.delta's response contains @odata.nextLink, it calls the method recursively and appends the result to the delta item array.

func syncUsingViewDelta(syncToken syncToken:String?, nextLink: String?, var currentDeltaArray: [DeltaItem]?,
        completion: (OneDriveManagerResult, newSyncToken: String?, deltaArray: [DeltaItem]?) -> Void)
        
        ...
if let nextLink = json["@odata.nextLink"] as? String {
                            self.syncUsingViewDelta(syncToken: syncToken, nextLink: nextLink, currentDeltaArray: currentDeltaArray, completion: completion)
                        }
                        else {
                            completion(OneDriveManagerResult.Success, newSyncToken: deltaToken, deltaArray: currentDeltaArray)
                        }

        

Questions and comments

We'd love to get your feedback about the OneDrive Sync Sample for iOS project. You can send your questions and suggestions to us in the Issues section of this repository.

Questions about Office 365 development in general should be posted to Stack Overflow. Make sure that your questions or comments are tagged with [Office365] and [OneDrive].

Contributing

You will need to sign a Contributor License Agreement before submitting your pull request. To complete the Contributor License Agreement (CLA), you will need to submit a request via the form and then electronically sign the CLA when you receive the email containing the link to the document.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

Additional resources

Copyright

Copyright (c) 2016 Microsoft. See License for more information.

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages