Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

iOS Objective-C SDK sample for working with profile pictures in Microsoft Graph, a unified endpoint for accessing data, relationships and insights that come from the Microsoft Cloud.

License

Notifications You must be signed in to change notification settings

microsoftgraph/ios-objectivec-profile-picture-sample

Repository files navigation

[ARCHIVED] Microsoft Graph SDK Profile Picture Sample for iOS

This project is being archived. As part of the archival process, we're closing all open issues and pull requests.

You can continue to use this sample "as-is", but it won't be maintained moving forward. We apologize for any inconvenience.

Overview

This sample is a lightweight iOS project for working with profile pictures in Microsoft Graph, a unified endpoint for accessing data, relationships and insights that come from the Microsoft Cloud. It shows how to connect and authenticate to the service, and then call user photo APIs through the Microsoft Graph SDK for iOS.

Profile Picture Workflow

It will cover the following:

  • Retrieving a profile picture for the signed in user from Office 365
  • Retrieving photo meta data such as the image height and width
  • Basic cropping of the profile picture
  • Uploading a newly cropped image as a profile picture in Office 365

Note: Try out the Application Registration Portal page which simplifies registration so you can get this sample running faster.

Prerequisites

Note: This sample relies on having an organizational account with a profile picture set in Microsoft Exchange or applied in the user's Outlook profile. If there is one not set, retieval of the photo will fail as no image is found.

  • A client id from the registered app at Application Registration Portal. Once at the site click Go to app list. From here you can add an app (mobile application), and record the Client ID for the steps below in Running this sample in Xcode.

Cloning and configuring this sample in Xcode

  1. Clone this repository.

  2. Use CocoaPods to import the SDK dependencies. This sample app already contains a podfile that will get the pods into the project. Simply navigate to the project root from Terminal and run:

     pod install
    

    For more information, see Using CocoaPods in Additional Resources

  3. Open iOS-ObjectiveC-ProfilePicture-SDK-sample.xcworkspace

  4. Open Authentication/AuthenticationConstants.m. You'll see that the Client ID from the registration process can be added to the top of the file:

     // You will set your application's clientId.
     
     NSString * const kClientId    = @"ENTER_YOUR_CLIENT_ID";
    

Enable keychain sharing

For Xcode 8, you'll need to add the keychain group or your app will fail to access the keychain. To add the keychain group:

  1. Select the project on the project manager panel in Xcode. (⌘ + 1).

  2. Select iOS-Objectivec-ProfilePicture-SDK-sample.

  3. On the Capabilities tab, enable Keychain Sharing.

  4. Add com.microsoft.iOS-ObjectiveC-ProfilePicture-SDK-sample to the Keychain Groups.

Running this sample

  1. Run the sample. You'll be asked to connect/authenticate to a work mail account and you'll provide your Office 365 credentials. Once authenticated you'll be taken to the profile controller where you'll have the ability to retrieve both the signed in user's photo and associated meta data. Also, you'll have the ability to crop the photo and upload the newly cropped image to Office 365.

Note: The cropping functionality here is basic, and just meant to assist with demonstrating uploading a new image to Office 365. It takes the center portion of the rendered image and displays as a newly cropped image. If you crop a picture and wish to return to the original, click Retrieve Photo. Any uploaded profile picture must meet the minimum size requirements of 48x48.

##Code of Interest

Contollers/ConnectViewController - Authenticates the user. From here a call is made into the AuthenticationProvider to obtain an access token, connectToGraphWithClientId: (NSString)clientId scopes(NSArray)completion:. Also an instance of the authentication provider is passed over to the ProfilePictureController to help initialize the Microsoft Graph client object needed for all Microsoft Graph calls.

Authentication/AuthenticationConstants.m - Here is where the app ClientId from the registration process is added to the project. Also, it's where the app permission scopes are defined. To access and work with the signed in user's profile pic the following scope must be supplied:

https://graph.microsoft.com/User.ReadWrite

Controllers/ProfilePictureController - This controller is responsible for initializing the MSGraphClient, and houses all Microsoft Graph photo operations.

-Gets the signed-in user's photo if they have a photo in Office 365.

- (void)getProfilePicture {
	 [[[self.graphClient me]photoValue] downloadWithCompletion:^(NSURL *location, NSURLResponse *response, NSError *error) {     
 	}];
}

-Gets the signed-in user's photo data if they have a photo (height and width).

- (void)getPhotoMetadata {
	[[[[self.graphClient me]photo]request]getWithCompletion:^(MSGraphProfilePhoto *response, NSError *error) {
	}];
}

-Uploads signed-in user's photo.

- (void)uploadPhoto {
 	NSData *croppedImage = UIImagePNGRepresentation(self.profilePictureImage.image);
	[[[self.graphClient me] photoValue] uploadFromData:croppedImage completion:^(MSGraphProfilePhoto *response, NSError *error) {
  }];
}

Questions and comments

We'd love to get your feedback about the Microsoft Graph SDK Profile Picture Sample. You can send your questions and suggestions to us in the Issues section of this repository.

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

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 opencode@microsoft.com with any additional questions or comments.

Additional resources

Copyright

Copyright (c) 2016 Microsoft. All rights reserved.

About

iOS Objective-C SDK sample for working with profile pictures in Microsoft Graph, a unified endpoint for accessing data, relationships and insights that come from the Microsoft Cloud.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published