Get Started with iOS Authentication using SwiftUI
Tags: security, mobile, swift
Published: 2022-03-31 16:00:52

### If you’re an iOS developer who’s new to SwiftUI and authentication with Auth0, you’re in for a treat. In this tutorial, you’ll build an app in SwiftUI that implements basic login and logout using Auth0.

### <br>

If you’re an iOS developer who’s new to SwiftUI and authentication with Auth0, you’re in for a treat. In this tutorial, you’ll build an app in SwiftUI that implements basic login and logout using Auth0!

**This tutorial covers using version 2.0.0 of the Auth0.swift SDK to implement login/logout in an iOS app written using the Swift programming language and the SwiftUI framework.** In the process, you’ll get a quick introduction to the basics of our newly revised Swift SDK and how to use it with Apple’s SwiftUI framework. You’ll also become familiar with the Auth0 dashboard and learn how to use it to register applications and manage users.

> **Look for the 🛠 emoji if you’d like to skim through the content while focusing on the build and execution steps.** 


## Apple’s SwiftUI Framework

<img src="https://images.ctfassets.net/23aumh6u8s0i/4jOxDICikkdQLFNtPKaNSp/1356c0c9d7415de3ea7164671699d968/swiftui_logo.png" align="right" alt="SwiftUI logo" width="200" />[SwiftUI](https://developer.apple.com/xcode/swiftui/) is a user interface toolkit and “the new way” for building apps that Apple introduced in 2019. With SwiftUI, you build user interfaces using Swift code with a _declarative_ approach, which means that you specify how the UI should look and behave under different states. You don’t have to specify how the UI moves between those states — it takes care of that for you. If you know [React](https://reactjs.org/) programming, you’ll find SwiftUI familiar.

The SwiftUI approach is quite different from the one used by [UIKit](https://developer.apple.com/documentation/uikit), iOS’ original UI toolkit, which dates back to 2007. With UIKit, you build user interfaces using Interface Builder, a drag-and-drop layout tool to build the app’s views graphically. You connect UI elements to variables and methods in the app’s view controllers using outlets and actions. UIKit uses an imperative approach, which means that you define how the program moves between states and how the UI should look and behave in those states.


## Auth0 and the *Auth0.swift* SDK

<img src="https://images.ctfassets.net/23aumh6u8s0i/2l2T8DQNjSXDHljZAJxlXa/ac5177aca992db4ab351d96f18a6f859/auth0_shield.png" align="left" alt="Auth0 logo" width="200" />Adding login and logout to an app may seem like a simple task — until you try it. You have to handle the many ways to log in, confirm email addresses and passwords, manage users, and handle security and scalability. Each of these issues has dozens of considerations, risks, issues, and edge cases.

Auth0 solves this problem. With Auth0 and a few lines of code, your app can have a full-featured system that supports logging in with a username/password combination, single sign-on and social accounts, passwordless login, biometrics, and more. You won’t have to handle the “behind the scenes” issues, either! Instead, you can focus on your app’s main functionality.

[*Auth0.swift*](https://auth0.com/docs/libraries/auth0-swift) is the Auth0 SDK for all Apple platforms: not just iOS, but macOS, iPadOS, watchOS, and tvOS. It’s our third most-used SDK, accounting for 11% of all API requests made to our systems. If you’re building an Apple device application that needs to authenticate or authorize its users, you need this SDK!

The newest version of *Auth0.swift*, version 2.0.0, incorporates what we’ve learned from securing applications on Apple devices over the past five years. If you’ve used earlier versions, you’ll appreciate its updated error handling, support for [async/await](https://docs.swift.org/swift-book/LanguageGuide/Concurrency.html) and [Combine](https://developer.apple.com/documentation/combine), improved default configuration, new documentation, and that we removed deprecated methods and legacy features. If you’re new to Auth0, you’ll be pleasantly surprised at how little code you’ll need to write to add authentication to your apps.


## What You’ll Build

In this tutorial, you’ll build a simple, single-screen iOS app that will allow the user to log in and log out using Auth0. While logged in, the screen will have a different appearance and show the user their name, email address, and photo from their profile.

Instead of building the app from a starter project, you’ll build it “from scratch,” starting with selecting **File** → **New** → **Project…** from Xcode’s menu bar.

[Read more...](https://auth0.com/blog/get-started-ios-authentication-swift-swiftui-part-1-login-logout/?utm_source=content_synd&utm_medium=sc&utm_campaign=swift)