I am in the process of developing a business app for iOS using SwiftUI, that will have CRUD operations to logged in users. Most of the functionality will be provided using a webview (with WKWebView), while also taking advantage of device features such as push notifications.
(For now, the webview is necessary until everything can be implemented using native SwiftUI code)
My main issue is sharing session data between the native SwiftUI code, and the webview–
I need this for stuff like push notifications, which need to send the device token to the backend.
I am wondering how to architect the entire thing in the cleanest way possible and have found two ways to implement the login flow:
Option #1: Native Login
Building a native login form that sends an API request to the backend, and returns a token that will be stored in the Keychain. This token serves as the way to identify users and provide access. Then, sending a querystring with a temporary token when opening the webview, so that the user is also identified via cookies.
This option has its pros an cons:
- Securely save the session token in the Keychain, refreshing it when necessary
- Passing a (possibly insecure) token to the web app, in order to authenticate the webview
Overall, seems like a solid option but due to my inexperience I’m not sure if it’s a proper way to go around it.
Option #2: Webview Login
The other option is to turn the entire app into a webview, including login information. The webview seems to retain login sessions quite well with cookies, so it would basically be a web app. Then the native code could read information from the webview in order to extract a user token, and use it to authenticate push notifications and such.
This option has some issues:
- Delegating login to the webview seems easier, since it falls on the web app
- It feels clunky to extract a user token in order to identify push notifications
What would be the best way to go around it? How is this native-to-webview communication usually implemented? I’ve only just started dabbling in iOS and SwiftUI, so the architecture side of things is still a bit foreign to me.