Showing posts with label XCode. Show all posts
Showing posts with label XCode. Show all posts

Saturday, April 2, 2022

Implementing Basic Auth0 in SwiftUI using Universal Login

Auth0 provides different mechanisms like SAML, OAuth, OIDC, and WS-Fed. I have looked into OAuth and will cover that today. Before jumping into implementation let's talk about basic terms which are used in the implementation.


Identity is something that proves who you are. e.g. if you have booked a flight and show up at an airport they would check your passport and hence proves that you are the one who the ticket is booked for and they compare the Given name, surname, passport number, etc. 

But here we are talking about digital identity, which consists of usernames, emails, and passwords. In some cases, it can be biometric as well like touch Id and face Id. 


Authentication is the process of validating that the provided user's identity details are valid and have been verified by the system. Once this is completed the system gets user identity which has information about the user e.g. name, age, maybe roles, or other information that is needed by the system.

Following are terms related to Authentication which you must have heard people talking about around you. 


If you are using only a single form of authentication e.g just user name and password to get into the system. Or just by swiping your access card.

Two-factor or 2FA

2FA is the latest term that is very popular and is a must IMHO. There are a few ways you can set up 2FA
  1. Using one time passwords that are sent to your mobile/phone
  2. Use an Authenticator app and use a code from the app which is required to be entered every time to log in.

Multi-factor or MFA

MFA is when we use more than 2 pieces of a user's identity to verify them e.g. along with username/password and OTP, users are using some kind of biometric to get into the system.


You must have seen the sign that reads "Authorized personnel only" at buildings or offices. That means that you need to special access code or badge to access those areas. 

It works in a similar way in digital systems, once you are authenticated, the system gets the user's identity and with the identity, it will know what kind of access the user has. The most basic roles are admin users and not normal users. Admins have special access and can have powers to maintain the system and do other operational activities. Systems can have different roles and groups to manage different access levels for the users. 

Now you know all the basic terms which are used when we set up any kind of system which involves setting up the login access and then customizing the system based on the level of access users have or what we say what role the user belongs to. 

I hope these terms make sense to you. Feel free to do some more search and make sure you understand these terms before moving forward. 

Ok, so now let's jump into some coding and see how we implement these in the real world. 

Auth0 has a toolkit/SDK for native iOS apps and we will use the new Swift Package Manger or SPM to add the toolkit to the SwiftUI app. Create a new Xcode project if you are starting or you can add a package to the existing project by going to File -> Add Package.

Add Package

You do not have to use SPM there are other installation options like Cocoapods or Carthage. After clicking Add Package get the URL for the Auth0 swift spm package and put it in the top right box as shown in the image below and should see the package. Click Add Package button on the bottom right and it will add the package to your project.

Package search

This will add the Auth0 package and any dependencies for this package like JWTDecode and Simple Keychain.

Auth0 package dependancies

I will be using Auth0 universal login to demo this integration for now but will go into more advanced implementations later. What it means is that we will use the login UI provided by Auth0 and all the features. 

To get started first thing is to create an account on Auth0 you can follow this link to sign up for Auth0. Once you create the account you need to register your app with Auth0. From the home page go to Applications -> Applications and click Create Application

Auth0 Create Application

Enter a name for the app, and select Mobile option. In the Settings tab after you create app you can see the Domain and ClientId. 

Add a new plist file to the project and add these to the newly added info plist file.

Auth0 info plist

Note: You want to do this for testing the app but do not check in the secrets to remote repository. Check this article in which I explained how to use and store secrets in iOS app. 

Once you finish the plist changes nexe thing is to build Allowed callback urls and logout urls. The format for Auth0 to build thse urls is format as below


The bundle identifier can be found as shown in image below. Copy the bundle identifier from project and build the url.

use the same url for Allowed Callback URLs and Allowed Logout URLs under the Application URIs section. 

Once you update the callback urls and then createa user that you can use to login once you have the Auth0 integrated in the app. Go to User Management -> Users and create a new user. 

Create User

Let's write some code and get Auth0 working in the sample app. Add a new view that is going to display the basic UI to login and logout actions. Let's call it RootView

import SwiftUI

struct RootView: View {

  @State var isLoggedIn = false


  @StateObject var viewModel = RootViewModel()

