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

Is here a macos universal links support? #121

Closed DmitryGaimaldinov closed 1 month ago

DmitryGaimaldinov commented 1 month ago

Describe the bug

I make an application which supports Android, IOS, MacOs and Windows. Universal links https://... work perfectly fine on Android and IOS but don't work for MacOS. They just open an application but it feels like there's not handler for the universal link. At the same time app links customScheme://... work on MacOS. I test everything after making a dmg and installing it.

I wish to know if this plugin supports universal links in MacOs. Because even in readme there's only example with custom app links scheme.

sample://foo/#/book/hello-world2

My code:

class AppLinksService extends ChangeNotifier {
  late StreamSubscription _sub;
  static final AppLinks _plugin = AppLinks();

  AppLinksService._() {
    _sub = _plugin.uriLinkStream.listen(_onLinkReceived);
  }

  void _onLinkReceived(Uri? link) {
    notifyListeners(); // does not work
  }

Info plist:

  ...
  <key>CFBundleURLTypes</key>
  <array>
      <dict>
          <key>CFBundleURLName</key>
          <string>portal</string>
          <key>CFBundleURLSchemes</key>
          <array>
              <string>custom-scheme</string>
              <string>https</string> // I also tried without this one but there no effect if "https" included or not here
          </array>
      </dict>
  </array>

DebugProfile.entitlements and Release.entitlements:

<key>com.apple.developer.associated-domains</key>
<array>
    <string>applinks:my.domain.com</string>
</array>

apple-app-site-assiciation also set up correctly because IOS universal linking works.

{
  "applinks": {
    "apps": [],
    "details": [
      {
        "appID": "...",
        "paths": [
          "NOT /_/*",
          "/*"
        ]
      }
    ]
  }
}

flutter doctor

[✓] Flutter (Channel stable, 3.19.3, on macOS 14.4.1 23E224 darwin-x64, locale ru-RU)
    • Flutter version 3.19.3 on channel stable at /Users/dima/development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ba39319843 (10 weeks ago), 2024-03-07 15:22:21 -0600
    • Engine revision 2e4ba9c6fb
    • Dart version 3.3.1
    • DevTools version 2.31.1

[✓] Xcode - develop for iOS and macOS (Xcode 15.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15E204a
    • CocoaPods version 1.15.2

[✓] Connected device (2 available)
• macOS (desktop) • macos  • darwin-x64     • macOS 14.4.1 23E224 darwin-x64
• Chrome (web)    • chrome • web-javascript • Google Chrome 121.0.6167.184

Does it related to

[X] App Links (Android)
[X] Deep Links (Android)
[X] Universal Links (iOS)
[X] or Custom URL schemes? (iOS)

Does the example project work?

[V] Yes
[ ] No
[ ] Irrelevant here

Did you fully read the instructions for the targeted platform before submitting this issue?

Uploaded your files to webserver, HTTPS, direct connection, scheme pattern setup, ...

[V] Yes
[ ] No
[ ] Irrelevant here

DmitryGaimaldinov commented 1 month ago

Related Issue: https://github.com/llfbandit/app_links/issues/36

llfbandit commented 1 month ago

Thanks for the report. There's an attempt here https://github.com/llfbandit/app_links/tree/macos_uni This is latest version 6.0.2 plus universal link (untested) support on macOS. Feedback appreciated!

DmitryGaimaldinov commented 1 month ago

Thank you very much for such a rapid response! I've tested this but unfortunately universal link still just opens an application and the link is not handled by flutter. It seems to me there has to be NSUserActivityTypeBrowsingWeb handling https://developer.apple.com/documentation/xcode/supporting-universal-links-in-your-app. This is done in IOS codebase of this plugin and, maybe, we have to add the same for macos

My pubspec.yaml:

  app_links:
    git:
      url: https://github.com/llfbandit/app_links.git
      ref: macos_uni
llfbandit commented 1 month ago

Can you ensure that your setup is correct from outside the app by using swcutil? (doc).

If this is OK, you can try with new commits on same branch. You'll have to add this in your own macOS AppDelegate (plugins don't have access to it like on iOS...):

public override func application(_ application: NSApplication,
                                 continue userActivity: NSUserActivity,
                                 restorationHandler: @escaping ([any NSUserActivityRestoring]) -> Void) -> Bool {

  guard let url = AppLinks.shared.getUniversalLink(userActivity) else {
    return false
  }

  AppLinks.shared.handleLink(link: url.absoluteString)

  return false
}
llfbandit commented 1 month ago

Related Flutter issue: https://github.com/flutter/flutter/issues/148233

DmitryGaimaldinov commented 1 month ago

Thank you so much, everything works with this setup! The only additional line of code for macos AppDelegate is import app_links (for future readme update). So full code is:

import app_links

public override func application(_ application: NSApplication,
                                 continue userActivity: NSUserActivity,
                                 restorationHandler: @escaping ([any NSUserActivityRestoring]) -> Void) -> Bool {

  guard let url = AppLinks.shared.getUniversalLink(userActivity) else {
    return false
  }

  AppLinks.shared.handleLink(link: url.absoluteString)

  return false
}

It would be nice to have same support on Windows. I'm ready to help cuz it's a quite priority task in my job now

llfbandit commented 1 month ago

Released in 6.1.0.