Microsoft Graph SDK for ObjC
Get started with the Microsoft Graph SDK for ObjectiveC by integrating it into your iOS and MacOS applications!
Installation
Using CocoaPods
You can use CocoaPods to remain up to date with our latest version. Include the following line in your podfile:
pod 'MSGraphClientSDK'
Using Carthage
You can also chose to use Carthage for package management.
-
Install Carthage on your Mac using a download from their website or if using Homebrew
brew install carthage
. -
You must create a
Cartfile
that lists the MSGraphClientSDK library for this project on Github.
github "microsoftgraph/msgraph-sdk-objc" "tags/<latest_release_tag>"
-
Run
carthage update
. This will fetch dependencies into aCarthage/Checkouts
folder, then build the MSGraphClientSDK library. -
On your application target's “General” settings tab, in the “Linked Frameworks and Libraries” section, drag and drop the
MSGraphClientSDK.framework
from theCarthage/Build
folder on disk. -
On your application target's “Build Phases” settings tab, click the “+” icon and choose “New Run Script Phase”. Create a Run Script in which you specify your shell (ex:
/bin/sh
), add the following contents to the script area below the shell:
/usr/local/bin/carthage copy-frameworks
and add the paths to the frameworks you want to use under “Input Files”, e.g.:
$(SRCROOT)/Carthage/Build/iOS/MSGraphClientSDK.framework
This script works around an App Store submission bug triggered by universal binaries and ensures that necessary bitcode-related files and dSYMs are copied when archiving.
With the debug information copied into the built products directory, Xcode will be able to symbolicate the stack trace whenever you stop at a breakpoint. This will also enable you to step through third-party code in the debugger.
When archiving your application for submission to the App Store or TestFlight, Xcode will also copy these files into the dSYMs subdirectory of your application’s .xcarchive
bundle.
Prerequisites
Each network call to Microsoft Graph needs to be authenticated. For that purpose, creation of an instance of MSHTTPClient takes an MSAuthenticationProvider protocol's instance as parameter.
You can create the MSAuthenticationProvider instance via two ways:
MSGraphMSALAuthProvider
UsingThis repo handles the authentication scenarios via MSAL
By writing your own implmentation of MSAuthenticationProvider
You can also chose to handle authentication via any means that suits your needs. You need to follow below steps so that this SDK can interact with your authentication mechanism.
-
After integrating the SDK, you will have to provide implmentation for MSAuthenticationProvider by creating a new file.
-
In this file, write an implmentation for below method of MSAuthenticationProvider
- (void) getAccessTokenWithCompletion:(void (^)(NSString *accessToken, NSError *error))completion;
This implmentation should be able to talk to your authentication mechanism in order to get the access token or any error which occurred during the process and return it in completion block.
How to use SDK
Assuming you have gone thorugh the above steps, you will now have :
-
A .xcworkspace file integrated with MSGraphClientSDK pod or MSGraphClientSDK.framework integrated in your project.
-
Instance of a class implementing MSAuthenticationProvider protocol.
After above steps, you need to follow below steps:
- Create an instance of MSHTTPClient from MSClientFactory in below fashion:
MSHTTPClient *httpClient = [MSClientFactory createHTTPClientWithAuthenticationProvider:authenticationProvider];
- Configure a native request object which might look like something below:
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[MSGraphBaseURL stringByAppendingString:@"/me"]]];
- Create an MSURLSessionTask (in case of our example a DataTask) object with the help of MSHTTPClient in below fashion and call execute on it:
MSURLSessionDataTask *meDataTask = [httpClient dataTaskWithRequest:urlRequest
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
//Do something
}];
[meDataTask execute];
And that's it. You have now successfully made call to graph server asking information about the user.