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.
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,...
When you profile your app with the Allocations instrument, you may want to find the largest memory allocations your app makes. Take the following steps to find the largest memory allocations: Press Cmd-3 to open the allocations list. Click the Size column heading to sort the allocations by size. Choose All Heap Allocations from the Allocation Type menu in the bottom bar to hide virtual memory allocations. Your code doesn’t directly make virtual memory allocations. The Allocation Type menu is...
A common problem people run into when they start profiling their apps with Instruments is finding the code that is causing problems. Many instruments initially show general statistics instead of statistics about the code you wrote. For example the Allocations instrument initially shows the number of memory allocations and amount of allocated memory for hundreds of memory categories. If your app allocates a lot of memory, you want to find the code that allocates high amounts of memory. How do...