我一直在关注this链接,以使用x-callback-url
实现interapp双向通信。所以我制作了两个不同的应用程序 - SourceApp & TargetApp
SourceApp
按以下方式打开TargetApp的实现:
@IBAction func btnOpenAppPressed(_ sender:UIButton){
let url = URL.init(string: "targetapp://x-callback-url/translate?x-success=sourceapp://x-callback-url/acceptTranslation&x-source=SourceApp&x-error=sourceapp://x-callback-url/translationError&word=Hello&language=Spanish")
if (UIApplication.shared.canOpenURL(url!)){
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
}
}
从TargetApp接收响应的AppDelegate方法:
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
print("Response From TargetApp==>\(url.absoluteString)")
return true
}
TargetApp
从SourceApp接收请求的AppDelegate方法:
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
print("Response From SourceApp==>\(url.absoluteString)")
return true
}
TargetApp的IBAction将响应发送回SourceApp:
@IBAction func btnBackToSourceAppPressed(_ sender:UIButton){
let url = URL.init(string: "sourceapp://x-callback-url/acceptTranslation?x-source=TargetApp&word=Hola")
if (UIApplication.shared.canOpenURL(url!)){
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
}
}
现在的问题是,我可以从SourceApp打开TargetApp,但无法从TargetApp返回到SourceApp。我甚至研究过this方法,但我发现它跟我的一样。
任何帮助将不胜感激。
答案 0 :(得分:0)
经过2天的挣扎,我发现我没有在plist中使用LSApplicationQueriesSchemes
。我还发现在Objective-C中,如果我跳过LSApplicationQueriesSchemes
,我可以轻松地在这两个应用之间进行通信。但是如果你使用swift,你必须LSApplicationQueriesSchemes
否则,你会得到
-canOpenURL: failed for URL: "targetapp://" - error: "This app is not allowed to query for scheme targetapp"
所以,我必须使用
<key>LSApplicationQueriesSchemes</key>
<array>
<string>targetapp</string>
</array>
在SourceApp的plist和
中<key>LSApplicationQueriesSchemes</key>
<array>
<string>sourceapp</string>
</array>
在TargetApp的plist中。
我使用x-callback-url轻松演示了应用程序之间的双向通信two demo apps。