Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS, Swift 5, AppDelegate open url / .onOpenUrl - how to find calling app that calls my custom scheme

Tags:

ios

swift

swiftui

So the old way of handling openURL calls in UIKit was to use the following in an AppDelegate:

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool { ...

Unfortunately the new @main SwiftUI this is redirected to

.onOpenUrl { url in ...

Thus we miss the dictionary OpenURLOptionsKey.sourceApplication which I would like to determine which app called mine, and then can make the appropriate actions as I have a use case that would require limiting certain scheme urls to be used by 3rd party apps and not others.

What's the best practice to do this? I've not been getting anywhere with the documentation, or searching here. It looked like I might have got a lead on it via onContinueUserActivity/NSUserActivityTypeBrowsingWeb but that doesn't get called either.

like image 707
oPless Avatar asked Sep 16 '25 04:09

oPless


1 Answers

So I managed to use this in the app delegate:

func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
    let sceneConfig = UISceneConfiguration(name: nil, sessionRole: connectingSceneSession.role)
    sceneConfig.delegateClass = SceneDelegate.self 
    return sceneConfig
}

Which sets this up in the scenedelegate:

    func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
       // get url and sourceApp out of first URLContext here
       }

Unfortunately, if the sender of the URL is NOT from your team, you'll get nil in the UIOpenURLContext.sourceApp

So if you're wanting to create RPCs between your teams apps, you're fine. If you want to communicate between apps from different teams, you'll have to figure out some other way to indicate which app it is and then apply your responding policy to that.

like image 55
oPless Avatar answered Sep 17 '25 20:09

oPless