Flutter iOS端使用receive_sharing_intent无法接收分享数据
解决iOS端receive_sharing_intent无法接收共享数据的问题
1. 补全Info.plist核心配置
iOS需配置URL类型和文档接收权限,才能让系统识别App可接收共享数据:
- 添加
CFBundleURLTypes,绑定App的Bundle ID作为URL Scheme - 添加
CFBundleDocumentTypes,声明可接收的文件/数据类型(文本、图片等)
示例配置(在Info.plist中添加):
<key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLSchemes</key> <array> <string>your-app-bundle-id</string> </array> </dict> </array> <key>CFBundleDocumentTypes</key> <array> <dict> <key>CFBundleTypeName</key> <string>Text</string> <key>LSHandlerRank</key> <string>Alternate</string> <key>LSItemContentTypes</key> <array> <string>public.text</string> </array> </dict> <dict> <key>CFBundleTypeName</key> <string>Images</string> <key>LSHandlerRank</key> <string>Alternate</string> <key>LSItemContentTypes</key> <array> <string>public.image</string> </array> </dict> </array>
2. 修复代码中的订阅覆盖问题
你的代码用同一个变量同时订阅文本流和媒体流,会导致第一个订阅被覆盖,仅最后一个订阅生效,需拆分两个独立订阅变量:
late StreamSubscription _textIntentSubscription; late StreamSubscription _mediaIntentSubscription; @override void initState() { super.initState(); // 监听前台文本流 _textIntentSubscription = ReceiveSharingIntent.getTextStream().listen( (String value) => _textReceiveIntent(value), onError: (err) => print("getLinkStream error: $err"), ); // 处理冷启动时的文本数据 ReceiveSharingIntent.getInitialText().then((String? value) => _textReceiveIntent(value)); // 监听前台媒体流 _mediaIntentSubscription = ReceiveSharingIntent.getMediaStream().listen( (List<SharedMediaFile> value) => _mediaReceiveIntent(value), onError: (err) => print("getIntentDataStream error: $err"), ); // 处理冷启动时的媒体数据 ReceiveSharingIntent.getInitialMedia().then((List<SharedMediaFile> value) => _mediaReceiveIntent(value)); } @override void dispose() { _textIntentSubscription.cancel(); _mediaIntentSubscription.cancel(); super.dispose(); }
3. 确认共享扩展的Xcode配置
若要接收其他App的共享数据,需确保Xcode项目中配置了Share Extension:
- 新增Share Extension目标,在其
Info.plist的NSExtension节点中,配置NSExtensionActivationRule声明支持的数据类型:
<key>NSExtension</key> <dict> <key>NSExtensionAttributes</key> <dict> <key>NSExtensionActivationRule</key> <dict> <key>NSExtensionActivationSupportsText</key> <true/> <key>NSExtensionActivationSupportsImageWithMaxCount</key> <integer>10</integer> </dict> </dict> <key>NSExtensionMainStoryboard</key> <string>MainInterface</string> <key>NSExtensionPointIdentifier</key> <string>com.apple.share-services</string> </dict>
4. 测试注意事项
- 测试时不要直接通过Xcode杀死App,需正常安装后从系统共享菜单启动App
- 文本测试可使用Safari分享链接、备忘录分享文本;媒体测试用照片App分享图片/视频
内容的提问来源于stack exchange,提问作者Samsudeen S
相关产品推荐
相关产品推荐

