Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deep link URL Scheme doesn't call any function(swift)

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
}
like image 685
Arvin Rezaei Avatar asked Oct 02 '19 11:10

Arvin Rezaei


Video Answer


2 Answers

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.

like image 107
Arvin Rezaei Avatar answered Oct 12 '22 10:10

Arvin Rezaei


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
  }
like image 1
SomuYadav Avatar answered Oct 12 '22 08:10

SomuYadav