llfbandit / app_links

Android App Links, Deep Links, iOs Universal Links and Custom URL schemes handler for Flutter.
https://pub.dev/packages/app_links
Apache License 2.0
176 stars 68 forks source link

[proposal] add `setInitialLink` setter on iOS #49

Closed LeGoffMael closed 7 months ago

LeGoffMael commented 1 year ago

This PR is made in order to fix #47.

I'm not sure where this problem really originated, and it's likely not directly from this library. But this PR offers an easy to use solution for people facing this problem (i.e. me for now).

Instead of using this solution, this PR would be quite easier :

override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    // .. HERE do other stuff if needed

    // Custom URL
    if let url = launchOptions?[UIApplication.LaunchOptionsKey.url] as? URL {
      if(url.absoluteString.hasPrefix("myapp://")) {
        SwiftAppLinksPlugin.setInitialLink(url: url)
      }
    }
    // Universal link
    else if let activityDictionary = launchOptions?[UIApplication.LaunchOptionsKey.userActivityDictionary] as? [AnyHashable: Any] { 
      for key in activityDictionary.keys {
        if let userActivity = activityDictionary[key] as? NSUserActivity {
          if let url = userActivity.webpageURL {
            if(url.absoluteString.hasPrefix("https://mywebsite.com")) {
              SwiftAppLinksPlugin.setInitialLink(url: url)
              break
            }
          }
        }
      }
    }

    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }

As I said, the direct cause of this problem has not been identified, so I will understand if you wish to reject this PR. Also, I don't know much about swift development so this might be optimizable.

Imgkl commented 1 year ago

@LeGoffMael What's the import you used in your app? Because when I add

// Custom URL
    if let url = launchOptions?[UIApplication.LaunchOptionsKey.url] as? URL {
      if(url.absoluteString.hasPrefix("myapp://")) {
        SwiftAppLinksPlugin.setInitialLink(url: url)
      }
    }

it says Swift Compiler Error (Xcode): No such module 'SwiftAppLinksPlugin'

LeGoffMael commented 1 year ago

@Imgkl just add import app_links at the top of the file

llfbandit commented 7 months ago

Thanks for your contribution. app_links 3.5.0 now allows handling links with custom AppDelegate.