Objective C Mac App Tutorial

-->

You start the tutorial with a review of the Xcode interface, and how you are going to use it to build your Objective-C project. Jesse teaches you what Objective-C is, and the structure of an Objective-C App. Throughout this video tutorial you will learn about using declared properties and instance variables, how to manage memory, creating. Aug 15, 2013 Objective-C maintains all the same syntax and features of C. The object-oriented features that were added were done so in the model of Smalltalk, in which we send messages to objects (more on these later) in this manner: mug makeEmpty (The makeEmpty message is sent to the mug object.) In Objective-C we simply surround such actions with square. Great for Xcode 5 beginners The simple to use interface and image related, image guided, step by step nature of the app makes application development with Xcode appear seamlessly easy, the app gets right into Xcode application building and yet it doesn't feel as though you are being thrown into the pits, the non- voice guided also allows to take your time in learning the significance of your. Sep 17, 2014 About Objective-C. Objective-C is the primary programming language you use when writing software for OS X and iOS. It’s a superset of the C programming language and provides object-oriented capabilities and a dynamic runtime. Objective-C inherits the syntax, primitive types, and flow control statements of C and adds syntax for defining.

This tutorial teaches you how to build an React Native app that uses the Microsoft Graph API to retrieve calendar information for a user.

Tip

If you prefer to just download the completed tutorial, you can download or clone the GitHub repository.

Prerequisites

Before you start this tutorial, you should have the following installed on your development machine.

You should also have either a personal Microsoft account with a mailbox on Outlook.com, or a Microsoft work or school account. If you don't have a Microsoft account, there are a couple of options to get a free account:

  • You can sign up for a new personal Microsoft account.
  • You can sign up for the Office 365 Developer Program to get a free Office 365 subscription.

Note

This tutorial was written using Xcode version 11.4 and CocoaPods version 1.9.1. The steps in this guide may work with other versions, but that has not been tested.

Feedback

Please provide any feedback on this tutorial in the GitHub repository.

Create an iOS Objective-C app

Begin by creating a new Swift project.

  1. Open Xcode. On the File menu, select New, then Project.

  2. Choose the Single View App template and select Next.

  3. Set the Product Name to GraphTutorial and the Language to Objective-C.

  4. Fill in the remaining fields and select Next.

  5. Choose a location for the project and select Create.

Install dependencies

Before moving on, install some additional dependencies that you will use later.

  • Microsoft Authentication Library (MSAL) for iOS for authenticating to with Azure AD.
  • Microsoft Graph SDK for Objective C for making calls to Microsoft Graph.
  • Microsoft Graph Models SDK for Objective C for strongly-typed objects representing Microsoft Graph resources like users or events.
  1. Quit Xcode.

  2. Open Terminal and change the directory to the location of your GraphTutorial project.

  3. Run the following command to create a Podfile.

  4. Open the Podfile and add the following lines just after the use_frameworks! line.

  5. Save the Podfile, then run the following command to install the dependencies.

  6. Once the command completes, open the newly created GraphTutorial.xcworkspace in Xcode.

Design the app

In this section you will create the views for the app: a sign in page, a tab bar navigator, a welcome page, and a calendar page. You'll also create an activity indicator overlay.

Create sign in page

  1. Expand the GraphTutorial folder in Xcode, then select the ViewController.m file.

  2. In the File Inspector, change the Name of the file to SignInViewController.m.

  3. Open SignInViewController.m and replace its contents with the following code.

  4. Select the ViewController.h file.

  5. In the File Inspector, change the Name of the file to SignInViewController.h.

  6. Open SignInViewController.h and change all instances of ViewController to SignInViewController.

  7. Open the Main.storyboard file.

  8. Expand View Controller Scene, then select View Controller.

  9. Select the Identity Inspector, then change the Class dropdown to SignInViewController.

  10. Select the Library, then drag a Button onto the Sign In View Controller.

  11. With the button selected, select the Attributes Inspector and change the Title of the button to Sign In.

  12. With the button selected, select the Align button at the bottom of the storyboard. Select both the Horizontally in container and Vertically in container constraints, leave their values as 0, then select Add 2 constraints.

  13. Select the Sign In View Controller, then select the Connections Inspector.

  14. Under Received Actions, drag the unfilled circle next to signIn onto the button. Select Touch Up Inside on the pop-up menu.

Create tab bar

  1. Select the Library, then drag a Tab Bar Controller onto the storyboard.

  2. Select the Sign In View Controller, then select the Connections Inspector.

  3. Under Triggered Segues, drag the unfilled circle next to manual onto the Tab Bar Controller on the storyboard. Select Present Modally in the pop-up menu.

  4. Select the segue you just added, then select the Attributes Inspector. Set the Identifier field to userSignedIn, and set Presentation to Full Screen.

  5. Select the Item 1 Scene, then select the Connections Inspector.

  6. Under Triggered Segues, drag the unfilled circle next to manual onto the Sign In View Controller on the storyboard. Select Present Modally in the pop-up menu.

  7. Select the segue you just added, then select the Attributes Inspector. Set the Identifier field to userSignedOut, and set Presentation to Full Screen.

