0

I've been having issues getting my custom URL to work right in my iOS app.
When I open this url bdat://hello in Safari, it transitions back to my app, but doesn't call the AppDelegate method.

Here's the method and relevant Info.plist data. I broke it down to this simple case, because I was seeing the same issue with the Spotify API.
Is there some other setting I'm missing?

// AppDelegate.swift
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    print(url)
    return true
}
// Info.plist
<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>CFBundleURLName</key>
        <string>com.bdat.test</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>bdat</string>
        </array>
    </dict>
</array>
LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174
Hethcox
  • 88
  • 2
  • 7
  • Are you sure your app is running? Maybe `application(_:didFinishLaunchingWithOptions:)` gets called instead. – Sulthan Jan 08 '20 at 22:52
  • ...and if it is running, check that the app is connected to Xcode. You need change how the app launches (not automatically, but on url trigger) and then hit run, then open the url. – LinusGeffarth Jan 08 '20 at 23:12
  • "This method is not called if your implementations return false from both the application(_:willFinishLaunchingWithOptions:) and application(_:didFinishLaunchingWithOptions:) methods." <-- make sure you have one of them, and it returns true – timbre timbre Jan 08 '20 at 23:52
  • Thanks for the suggestions. didFinishLaunchingWithOptions is getting called when the app launches and I return true. I changed the scheme to 'Wait for executable to be launched'. I'm still seeing the same behavior. If I understand correctly, the CFBundleURLName doesn't make much difference, but it is the same as my bundle ID. – Hethcox Jan 09 '20 at 02:14
  • @Hethcox Hi this method working or not ??? open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] – Himanshu Patel Jan 09 '20 at 04:41
  • 1
    @Hethcox I've checked your code and settings for plist and everything works correctly (the method `func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool` works). Very strange. – Vadim Nikolaev Jan 09 '20 at 08:13
  • Hi Himanshu. I'm not sure I understand your question. The code above shows the signature of the method in my AppDelegate. I keep wondering if there's something wrong with the signature so the OS isn't calling it. But the signature was created by XCode and matches the documentation. – Hethcox Jan 09 '20 at 14:49
  • @VadimNikolaev Thanks for trying that. I’m not sure what’s going on. – Hethcox Jan 13 '20 at 01:24

1 Answers1

-1

The app in this question was created in XCode 11.x as a test to determine why my real app wasn't working. When XCode generated the app it created a SceneDelegate and the entries in Info.plist to activate it. Apple's documentation (https://developer.apple.com/documentation/uikit/app_and_environment/scenes/specifying_the_scenes_your_app_supports_) says you must explicitly opt-in, but apparently not. Even though 'Supports multiple windows' is unchecked it was calling scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) in the SceneDelegate that it generated. I'm not sure if this is intentional or a bug in iOS/XCode.

Hethcox
  • 88
  • 2
  • 7
  • It's good that you woke up to what's been going on since last June. :) But that won't work if your app was not running when the link was tapped. You have to implement `willConnectTo` as well. – matt Jan 30 '20 at 02:39
  • As for "intentional or bug" it's totally intentional. You might not opt in to multiple windows but if you have a scene delegate entry at all in the Info.plist you've opted in to the scene delegate architecture. They are two different (though related) things. Again, this has been the way of things for 8 months now. – matt Jan 30 '20 at 02:41
  • Thanks @matt. The app I'm trying to update doesn't call the AppDelegate method and doesn't have a SceneDelegate. It just doesn't notify my app at all. That's what I consider a little buggy. – Hethcox Jan 30 '20 at 16:12
  • Hi @Hethcox. — You said you created the app in Xcode 11. Okay, so when you did that, it had a scene delegate. What happened to it? Or maybe I've totally misunderstood???? – matt Jan 30 '20 at 16:15
  • Hi @matt. I'm working on resurrecting a legacy app that was written four years ago. I had to upgrade Swift and a bunch of API stuff, but got stuck when the URL callback wasn't getting called when a 3rd-party authorization window redirected back to my app. This was so old that it didn't have a SceneDelegate. Because of its size I created a smaller test app in XCode 11 and that was what led to this question. The new, test app has a SceneDelegate, but all I noticed was that the AppDelegate method didn't get called in either app. The help here eventually helped me find the problem in the test app. – Hethcox Feb 02 '20 at 00:17
  • ...and I implemented SceneDelegate in the old app and it's working too. – Hethcox Feb 02 '20 at 00:21
  • That doesn't make sense to me. It's all about the Application Scene Manifest. The old app presumably does not have an Application Scene Manifest in its _Info.plist_, or any scene session methods in the app delegate, so any scene delegate you give it should be completely nonfunctional. – matt Feb 02 '20 at 00:39
  • Could you show the old app's _Info.plist_ and the entirety (or at least all method declarations) of its AppDelegate? Otherwise I don't see how we can explain this. – matt Feb 02 '20 at 00:45