簡體   English   中英

iOS Interapp使用x-callback-url的雙向通信

[英]iOS Interapp two way communication using x-callback-url

我一直在跟蹤鏈接以使用x-callback-url實現interapp雙向通信。 因此,我制作了兩個不同的應用程序-SourceAppTargetApp

SourceApp

URL方案: 在此處輸入圖片說明

並執行如下步驟打開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)
        }
}

AppDelegate方法從TargetApp接收回響應:

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {

        print("Response From TargetApp==>\(url.absoluteString)")
        return true
}

TargetApp

URL方案: 在此處輸入圖片說明

AppDelegate方法從SourceApp接收請求:

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。 我什至研究了這種方法,但發現與我的方法相同。

任何幫助將不勝感激。

經過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>

在Tar​​getApp的plist中。

我制作了兩個演示應用程序 ,使用x-callback-url輕松演示了應用程序間的雙向通信。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM