2

我有 2 个应用程序,一个客户端和一个服务器。我正在尝试使用 URL 方案在 ios 应用程序之间进行应用程序间通信

(我已将此https://developer.apple.com/documentation/uikit/inter-process_communication/allowing_apps_and_websites_to_link_to_your_content/defining_a_custom_url_scheme_for_your_app 作为指导方针)

这是客户端的plist

<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>$(DEVELOPMENT_LANGUAGE)</string>
    <key>CFBundleExecutable</key>
    <string>$(EXECUTABLE_NAME)</string>
    <key>CFBundleIdentifier</key>
    <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>$(PRODUCT_NAME)</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0</string>
    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeRole</key>
            <string>Viewer</string>
            <key>CFBundleURLName</key>
            <string>com.test.serverendapp</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>serverendapp</string>
            </array>
        </dict>
    </array>
    <key>CFBundleVersion</key>
    <string>1</string>
    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>com.test.serverendapp</string>
    </array>
    <key>LSRequiresIPhoneOS</key>
    <true/>
    <key>UILaunchStoryboardName</key>
    <string>LaunchScreen</string>
    <key>UIMainStoryboardFile</key>
    <string>Main</string>
    <key>UIRequiredDeviceCapabilities</key>
    <array>
        <string>armv7</string>
    </array>
    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>UISupportedInterfaceOrientations~ipad</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationPortraitUpsideDown</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
</dict>
</plist>

iOS端代码

@IBAction func tapToClient(){
        let urlsrting = "com.test.serverEndApp://requestInfo?userID='22'"
        let url = URL(string: urlsrting)!
        if UIApplication.shared.canOpenURL(url){

            if #available(iOS 10.0, *) {
                UIApplication.shared.open(url, options: [:]) { (success) in
                    print(" this is \(success)")
                }

            } else {
                UIApplication.shared.openURL(url)
            }
        }else{

            let alertController = UIAlertController(title: "Error", message:
                "There is no such server app here", preferredStyle: .alert)
            alertController.addAction(UIAlertAction(title: "Dismiss", style: .default))

            self.present(alertController, animated: true, completion: nil)

        }
    }

服务器端

列表

<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>$(DEVELOPMENT_LANGUAGE)</string>
    <key>CFBundleExecutable</key>
    <string>$(EXECUTABLE_NAME)</string>
    <key>CFBundleIdentifier</key>
    <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>$(PRODUCT_NAME)</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0</string>
    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeRole</key>
            <string>Viewer</string>
            <key>CFBundleURLName</key>
            <string>com.test.clientendapp</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>clientendapp</string>
            </array>
        </dict>
    </array>
    <key>CFBundleVersion</key>
    <string>1</string>
    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>com.test.clientendapp</string>
    </array>
    <key>LSRequiresIPhoneOS</key>
    <true/>
    <key>UILaunchStoryboardName</key>
    <string>LaunchScreen</string>
    <key>UIMainStoryboardFile</key>
    <string>Main</string>
    <key>UIRequiredDeviceCapabilities</key>
    <array>
        <string>armv7</string>
    </array>
    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>UISupportedInterfaceOrientations~ipad</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationPortraitUpsideDown</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
</dict>
</plist>

代码

 @IBAction func tapToServer(){
        let urlsrting = "com.test.clientEndApp://provideInfo?username='Debanjan'"
        let url = URL(string: urlsrting)!
        if UIApplication.shared.canOpenURL(url){
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(url, options: [:]) { (success) in
                    print(" this is \(success)")
                }

            } else {
                UIApplication.shared.openURL(url)
            }
        }else{
            let alertController = UIAlertController(title: "Error", message:
                "There is no such client app here", preferredStyle: .alert)
            alertController.addAction(UIAlertAction(title: "Dismiss", style: .default))

            self.present(alertController, animated: true, completion: nil)

        }
    }
}

我从他们那里得到的只是

-canOpenURL:URL 失败:“com.test.clientEndApp://provideInfo?username='Debanjan'” - 错误:“此应用不允许查询方案 com.test.clientendapp”

-canOpenURL:URL 失败:“com.test.serverEndApp://requestInfo?userID='22'” - 错误:“此应用不允许查询方案 com.test.serverendapp”

我已遵循所有步骤,但仍处于这种状态。帮助我意识到我做错了什么

我在 xcode 12.2 上的 iPhone 8 iOS 12.2 模拟器上运行它

4

2 回答 2

3

如果您注册,则以相反的方式进行:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>com.test.clientEndApp</string>
</array>

你应该调用这个方案

let urlsrting = "com.test.clientEndApp://provideInfo?username='Debanjan'"

一个你com.test.从你的LSApplicationQueriesSchemes

于 2019-10-22T10:41:35.913 回答
0

所以,感谢@Lu_,指出我的错误

要点: 1. 客户端应用程序将具有服务器应用程序的方案,反之亦然 2. LS 应该有一个完整的名称,并且没有这样的 com.test 或什么不是

于 2019-10-22T11:35:01.100 回答