Create welcome page

  1. Select the Assets.xcassets file.

  2. On the Editor menu, select Add Assets, then New Image Set.

  3. Select the new Image asset and use the Attribute Inspector to set its Name to DefaultUserPhoto.

  4. Add any image you like to serve as a default user profile photo.

  5. Create a new Cocoa Touch Class file in the GraphTutorial folder named WelcomeViewController. Choose UIViewController in the Subclass of field.

  6. Open WelcomeViewController.h and add the following code inside the @interface declaration.

  7. Open WelcomeViewController.m and replace its contents with the following code.

  8. Open Main.storyboard. Select the Item 1 Scene, then select the Identity Inspector. Change the Class value to WelcomeViewController.

  9. Using the Library, add the following items to the Item 1 Scene.

    • One Image View
    • Two Labels
    • One Button
  10. Using the Connections Inspector, make the following connections.

    • Link the userDisplayName outlet to the first label.
    • Link the userEmail outlet to the second label.
    • Link the userProfilePhoto outlet to the image view.
    • Link the signOut received action to the button's Touch Up Inside.
  11. Select the image view, then select the Size Inspector.

  12. Set the Width and Height to 196.

  13. Use the Align button to add the Horizontally in container constraint with a value of 0.

  14. Use the Add New Constraints button (next to the Align button) to add the following constraints:

    • Align Top to: Safe Area, value: 0
    • Bottom Space to: User Display Name, value: Standard
    • Height, value: 196
    • Width, value: 196
  15. Select the first label, then use the Align button to add the Horizontally in container constraint with a value of 0.

  16. Use the Add New Constraints button to add the following constraints:

    • Top Space to: User Profile Photo, value: Standard
    • Bottom Space to: User Email, value: Standard
  17. Select the second label, then select the Attributes Inspector.

  18. Change the Color to Dark Gray Color, and change the Font to System 12.0.

  19. Use the Align button to add the Horizontally in container constraint with a value of 0.

  20. Use the Add New Constraints button to add the following constraints:

    • Top Space to: User Display Name, value: Standard
    • Bottom Space to: Sign Out, value: 14
  21. Select the button, then select the Attributes Inspector.

  22. Change the Title to Sign Out.

  23. Use the Align button to add the Horizontally in container constraint with a value of 0.

  24. Use the Add New Constraints button to add the following constraints:

    • Top Space to: User Email, value: 14
  25. Select the tab bar item at the bottom of the scene, then select the Attributes Inspector. Change the Title to Me.

  26. On the Editor menu, select Resolve Auto Layout Issues, then select Add Missing Constraints underneath All Views in Welcome View Controller.

The welcome scene should look similar to this once you're done.

Create calendar page

  1. Create a new Cocoa Touch Class file in the GraphTutorial folder named CalendarViewController. Choose UIViewController in the Subclass of field.

  2. Open CalendarViewController.h and add the following code inside the @interface declaration.

  3. Open CalendarViewController.m and replace its contents with the following code.

  4. Open Main.storyboard. Select the Item 2 Scene, then select the Identity Inspector. Change the Class value to CalendarViewController.

  5. Using the Library, add a Text View to the Item 2 Scene.

  6. Select the text view you just added. On the Editor, choose Embed In, then Scroll View.

  7. Using the Connections Inspector, connect the calendarJSON outlet to the text view.

  8. Select the tab bar item at the bottom of the scene, then select the Attributes Inspector. Change the Title to Calendar.

  9. On the Editor menu, select Resolve Auto Layout Issues, then select Add Missing Constraints underneath All Views in Welcome View Controller.

The calendar scene should look similar to this once you're done.

Create activity indicator

  1. Create a new Cocoa Touch Class file in the GraphTutorial folder named SpinnerViewController. Choose UIViewController in the Subclass of field.

  2. Open SpinnerViewController.h and add the following code inside the @interface declaration.

  3. Open SpinnerViewController.m and replace its contents with the following code.

Test the app

Save your changes and launch the app. You should be able to move between the screens using the Sign In and Sign Out buttons and the tab bar.

Register the app in the portal

