URL Scheme is not working in SwiftUI – Solution

I just tried using URL Scheme in my Xcode project to open my iOS application from the browser through URL and found an error like this: “safari cannot open the page because the address is invalid”.

However, I somehow managed to find out how to fix it.

I fill the URL Types form, in the info tab properly. Added correct identifier name and a URL Schemes name as well. I put heyThere in URL Schemes field box.

Then after running the project, I tried opening Safari browser and when I tried to open this URL: heyThere://

As per my knowledge, it should prompt a message saying “open this page in your app (App name should appear here)”

But instead of that, I got an error that URL is invalid.

In another tutorial I discussed Open a SwiftUI app from URL link using URL Scheme

Reason for “URL Scheme is not working”

When we make a project and run that on either a physical device or a simulator, it will also add a list of URL patterns (If there are any in that newly installed app) to its OS.

But if we are getting that error, that means during installation it did not recognize and add that URL type in the OS.

I will tell you the possible reason for that as well as the specific reason for which I got that error.

  1. You are using iOS version 17.o or higher.
  2. While installing the App, URL pattern is not added to the whitelist of your OS properly.
  3. You should mispelled the URL. ( I am ignoring this for now as you can avoid it by cross-checking it)

Solution: safari cannot open the page because the address is invalid for URL Scheme

  1. Close the XCode and simulator too. Run everything again from the beginning.
  2. If the first method does not work, then follow this:
    import SwiftUI
    
    @main
    struct MyApp: App {
        @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
        var body: some Scene {
            WindowGroup {
                ContentView()
            }
        }
    }
    
    class AppDelegate: NSObject, UIApplicationDelegate {
        func application(
            _ application: UIApplication,
            didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
        ) -> Bool {
            return true
        }
    
        func application(
            _ app: UIApplication,
            open url: URL,
            options: [UIApplication.OpenURLOptionsKey : Any] = [:]
        ) -> Bool {
            // Handle the URL here
            print("Opened with URL: \(url)")
            return true
        }
    }
    

    You can add this code to your @main file.

    @main app file SwiftUIThe location of the file is mentioned in the above image. The file name ends with App by default.

  3. It’s not necessary but once I added this, it solved my problem and then I removed the code and there was no issue.
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate this line and the Class AppDelegate needs to be added and the rest of the code is predefined and created when creating a new project.

What I faced

I used the same method like adding an identifier name and URL Schemes name in the info tab or info.plist but it was working fine only on the older versions of iOS.

I am using iOS 17+ and it did not work in the first attempt but when I tried the above-mentioned methods, it’s working fine now.

Leave a Reply

Your email address will not be published. Required fields are marked *