Check for installed apps on iOS 9 and 10

2019-07-24 02:06发布

I am trying to check if a user has installed a couple of apps. I need it to work on ios9 and ios 10. I am testing on ios9 first and the schemeAvailable func is returning true even though the app passed is not installed on the device.

func schemeAvailable(scheme: String) -> Bool {
   if let url = URL(string: scheme) {
       return UIApplication.shared.canOpenURL(url)
   }
   return false
}

I have added the following to my info.plist i.e. the apps I will check are installed:

<key>LSApplicationQueriesSchemes</key>
<array>
<string>app1</string>
<string>app2</string>
</array>

and in my project's info tab, I have added 2 url types and inserted the identifier and url scheme for each.

2条回答
乱世女痞
2楼-- · 2019-07-24 02:31

It sounds like you do NOT have all registrations correct. Your App1 and App2 each need URL Identifiers and Schemes, and your app that "wants to launch App1 and App2" must have LSApplicationQueriesSchemes defined.

E.G.

This is in "App1"

enter image description here

This is in "App2"

enter image description here

This is in "LauncherApp"

enter image description here

Then, in "LauncherApp" I can do:

    let s = "DMCApp1://TestMe"
    let customURL: URL = URL(string: s)!

    if UIApplication.shared.canOpenURL(customURL) {
        print("YES - I can open App1 !!!")
        UIApplication.shared.open(customURL, options: [:], completionHandler: nil)
    } else {
        print("NO - App1 is not available.")
    }
查看更多
趁早两清
3楼-- · 2019-07-24 02:41

You don't have to add the other apps' schemes on your app plist. If you do that you app becomes the responder to URL with that scheme and always be true.

canOpenURL method is not marked as deprecated on documentation

I copy a snippet with part of the code I use to open app url by custom schemes...

/**
    Abre los perfiles de la app en las distintas redes sociales.

    - Parameters:
        - url: Esquema personalizado de la app
        - secondaryURL: Si la app no está presente en el dispositivo abrimos su version web (twitter, facebook, etc...)
*/    
    func open(_ url: String, secondaryURL: String? = nil) -> Void
        {
            if let url = URL(string: url), UIApplication.shared.canOpenURL(url)
            {
                UIApplication.shared.open(url, completionHandler: nil)
            }
            else if let secondaryURL = secondaryURL, let url = URL(string: secondaryURL), UIApplication.shared.canOpenURL(url)
            {
                UIApplication.shared.open(url, completionHandler: nil)
            }
            else 
            {
                print("No se puede hacer nada de nada.\r\napp_url: \(url)")
            }
        } 

And here's an example. I try to open twitter app, and if not presented, open Safari and shows the twitter web page.

@IBAction private func handleTwitterButtonTap(sender: UIButton) -> Void
{
    let twitter_app_url: String = "twitter://user?screen_name=GetPomodoroApp"
    let twitter_web_url: String = "https://twitter.com/GetPomodoroApp"

    self.open(twitter_app_url, secondaryURL: twitter_web_url)
}
查看更多
登录 后发表回答