In this exercise you will create a new Azure AD native application using the Azure Active Directory admin center.

  1. Open a browser and navigate to the Azure Active Directory admin center and login using a personal account (aka: Microsoft Account) or Work or School Account.

  2. Select Azure Active Directory in the left-hand navigation, then select App registrations under Manage.

  3. Select New registration. On the Register an application page, set the values as follows.

    • Set Name to iOS Objective-C Graph Tutorial.
    • Set Supported account types to Accounts in any organizational directory and personal Microsoft accounts.
    • Leave Redirect URI empty.
  4. Select Register. On the iOS Objective-C Graph Tutorial page, copy the value of the Application (client) ID and save it, you will need it in the next step.

  5. Select Authentication under Manage. Select Add a platform, then iOS / macOS.

  6. Enter your app's Bundle ID and select Configure, then select Done.

C-mac Video Laryngoscope

Add Azure AD authentication

In this exercise you will extend the application from the previous exercise to support authentication with Azure AD. This is required to obtain the necessary OAuth access token to call the Microsoft Graph. To do this, you will integrate the Microsoft Authentication Library (MSAL) for iOS into the application.

  1. Create a new Property List file in the GraphTutorial project named AuthSettings.plist.

  2. Add the following items to the file in the Root dictionary.

    KeyTypeValue
    AppIdStringThe application ID from the Azure portal
    GraphScopesArrayTwo String values: User.Read and Calendars.Read

Important

If you're using source control such as git, now would be a good time to exclude the AuthSettings.plist file from source control to avoid inadvertently leaking your app ID.

Implement sign-in

In this section you will configure the project for MSAL, create an authentication manager class, and update the app to sign in and sign out.

Configure project for MSAL

  1. Add a new keychain group to your project's capabilities.

    1. Select the GraphTutorial project, then Signing & Capabilities.
    2. Select + Capability, then double-click Keychain Sharing.
    3. Add a keychain group with the value com.microsoft.adalcache.
  2. Control click Info.plist and select Open As, then Source Code.

  3. Add the following inside the <dict> element.

  4. Open AppDelegate.m and add the following import statement at the top of the file.

  5. Add the following function to the AppDelegate class.

Create authentication manager

  1. Create a new Cocoa Touch Class in the GraphTutorial project named AuthenticationManager. Choose NSObject in the Subclass of field.

  2. Open AuthenticationManager.h and replace its contents with the following code.

  3. Open AuthenticationManager.m and replace its contents with the following code.

Add sign-in and sign-out

  1. Open the SignInViewController.m file and replace its contents with the following code.

  2. Open WelcomeViewController.m and add the following import statement to the top of the file.

  3. Replace the existing signOut function with the following.

  4. Save your changes and restart the application in Simulator.

If you sign in to the app, you should see an access token displayed in the output window in Xcode.

Get user details

In this section you will create a helper class to hold all of the calls to Microsoft Graph and update the WelcomeViewController to use this new class to get the logged-in user.

  1. Create a new Cocoa Touch Class in the GraphTutorial project named GraphManager. Choose NSObject in the Subclass of field.

  2. Open GraphManager.h and replace its contents with the following code.

  3. Open GraphManager.m and replace its contents with the following code.

  4. Open WelcomeViewController.m and add the following #import statements at the top of the file.

  5. Add the following property to the WelcomeViewController interface declaration.

  6. Replace the existing viewDidLoad with the following code.

If you save your changes and restart the app now, after sign-in the UI is updated with the user's display name and email address.

Get calendar data

In this exercise you will incorporate the Microsoft Graph into the application. For this application, you will use the Microsoft Graph SDK for Objective C to make calls to Microsoft Graph.

Get calendar events from Outlook

In this section you will extend the GraphManager class to add a function to get the user's events and update CalendarViewController to use these new functions.

Tutorial
  1. Open GraphManager.h and add the following code above the @interface declaration.

  2. Add the following code to the @interface declaration.

  3. Open GraphManager.m and add the following function to the GraphManager class.

    Note

    Consider what the code in getEventsWithCompletionBlock is doing.

    • The URL that will be called is /v1.0/me/events.
    • The select query parameter limits the fields returned for each events to just those the app will actually use.
    • The orderby query parameter sorts the results by the date and time they were created, with the most recent item being first.
  4. Open CalendarViewController.m and replace its entire contents with the following code.

  5. Run the app, sign in, and tap the Calendar navigation item in the menu. You should see a JSON dump of the events in the app.

Display the results

Now you can replace the JSON dump with something to display the results in a user-friendly manner. In this section, you will modify the getEventsWithCompletionBlock function to return strongly-typed objects, and modify CalendarViewController to use a table view to render the events.

Update getEvents

  1. Open GraphManager.h. Change the GetEventsCompletionBlock type definition to the following.

  2. Open GraphManager.m. Replace the completionBlock(data, nil); line in the getEventsWithCompletionBlock function with the following code.

