Flutter社交类APP用户资料分享功能实现方案咨询(禁用已废弃的Uni Links/Dynamic Links)
Hey there, let's work through this profile sharing challenge you're dealing with. You want shared profiles to open directly in your app if it's installed, or redirect to the Play Store if not—all without relying on deprecated tools like the Uni Links package or Firebase Dynamic Links. I’ve got a couple of reliable approaches to help you pull this off, plus a fix for the share_plus issue you ran into.
方案1:自定义Scheme + 中转网页(通用跨平台方案)
This is a tried-and-true method that works across both Android and iOS, even if you don’t have a fully set-up backend for system-level deep links yet. Here’s how it breaks down:
- You’ll create a custom app scheme (like
myapp://profile/123) that your app registers to recognize. - Instead of sharing the scheme directly (which some social apps might block or not handle well), you share a simple HTTPS redirect page. This page first tries to wake up your app via the custom scheme; if that fails (meaning the app isn’t installed), it automatically sends the user to the Play Store.
Step-by-step setup:
- Register the custom scheme in your app
- For Android: Add this intent filter to your
AndroidManifest.xmlinside your main activity tag:<intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="myapp" android:host="profile" /> </intent-filter> - For iOS: Add this to your
Info.plistto register the scheme:<key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLSchemes</key> <array> <string>myapp</string> </array> <key>CFBundleURLName</key> <string>Profile Sharing</string> </dict> </array>
- For Android: Add this intent filter to your
- Build the redirect webpage
Host a simple HTML file on your domain (e.g.,https://yourapp.com/share/profile/{user_id}) with this JS logic to handle the redirect:<!DOCTYPE html> <html> <body> <script> // Extract user ID from the URL path const userId = window.location.pathname.split('/').pop(); const appDeepLink = `myapp://profile/${userId}`; const playStoreLink = `https://play.google.com/store/apps/details?id=com.your.app.package`; // Try to open the app first window.location.href = appDeepLink; // If the app doesn't open after 500ms, redirect to Play Store setTimeout(() => { window.location.replace(playStoreLink); }, 500); </script> </body> </html> - Handle deep links in your Flutter app
Useurl_launcher(a maintained, non-deprecated package) to parse incoming links and navigate to the correct profile:import 'package:flutter/material.dart'; import 'package:url_launcher/url_launcher.dart'; final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>(); void main() async { WidgetsFlutterBinding.ensureInitialized(); // Check for initial deep link when app starts final initialLink = await getInitialLink(); if (initialLink != null) { _handleDeepLink(initialLink); } // Listen for subsequent deep links while app is running linkStream.listen((link) { if (link != null) _handleDeepLink(link); }); runApp(MyApp(navigatorKey: navigatorKey)); } void _handleDeepLink(String link) { final uri = Uri.parse(link); // For a link like myapp://profile/123, extract user ID if (uri.host == 'profile' && uri.pathSegments.isNotEmpty) { final userId = uri.pathSegments.first; // Navigate to the profile screen with this user ID navigatorKey.currentState?.pushNamed('/profile', arguments: userId); } } class MyApp extends StatelessWidget { final GlobalKey<NavigatorState> navigatorKey; const MyApp({super.key, required this.navigatorKey}); @override Widget build(BuildContext context) { return MaterialApp( navigatorKey: navigatorKey, routes: { '/profile': (context) => ProfileScreen(userId: ModalRoute.of(context)?.settings.arguments as String), }, ); } } - Share the redirect link with share_plus
This is where you probably went wrong before—instead of sharing plain text or a user ID, share the HTTPS redirect link:import 'package:share_plus/share_plus.dart'; void shareUserProfile(String userId) async { final shareUrl = 'https://yourapp.com/share/profile/$userId'; await Share.share( "Check out this cool profile: $shareUrl", subject: "User Profile" ); }
方案2:Android App Links + iOS Universal Links(无缝系统级体验)
If you have a registered domain, this method offers a smoother experience because it uses HTTPS links directly (no visible redirect page) and is natively supported by both platforms. Here’s how to set it up:
- Android App Links: Associates your HTTPS domain with your app, so when users click a link like
https://yourapp.com/user/123, Android automatically opens your app if installed, or loads the web page (which you can set to redirect to Play Store).- Host an
assetlinks.jsonfile athttps://yourapp.com/.well-known/assetlinks.jsonwith your app’s package name and signing fingerprint. - Add an intent filter to your
AndroidManifest.xmlto handle the HTTPS links:<intent-filter android:autoVerify="true"> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="https" android:host="yourapp.com" android:pathPrefix="/user" /> </intent-filter>
- Host an
- iOS Universal Links: Similar to App Links, this links your HTTPS domain to your iOS app.
- Host an
apple-app-site-associationfile (no file extension) athttps://yourapp.com/.well-known/apple-app-site-associationwith your app’s Team ID and Bundle ID. - Enable Associated Domains in Xcode for your project, adding
applinks:yourapp.com.
- Host an
- Handle links in Flutter: Same as the first scheme—parse the HTTPS link (e.g.,
https://yourapp.com/user/123), extract the user ID, and navigate to the profile screen using the sameurl_launcherlogic above.
Quick fix for your share_plus issue
You mentioned share_plus didn’t open the specific profile—this was almost certainly because you were sharing plain text (like "User ID: 123") instead of a link that triggers the deep link flow. By sharing the HTTPS redirect link (from Scheme + 中转页) or App Links/Universal Links URL, and setting up your app to parse those links, share_plus will work exactly as you need it to.
Pro Tips:
- Test deep links on real devices, not just emulators—emulators sometimes handle link wake-up logic differently than physical devices.
- For Android, verify your App Links in the Google Play Console to ensure they’re recognized as official links.
- If you don’t want to host a redirect page, you can use a free static site host (like GitHub Pages) to host the simple HTML file.
内容来源于stack exchange




