简体   繁体   中英

Custom URL Scheme Error : This app is not allowed to query for scheme

I have 2 apps, one client and one server. I am trying interapp communication between ios apps using URL schemes

( I have refered to this 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 as guideline )

This is the plist for client end

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

Code for iOS end

@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)

        }
    }

The server end

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.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>

code

 @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)

        }
    }
}

All I get from either of them is

-canOpenURL: failed for URL: "com.test.clientEndApp://provideInfo?username='Debanjan'" - error: "This app is not allowed to query for scheme com.test.clientendapp"

-canOpenURL: failed for URL: "com.test.serverEndApp://requestInfo?userID='22'" - error: "This app is not allowed to query for scheme com.test.serverendapp"

I have followed all the steps and yet am being in this rut. Help me out to realise what am I doing wrong

I am running it on iPhone 8 iOS 12.2 simulator, on xcode 12.2

You are doing it in opposite way if you register:

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

You should be calling this scheme

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

An you were missing com.test. from your LSApplicationQueriesSchemes

So, thanks to @Lu_, for pointing out my mistakes

Points: 1. The client app will have the scheme for the server app and vice versa 2. The LS should have a whole name and no such com.test or what nots

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM