【问题标题】:Flutter open deep link using packageFlutter 使用包打开深层链接
【发布时间】:2021-12-20 19:53:49
【问题描述】:

我正在使用以下颤振包,以允许我的用户打开 Spotify 应用程序: https://pub.dev/packages/external_app_launcher

当用户点击应用程序中的按钮时,他应该导航到 Spotify 深层链接。 如何使用上面的包实现它?

深层链接应该是这样的: https://open.spotify.com/playlist/37i9dQZF1DWX5ZOsG2Ogi1?si=41c6a392cf7d4a2b

但是,我目前的实现如下:

await LaunchApp.openApp(
  androidPackageName: 'com.spotify.music',
  iosUrlScheme: 'spotify:',
);

我该怎么做?

【问题讨论】:

    标签: flutter


    【解决方案1】:

    恕我直言,该软件包不支持它。您可以先了解原生普通 iOS 应用如何打开深度链接。然后写下原生 ios (swift/objc) 代码。然后用简单的 Flutter 代码包装它,这样你就可以从 Flutter 中调用它(flutter 有一些关于这种情况的官方文档 - 开发一个包)。

    您也可以尝试将https://open.spotify.com/playlist/37i9dQZF1DWX5ZOsG2Ogi1?si=41c6a392cf7d4a2b 视为“普通” url,并使用flutter在外部打开它。然后ios可能会打开应用程序而不是浏览器。

    【讨论】:

      【解决方案2】:

      您的包用于打开应用程序而不是打开深层链接。深度链接基本上只是打开一个 URL,如果可用,它将将您重定向到其关联的应用程序。

      因此,要打开深层链接,您应该使用包 url_launcher 并使用它启动您的 Spotify URL。

      这是一个关于 Android 的教程:

      第 1 步:向您的 AndroidManifest 添加权限

      您需要添加权限QUERY_ALL_PACKAGES,以便您可以使用canLaunch 验证手机上是否可以使用Spotify 应用程序。

      • android/app/src/debug/AndroidManifest.xml(如果您也希望它在调试模式下工作)
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.so_tests">
          <!-- Flutter needs it to communicate with the running application
               to allow setting breakpoints, to provide hot reload, etc.
          -->
          <uses-permission android:name="android.permission.INTERNET"/>
      
          <!-- Add this line -->
          <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>
      </manifest>
      
      • android/app/src/main/AndroidManifest.xml(用于发布)
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.so_tests">
          <uses-permission android:name="android.permission.INTERNET"/>
      
          <!-- Add this line -->
          <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>
         <application
              android:label="so_tests"
              android:icon="@mipmap/ic_launcher">
      
              <!-- TODO: Add your Google Maps API key here -->
              <meta-data android:name="com.google.android.geo.API_KEY"
                     android:value="YOUR-KEY-HERE"/>
                     
              <activity
                  android:name=".MainActivity"
                  android:launchMode="singleTop"
                  android:theme="@style/LaunchTheme"
                  android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
                  android:hardwareAccelerated="true"
                  android:windowSoftInputMode="adjustResize">
                  <!-- Specifies an Android theme to apply to this Activity as soon as
                       the Android process has started. This theme is visible to the user
                       while the Flutter UI initializes. After that, this theme continues
                       to determine the Window background behind the Flutter UI. -->
                  <meta-data
                    android:name="io.flutter.embedding.android.NormalTheme"
                    android:resource="@style/NormalTheme"
                    />
                  <!-- Displays an Android View that continues showing the launch screen
                       Drawable until Flutter paints its first frame, then this splash
                       screen fades out. A splash screen is useful to avoid any visual
                       gap between the end of Android's launch screen and the painting of
                       Flutter's first frame. -->
                  <meta-data
                    android:name="io.flutter.embedding.android.SplashScreenDrawable"
                    android:resource="@drawable/launch_background"
                    />
                  <intent-filter>
                      <action android:name="android.intent.action.MAIN"/>
                      <category android:name="android.intent.category.LAUNCHER"/>
                  </intent-filter>
              </activity>
              <!-- Don't delete the meta-data below.
                   This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
              <meta-data
                  android:name="flutterEmbedding"
                  android:value="2" />
          </application>
      </manifest>
      

      第2步:使用canLaunchlaunch

      这是一个基本的代码示例:

      import 'package:flutter/material.dart';
      import 'package:url_launcher/url_launcher.dart';
      
      class HomePage extends StatelessWidget {
        @override
        Widget build(BuildContext context) {
          return Scaffold(
            body: Center(
              child: ElevatedButton(
                onPressed: () async {
                  final spotifyUrl =
                      'https://open.spotify.com/playlist/37i9dQZF1DWX5ZOsG2Ogi1?si=41c6a392cf7d4a2b';
      
                  // Check if Spotify is installed
                  if (await canLaunch(spotifyUrl)) {
                    // Launch the url which will open Spotify
                    launch(spotifyUrl);
                  }
                },
                child: Text('Open Spotify'),
              ),
            ),
          );
        }
      }
      

      【讨论】:

      • IOS呢?
      • @TalRofe 我检查了 Spotify API 文档,它应该以相同的方式工作,只是我认为您不需要添加任何权限来查询设备上安装的软件包。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-21
      • 2023-01-25
      • 1970-01-01
      • 1970-01-01
      • 2023-01-04
      • 1970-01-01
      相关资源
      最近更新 更多