iOS 原生配置
分享后能否跳回 App 全靠原生侧 URL Scheme 与回调注册。模板别凭记忆编,逐项核对本页。
友盟 appkey、微信 AppSecret、Universal Link 等配置通过 JS Common.preInit({ appkey, wechatAppId, ... }) 传入,不写在 Info.plist。Info.plist 只配 iOS 系统强制的 URL Scheme 白名单 + 回调 scheme。
ios/<App>/Info.plist
<!-- iOS 9+ 强制:声明 App 想用哪些第三方 App 的 URL Scheme 查询 / 跳转 -->
<key>LSApplicationQueriesSchemes</key>
<array>
<string>weixin</string>
<string>weixinULAPI</string>
<string>weixinURLParamsAPI</string>
<string>wechat</string>
<string>dingtalk</string>
<string>dingtalk-open</string>
<string>dingtalk-sso</string>
</array>
<!-- 接收微信 / 钉钉分享回调跳回 App。URL Scheme 由各平台开放平台分配:
微信 = "wx" + appid;钉钉 = "dingoa" + appid -->
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key><string>Editor</string>
<key>CFBundleURLName</key><string>weixin</string>
<key>CFBundleURLSchemes</key>
<array><string>wxXXXXXXXX</string></array>
</dict>
<dict>
<key>CFBundleTypeRole</key><string>Editor</string>
<key>CFBundleURLName</key><string>dingtalk</string>
<key>CFBundleURLSchemes</key>
<array><string>dingoaXXXXXXXX</string></array>
</dict>
</array>
| 键 | 必填 | 说明 |
|---|---|---|
LSApplicationQueriesSchemes | ✅ | 第三方 App scheme 查询白名单,缺则 isInstalled / 跳转判断不准 |
CFBundleURLTypes | ✅ | 回调 scheme(wx+appid、dingoa+appid),缺则分享后跳不回 App |
ios/<App>/AppDelegate.swift
把 open url 与 continue userActivity 转发给桥导出的 UmengBootstrap。
import UIKit
// UmengBootstrap 是 @unif/react-native-umeng 桥导出的 Objective-C class,
// CocoaPods 装好后由 ReactNativeUmeng pod 的 public header 暴露给宿主,
// Swift 端自动 bridge,无需写 bridging header。
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ app: UIApplication, open url: URL,
options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
return UmengBootstrap.shared().handleOpen(url, options: options)
}
func application(_ application: UIApplication,
continue userActivity: NSUserActivity,
restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
return UmengBootstrap.shared().handleUniversalLink(userActivity)
}
}
handleOpen(_:options:),不是 handleOpenURL桥侧 Objective-C 方法签名是 handleOpenURL:options:,首参类型 NSURL。Swift 的 omit-needless-words 规则会去掉方法名末尾与首参类型重复的 URL,导入成 handleOpen(_:options:)。写 handleOpenURL 会编译报 has been renamed to 'handleOpen(_:options:)'。
Swift omit-needless-words 规则说明
Swift 导入 ObjC 时,若方法名末尾的词与首参类型名(去 NS 前缀后)完全匹配,则自动省略:
handleOpenURL:options:→ 首参NSURL→ 末尾URL与类型名URL匹配 → 省略 →handleOpen(_:options:)handleUniversalLink:→ 首参NSUserActivity→ 末尾Link与UserActivity不匹配 → 不省略 → 保持handleUniversalLink(_:)
| 转发方法(Swift 侧) | 触发场景 |
|---|---|
handleOpen(_:options:) | App 通过 URL Scheme 被分享回调拉起 |
handleUniversalLink(_:) | 微信 Universal Link 回跳(1.8.6+ 必需) |
ios/Podfile
无需特殊配置,标准 RN 新架构 Podfile 即可(use_native_modules! 会自动装入 ReactNativeUmeng pod 及其友盟依赖 UMCommon / UMDevice / UMShare)。
target 'YourApp' do
config = use_native_modules!
use_react_native!(:path => config[:reactNativePath])
post_install do |installer|
react_native_post_install(installer, config[:reactNativePath])
end
end
友盟 U-Share 在 Apple Silicon Mac 模拟器上有 EXCLUDED_ARCHS=arm64 限制(旧式 .framework 未出 arm64 simulator slice)。用真机测试微信 / 钉钉分享。强制清 EXCLUDED_ARCHS 反而可能在编译期触发 arm64 链接错。
微信 Universal Link
微信 SDK 1.8.6+ 强制 Universal Link:
- 到 Apple Developer Portal 为 App ID 启用 Associated Domains capability。
- 在服务器部署
apple-app-site-association文件(路径/.well-known/apple-app-site-association)。 - Xcode Entitlements 加
applinks:your.host。 - 把对应的
https://your.host/传给Common.preInit({ wechatUniversalLink })。
详见微信开放平台官方文档。
平台支持
| 配置项 | iOS | Android |
|---|---|---|
LSApplicationQueriesSchemes | ✅ 必填 | — |
CFBundleURLTypes | ✅ 必填 | — |
| AppDelegate 转发 | ✅ 必填 | — |
| 微信 Universal Link | ✅ 1.8.6+ 强制 | — |
相关
- Android 原生配置 —— 回调 Activity / 权限 / queries
- 快速上手 —— 完整接入流程
- 常见问题 → 分享无回调 —— 原生未注册的排障