Thank you very much for this detailed answer!
I double checked my code against your suggestions and read the documentation with all the edge cases but I have not been able to get it to work.
I'm a bit confused by what you're saying here. That's the function the system uses to pass URLs into your app. Why haven't you implemented it?
Sorry for the misunderstanding. What I meant to say is that I implemented this function but it is not being called.
Basically I implemented the functions from which I would expect the url variables and a ContentView struct then proceeds to display whatever it is that has been written to AppState.shared.startParam and it only ever displays: URL Parameters: launchOptions:nil.
[quote='835877022, DTS Engineer, /thread/781255?answerId=835877022#835877022']
First, the "nil" argument is probably caused by this:
"If the app supports scenes, this is nil. For information about the possible keys in this dictionary and how to handle them, see UIApplication.LaunchOptionsKey."
[/quote]
The App does not support scenes.
[quote='835877022, DTS Engineer, /thread/781255?answerId=835877022#835877022']
Finally, application(_:open:options:) add it's own layer of complications:
[/quote]
I return true in all the functions. And the app does not support scenes.
CustomURLNativeIOSApp.swift:
import SwiftUI
class AppState: ObservableObject {
static let shared = AppState()
@Published var startParam: String = "No URL parameters detected yet"
private init() {}
}
@main
struct iOSApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self)
var appDelegate: AppDelegate
@ObservedObject private var appState = AppState.shared
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(AppState.shared)
}
}
}
//customurlios://open?test=things
class AppDelegate: NSObject, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]?) -> Bool {
print("URL received launchOptions:", launchOptions ?? [:])
let param = "launchOptions:\(String(describing: launchOptions))"
AppState.shared.startParam = param
print("Set startParam to: \(param)")
return true
}
func application(_ application: UIApplication, handleOpen url: URL) -> Bool {
print("URL received handleOpen:", url.absoluteString)
let param = "handleOpen: \(url.absoluteString)"
AppState.shared.startParam = param
print("Set startParam to: \(param)")
return true
}
func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
print("URL received open url:", url.absoluteString)
let param = "url: \(url.absoluteString) options:\(options.count)"
AppState.shared.startParam = param
print("Set startParam to: \(param)")
return true
}
}
ContentView.swift:
import SwiftUI
struct ContentView: View {
@State private var showContent = false
@EnvironmentObject var appState: AppState
var body: some View {
VStack {
Button("Click me!") {
withAnimation {
showContent = !showContent
}
}
Text("URL Parameters: \(appState.startParam)")
.font(.system(size: 14))
.padding()
.background(Color.gray.opacity(0.1))
.cornerRadius(8)
if showContent {
VStack(spacing: 16) {
Image(systemName: "swift")
.font(.system(size: 200))
.foregroundColor(.accentColor)
Text("SwiftUI: Hello")
}
.transition(.move(edge: .top).combined(with: .opacity))
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
.padding()
.onAppear {
// Print the current value for debugging
print("ContentView appeared, startParam: \(appState.startParam)")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.environmentObject(AppState.shared)
}
}
Info.plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>CFBundleURLName</key>
<string>dev.customurl.ios</string>
<key>CFBundleURLSchemes</key>
<array>
<string>customurlios</string>
</array>
</dict>
</array>
</dict>
</plist>
Still cannot find my mistake...