有效登录后React Native iOS“Safari无法打开页面,因为地址无效”
Posted
技术标签:
【中文标题】有效登录后React Native iOS“Safari无法打开页面,因为地址无效”【英文标题】:React Native iOS "Safari cannot open the page because the address is invalid" after valid login 【发布时间】:2019-04-28 18:24:22 【问题描述】:登录成功后得到以下内容。登录流程在 android 上按预期工作,并正确返回令牌、刷新等...
“地址无效,Safari无法打开页面”
Screen Recording of Error
Auth0 回调配置
Info.plist
...
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>None</string>
<key>CFBundleURLName</key>
<string>auth0</string>
<key>CFBundleURLSchemes</key>
<array>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
</array>
</dict>
</array>
...
AppDeligate.m
...
#import <React/RCTLinkingManager.h>
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
return [RCTLinkingManager application:application openURL:url
sourceApplication:sourceApplication annotation:annotation];
...
【问题讨论】:
你的PRODUCT_BUNDLE_IDENTIFIER
是什么?
@hardikparmar 我实际上是在使用<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
,这不是占位符表示。这就是 info.plist 中的 CFBundleIdentifier
最初是通过 create-react-native-app 设置的。如果我在 xcode 中打开项目,包标识符是 org.reactjs.native.example.Dieta
@hardikparmar 我尝试在 Info.plist 的两个位置将 $(PRODUCT_BUNDLE_IDENTIFIER)
替换为 org.reactjs.native.example.Dieta
,但仍然看到同样的问题
转到 safari 并输入相同的 url (org.reactjs.native.example.Dieta
) 看看它是否打开了应用程序。
@hardikparmar 它没有 - 它只是运行谷歌搜索
【参考方案1】:
我遇到了同样的问题(在使用 Auth0 时,答案取决于他们在此处提供的解决方案 - https://auth0.com/docs/quickstart/native/react-native)
此外,来自此处的另一个答案的一些输入 - "Safari cannot open the page because the address is invalid" appearing when accessing Branch link with app uninstalled
最终,归根结底
-
从 ios 9.2 开始,Apple 不再正式支持用于深度链接的 URI 方案,强烈建议开发人员实施通用链接以便在 iOS 上获得等效功能
要克服这个问题,您必须启用此应用程序链接才能工作。一种选择是(检查链接堆栈的答案)希望发生这种情况的服务提供商将提供一个通用链接。
另一个选项(下面的代码 sn-ps)是添加一个 'URLScheme'
#import <React/RCTLinkingManager.h>
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url
options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options
return [RCTLinkingManager application:app openURL:url options:options];
第二步
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>None</string>
<key>CFBundleURLName</key>
<string>auth0</string>
<key>CFBundleURLSchemes</key>
<array>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
</array>
</dict>
</array>
这就是为我解决问题的原因(使用 Auth0 库时,但您使用的任何库都应该有类似的东西)。这基本上克服了苹果/iOS 对应用链接的限制,这就是您得到原始 safari 错误的原因。
【讨论】:
以上是关于有效登录后React Native iOS“Safari无法打开页面,因为地址无效”的主要内容,如果未能解决你的问题,请参考以下文章
当应用程序被杀死时,在推送通知后执行操作:react-native,IOS
React-native Log in with Facebook App 刷新应用
react native fcm在android上有效,但在ios上无效
Facebook 登录“异常抛出”错误通过 iOS 上的 React Native