avioli / uni_links

Flutter plugin for accepting incoming links.
BSD 2-Clause "Simplified" License
563 stars 303 forks source link

Not working with Flutter add-to-app in iOS #188

Closed samarthagarwal closed 5 months ago

samarthagarwal commented 1 year ago

I am using Flutter add-to-app to add flutter as a module to an iOS app. I have used the plugin to get the initial link and the stream updates as well. Here is the basic code.

Future<void> initUniLinks() async {
    final _appLinks = AppLinks();
    final initialLink = await _appLinks.getInitialAppLinkString();

    print('initialLink: ${initialLink ?? "no initial link"}');
    _streamController.add(initialLink ?? "/");
    // Attach a listener to the stream
    _sub = _appLinks.allStringLinkStream.listen((String? link) {
      print(link ?? "no link");
      _streamController.add(link ?? "/");
    }, onError: (err) {
      print(err);
    });
  }

I am using the allStringLinkStream method to listen to the new links but I never get any updates. The app launches but the link is never received so the app stays on the root URL.

Here is some of my iOS code that I am using to render the Flutter app inside the iOS SwiftUI app.

class AppDelegate: FlutterAppDelegate, ObservableObject {
  let flutterEngine = FlutterEngine(name: "my flutter engine")

  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
      // Runs the default Dart entrypoint with a default Flutter route.
      flutterEngine.run();
      // Used to connect plugins (only if you have plugins with iOS platform code).
      GeneratedPluginRegistrant.register(with: self.flutterEngine);
      return true;
    }
}

@main
struct MyApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

    var body: some Scene {
        WindowGroup {
             ContentView()
        }
    }
}

struct ContentView: View {

    @EnvironmentObject var appDelegate: AppDelegate

  // Button is created to call the showFlutter function when pressed.
  var body: some View {
    Button("Show Flutter!") {
      showFlutter()
    }
  }

func showFlutter() {
    // Get the RootViewController.
    guard
      let windowScene = UIApplication.shared.connectedScenes
        .first(where: { $0.activationState == .foregroundActive && $0 is UIWindowScene }) as? UIWindowScene,
      let window = windowScene.windows.first(where: \.isKeyWindow),
      let rootViewController = window.rootViewController
    else { return }

    // Create the FlutterViewController.
    let flutterViewController = FlutterViewController(
      engine: appDelegate.flutterEngine,
      nibName: nil,
      bundle: nil)
    flutterViewController.modalPresentationStyle = .overCurrentContext
    flutterViewController.isViewOpaque = false

    rootViewController.present(flutterViewController, animated: true)
  }
}

I have also added the required entries to info.plist file as well and the custom scheme is registered. Whenever I try to open a custom scheme url, it launches the app but never receives the link information in the Flutter code.

avioli commented 5 months ago

This package is now marked as discontinued and app_links recommended replacement.