Update CalendarViewController

  1. Create a new Cocoa Touch Class file in the GraphTutorial project named CalendarTableViewCell. Choose UITableViewCell in the Subclass of field.

  2. Open CalendarTableViewCell.h and replace its contents with the following code.

  3. Open CalendarTableViewCell.m and replace its contents with the following code.

  4. Open Main.storyboard and locate the Calendar Scene. Select the View in the Calendar Scene and delete it.

  5. Add a Table View from the Library to the Calendar Scene.

  6. Select the table view, then select the Attributes Inspector. Set Prototype Cells to 1.

  7. Use the Library to add three Labels to the prototype cell.

  8. Select the prototype cell, then select the Identity Inspector. Change Class to CalendarTableViewCell.

  9. Select the Attributes Inspector and set Identifier to EventCell.

  10. With the EventCell selected, select the Connections Inspector and connect durationLabel, organizerLabel, and subjectLabel to the labels you added to the cell on the storyboard.

  11. Set the properties and constraints on the three labels as follows.

    • Subject Label
      • Add constraint: Leading space to Content View Leading Margin, value: 0
      • Add constraint: Trailing space to Content View Trailing Margin, value: 0
      • Add constraint: Top space to Content View Top Margin, value: 0
    • Organizer Label
      • Font: System 12.0
      • Add constraint: Leading space to Content View Leading Margin, value: 0
      • Add constraint: Trailing space to Content View Trailing Margin, value: 0
      • Add constraint: Top space to Subject Label Bottom, value: Standard
    • Duration Label
      • Font: System 12.0
      • Color: Dark Gray Color
      • Add constraint: Leading space to Content View Leading Margin, value: 0
      • Add constraint: Trailing space to Content View Trailing Margin, value: 0
      • Add constraint: Top space to Organizer Label Bottom, value: Standard
      • Add constraint: Bottom space to Content View Bottom Margin, value: 8
  12. Open CalendarViewController.h and remove the calendarJSON property.

  13. Change the @interface declaration to the following.

  14. Open CalendarViewController.m and replace its contents with the following code.

  15. Run the app, sign in, and tap the Calendar tab. You should see the list of events.

Congratulations!

You've completed the iOS Objective-C Microsoft Graph tutorial. Now that you have a working app that calls Microsoft Graph, you can experiment and add new features. Visit the Overview of Microsoft Graph to see all of the data you can access with Microsoft Graph.

Feedback

Please provide any feedback on this tutorial in the GitHub repository.

Have an issue with this section? If so, please give us some feedback so we can improve this section.

  • iOS Tutorial
  • iOS Useful Resources
  • Selected Reading

The language used in iOS development is objective C. It is an object-oriented language and hence, it would be easy for those who have some background in object-oriented programming languages.

Interface and Implementation

In Objective C, the file where the declaration of class is done is called the interface file and the file where the class is defined is called the implementation file.

A simple interface file MyClass.h would look like the following −

The implementation file MyClass.m would be as follows −

Object Creation

Object creation is done as follows −

Methods

Method is declared in Objective C as follows −

An example is shown below.

You might be wondering what the andBreadth string is for; actually it’s an optional string, which helps us read and understand the method easily, especially at the time of calling. To call this method in the same class, we use the following statement −

As said above, the use of andBreadth helps us understand that breadth is 20. Self is used to specify that it's a class method.

Class Methods

Class methods can be accessed directly without creating objects for the class. They don't have any variables and objects associated with it. An example is shown below.

It can be accessed by using the class name (let's assume the class name as MyClass) as follows −

Instance Methods

Instance methods can be accessed only after creating an object for the class. Memory is allocated to the instance variables. An example instance method is shown below.

It can be accessed after creating an object for the class as follows −

Important Data Types in Objective C

Objective C Mac App Tutorial Pdf

Sr.No.Data Type
1

NSString

It is used for representing a string.

2

CGfloat

It is used for representing a floating point value (normal float is also allowed but it's better to use CGfloat).

3

NSInteger

It is used for representing integer.

4

BOOL

It is used for representing Boolean (YES or NO are BOOL types allowed).

Printing Logs

NSLog - used for printing a statement. It will be printed in the device logs and debug console in release and debug modes respectively. For example,

Control Structures

Most of the control structures are same as in C and C++, except for a few additions like for in statement.

Objective C Mac App Tutorial 2017

Properties

Mac App Development Tutorial Objective C

For an external class to access the class, variable properties are used. For example,

Accessing Properties

You can use dot operator to access properties. To access the above property, we will do the following.

You can also use the set method as follows −

Categories

Categories are used to add methods to the existing classes. By this way, we can add method to classes for which we don't have even implementation files where the actual class is defined. A sample category for our class is as follows −

Arrays

NSMutableArray and NSArray are the array classes used in objective C. As the name suggests, the former is mutable and the latter is immutable. An example is shown below.

Dictionary

NSMutableDictionary and NSDictionary are the dictionary classes used in objective C. As the name suggests, the former is mutable and the latter is immutable. An example is shown below.