Sign in to websites in your SwiftUI appMany Swift apps require access to an online account. A person using your app must sign into a website to give your app access to the account. How do you let the person sign in from your app? Apple provides a class, You must perform the following tasks to use
Create a custom URL typeWhen someone finishes logging in to the website, they should go back to your app. You must add a URL type to your app to go back to the app. Select your app target from the project editor and click the Info button at the top of the project editor to add a URL type to the app. Give your URL type a name in the Identifier text field. Enter the URL type in the URL Schemes text field. The URL type should take the following form:
Create a class for your login sessionTo use
The The class must conform to the Conforming to The class needs a property of type Add a presentation anchorThe login session requires a presentation anchor, which is a window to show the login session so people can sign in. Add the following function to your login session class:
Create an ASWebAuthenticationSession instanceNow you can create an instance of
The callback URL is the custom URL type you created for the app, minus the
The following code creates the session:
Add a You should use ephemeral web browser sessions. If you set If you need to make any async calls in the completion handler, wrap them in a
Create a login viewAfter creating the class to handle the login session, you can create a SwiftUI view for people to sign in. The following code shows an example of a simple login view:
The view code uses the Additional readingThe following articles provide more information: |
Subscribe and get exclusive articles on Swift development, a free guide on moving from tutorials to making your first app, notices of sales on books, and anything I decide to add in the future.
TextEditingReference collection I am the new maintainer of TextEditingReference, a collection of open-source libraries for working with text in iOS and Mac apps. The libraries in the collection include the following: Markdown editing views Rich text editing views Text views with syntax highlighting Syntax highlighting libraries TextKit utility packages If you develop apps that involve editing more than one line of text, you may find a library or two in the TextEditingReference collection to...
Swift Concurrency Learning Resources One of the biggest pain points experienced Swift developers have is dealing with Swift concurrency, especially the changes in Swift 6. Many developers are sticking with Swift 5 to avoid the pain of migrating their code to Swift 6. If you are having trouble learning about Swift concurrency, I put together a list of resources that should help. F**king Approachable Swift Concurrency F**king Approachable Swift Concurrency is a good starting point to learn...
Find the Code Causing Long SwiftUI Updates with Instruments Instruments comes with a SwiftUI instrument to find performance problems in your SwiftUI apps. This article shows how to use Instruments to find the code that causes long SwiftUI view updates. If you have never used Instruments, read the following article to learn how to profile your app with the SwiftUI instrument: Find the SwiftUI Views that Update the Most Using Instruments Finding Long Updates When you finish profiling your app,...