  var body: some View {

    VStack(spacing: 20) {

      if viewModel.isAuthenticated {


        Button(action: viewModel.logout) {



      } else {

        Text("Welcome to codewithKarma")

        Button(action: viewModel.login) {







struct LoginView_Previews: PreviewProvider {

  static var previews: some View {




So we need 2 actions login and logout. Let's create the Viewmodel and add these functions in the viewmodel

import Auth0

import Combine

class RootViewModel: ObservableObject {

  @Published var isAuthenticated = false

  func login() {



      .start { result in

        switch result {


        case .failure(let error):

          print("Failed with: \(error)")


        case .success(let credentials):


          self.isAuthenticated = true





  func logout() {



      .clearSession { result in

        switch result {

        case .failure(let error):

          print("Failed with: \(error)")

        case .success:

          self.isAuthenticated = false





View the print output and try to play with the parameters and see what comes in the parameters, 

Wrap up!

This is the most basic integration of Auth0 to the native iOS app using the Universal Login. I hope you will be able to achieve this, if you have any questions do let me know in comments.

Happy Coding!!

Thursday, March 24, 2022

Every iOS developer should know these Xcode tricks

 As iOS Developer you must be spending 8-9 hours of the day using Xcode. It is really important that you know the IDE to get most out of it and get more efficient with coding.

Refresh Canvas and close/open canvas

While using SwiftUI one of things you interact most is SwiftUI previewer and we see the Resume button on the previewer and have to click it to so many times. 

Well there is a keyboard shortcut that can make your life easier. By default it is set to Option + Command + P but you can reconfigure it your liking. e.g. I have set it to Command + P

and to do this I had to remove the shortcut for Print. Let's be very clear we do not do Print command from Xcode every day :), So I choose the nearest key to the default one.

Another good shortcut to know if Opening and closing the Canvas, May be there are times where you want to just focus on code and get some more real estate to work with and just want to close the canvas and then once done you want to reopen it again. You can use Option + Command + return to toggle the canvas visibility. Again you can use Xcode keybinding to reconfigure it to your liking.

Open Quickly

If you are working on large project and working on few files then you must know this feature of Xcode. Open quickly let's you open files with you know the name of the file. Press Command + Shift + O to open it and then just start trying the name and you will see the list

A companion option to this one is show only recent files toggle in the project navigator. When enabled it will only show the files you have recently interact with. 

Rename refactoring

Tuesday, March 22, 2022

How to enable spell checking in XCode

 Do you get a lot of PR comments for spelling mistakes? Then this will help you a lot to fix those issue while you are typing.

You can navigated to Edit -> Format -> Spelling and Grammar  and then select ✔Check Spelling While Typing option so that you get live spell checking.

As you can see in screenshot my misspelled word is highlighted with dotted red underline. 

Enjoy Coding!!

Sunday, May 24, 2020

Todo app using SwiftUI

It's time again and I started learning the iOS programming again as SwiftUI looks cool.

I spent couple of weeks going through the swift programming documentation and watched the WWDC videos. 

Note: This is an ongoing blog, which I update as I add more features to my TodoApp.


Follow along the Swift documentation. Try to code along the examples. Just go through the documentation even if you don't understand the whole thing just try to read all the documentation and get yourself aware with the concepts. This is will help later while reading the code of some
open source project. 

And.. read the documentation again. This is what I am doing now, this time try to understand and repeat until it becomes muscle memory. this is ongoing process.


Follow the SwiftUI tutorials and try to recreate them. 


One of my colleague suggested WWDC videos and watch them in the order. 


After going through all those videos and tutorials, I have decided to start creating something. So what is first app everyone build, it's Todo app. 


Download the Github repo to follow along and try the completed features.


  • Create a new Todo
  • Context menu to Complete or Delete a Todo
  • Show completed tasks
  • Add Priority
  • Add Date


  • Edit 
  • Persist the data
  • Add Notification
  • Add Location
  • Login functionality

Learnings while doing TodoApp

Thursday, June 13, 2019

Test Xamarin Forms app for Android 10 and iOS 13

Google and Apple have both released their new OS for mobile. Google with Android Q and Apple with iOS 13.  Have a quick look at the new features of iOS 13 and of Android Q.

I work on Xamarin Forms and I was eager to test my app on the new releases. So these steps are specific to Xamarin development with Visual Studio on Windows and Visual Studio for Mac.

Let's start with iOS.

iOS 13 Setup

To use the iOS 13 simulator, you would need to install xCode 11 beta. You can download this from the apple developer website. 

Once you install the XCode 11 beta you can configure the new simulators for iOS 13. 

If you open with Visual Studio it will not show you the iOS 13 simulators for your iOS project. To make them available, we would need to change the SDK to point to xCode-beta. To change the SDK location Goto Visual Studio - Preferences - Projects - SDK Locations - Apple and change the Xcode location to xCode 11 beta as shown in the image below. 

Apple SDK location

You will need to restart Visual Studio.

Wednesday, October 16, 2013

What's New in XCode 5 Part 3

What's New in XCode 5 Part 3

This is part 3 of the Series What's New in XCode 5. In Part 2 we had discussed what's new in
  • Interface Design
  • Compiler and Language
  • Auto Vectorizer
  • Parsing the Comments
XCode 5 is so fast and stable as the transition from GC to ARC has be completed with XCode 5... :) It has been 2 years since ARC has come into picture and has been great thing happen. Apple has announced that GC is deprecated technology and those developers who are using GC in Mac should switch to ARC and they will notice the change. Besides this enhancement there are still other features which we need to discuss..

XCode Debugger

Similar to Compiler XCode supported two different Debuggers "LLDB" and "GDB". Now with XCode 5 only LLDB debugger is available and gdb is gone. New features of debugger are:

Monday, October 14, 2013

What's New in XCode 5 Part 2

What's New in XCode 5 Part 2

In Part 1 of this series we had discussed about:
  • Documentation
  • Performance
  • SourceControl
  • Automatic Configuration
in XCode 5. Today we will discuss about more in features of XCode 5.

Monday, October 7, 2013

What's New in XCode 5 Part 1

What's New in XCode 5 Part 1

As we all know that iOS 7 has been the major release of iOS and it has brought us developers so many cool features. iOS 7 What's a new link to know more about it.

iOS 7 release brings the release of iOS 7 SDK and XCode 5, iOS 7 SDK and Xcode 5 download is available. Once you download and Installed XCode 5 you must be wondering now what's new in this... Right? That's what we are going to discuss in this very Blog.. and of course as IDE XCode do a lot of things and you know being a programmer it won't be easy to explain all the things in place. :) So I will be writing series of blogs to cover the features and this is the Beginning.. :) 

So What's New in XCode 5? Here are features: