Deep link URL Scheme doesn't call any function in iOS App Delegate
Asked Answered
D

5

14

im implementing deep link is iOS. I have configured the URL Scheme in Project-Setting->Info->Url type URL Schemes : carwash role:Viewer

when I type carwash://something the browser asks for opening the application but nothing get called in Application that I handle that what action should occur .

apple documentation says you should override application(open url) in AppDelegate but deep link dosent call it and the application opens in last state

application:openURL:options:' is not being called

this is my code and dose not work

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

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    if let url = launchOptions?[UIApplication.LaunchOptionsKey.url] as? URL {
        /// some
        fatalError()
    }
    GMSServices.provideAPIKey("")

    return true
}
Dispersoid answered 2/10, 2019 at 11:1 Comment(2)
You need to return true in your open url delegate method.Nab
Does this answer your question? Method 'application:openURL:options:' is not calledScabrous
D
15

In iOS 13, UIApplication application(_:open:options:) doesn't get called (at least in my case maybe).

You should override the SceneDelegate functions below instead:

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    if let url = URLContexts.first?.url{
        print(url) 
    }
}

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // detect the URLContext in the `options:` and deal with it.
}

If the user taps the link when your app isn't running, scene(_: openURLContexts:) won't be called but scene(_:willConnectTo:options:) will be.

Dispersoid answered 6/10, 2019 at 6:46 Comment(4)
Half right. That won't work if the URL arrives when your app isn't running.Carabin
then what do you suggest ?Dispersoid
It's not a matter of what I "suggest", it's a fact. You have to implement scene(_:willConnectTo:options:) to detect the URLContext in the options: and deal with it. Otherwise, if the user taps the link when your app isn't running, you won't respond to it. Try it and you'll see.Carabin
Thank you crystal sphere matt. Really helpful comment.Lactose
B
2

in iOS 13+ or scene delegate there two methods will call and willPerformHTTPRedirection method will definitely call.

  func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
        if userActivity.activityType == NSUserActivityTypeBrowsingWeb, let url =      userActivity.webpageURL {
          
        }
      }
    }

 func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Swift.Void) {
            if let url = request.url {
               guard let detailDictionary = String.queryParameters(from: url) else { return }

            }
        }
    }

Parse url by this function:

static func queryParameters(from url: URL) -> [String: String]? {
    let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false)
    var queryParams: [String : String]? = nil
    if let components = urlComponents?.queryItems {
      queryParams = [String: String]()
      for queryItem in components {
        if queryItem.value == nil {
          continue
        }
        queryParams?[queryItem.name] = queryItem.value
      }
    }
    return queryParams
  }
Blackness answered 18/5, 2021 at 19:42 Comment(1)
queryParameters is helpful to me for new version of firebaseDissociable
P
0

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

Note that you have no return statement in your method

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    print("Got from URL: \(url)"
    return true
}

Links in simulator best to be done through terminal

xcrun simctl openurl booted “carwasher://deeplink”
Peplum answered 2/10, 2019 at 13:9 Comment(2)
thank you for answering . I copied your code but the app opens through deep link but that function doesn't get called and nothing will be printedDispersoid
Try change role in Info tab from Viewer to EditorPeplum
R
0

For me in iOS13+ the following works (in my SceneDelegate):

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Set up everything you need
    // ...
    
    // Handle deep link on cold start
    if let url = connectionOptions.userActivities.first?.webpageURL {
        handle(url: url)
    }
}

func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
    if let url = userActivity.webpageURL {
        // Handle deep link when the app is running
        handle(url: url)
    }
}
Renferd answered 30/4, 2021 at 17:19 Comment(0)
L
-1

You should configure your deeplinks in Info.plist file

Example:

<key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeRole</key>
            <string>Viewer</string>
            <key>CFBundleURLName</key>
            <string>carwash</string> // your identifier
            <key>CFBundleURLSchemes</key>
            <array>
                <string>carwash</string> // schema
            </array>
        </dict>
    </array>
Lachrymal answered 2/10, 2019 at 11:17 Comment(1)
Its Already Declared . I can Open The App With deep link but I can't handle itDispersoid

© 2022 - 2024 — McMap. All rights reserved.