【问题标题】:How to check & open installed parent app in your device from child app?如何从子应用程序检查和打开设备中已安装的父应用程序?
【发布时间】:2018-01-15 11:39:49
【问题描述】:

我的设备中有父子应用程序。我需要检查并打开父应用程序。在我的应用程序委托中(当应用程序启动时),我正在检查应用程序是否已安装,然后打开父应用程序,否则转到应用程序商店。

我已尝试以下代码来检查您的设备中是否安装了应用程序。

我是按照给定的 URL 类型创建的 -

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>Parent</string>
        </array>
        <key>CFBundleURLName</key>
        <string>parentAppBundleid</string>
    </dict>
</array>

我在 didFinishLaunchingWithOptions 中编写了以下代码-

 func openGoYoApp() {
    let parentapp = "Parent://"
    let parentAppUrl = URL(string: parentapp)
    if UIApplication.shared.canOpenURL(parentAppUrl! as URL)
    {
        UIApplication.shared.open(parentAppUrl!)

    }
    else {
        //redirect to safari because the user doesn't have Instagram
        print("App not installed")
        UIApplication.shared.open(URL(string: "ituneLink")!)
    }

}

在给定的条件下,如果返回 true 并且 UIApplication.shared.open(parentAppUrl!) 被执行但它没有打开我的父应用程序。

【问题讨论】:

    标签: ios swift url-scheme


    【解决方案1】:

    父应用的信息列表应该是这样的:

    <array>
        <dict>
            <key>CFBundleTypeRole</key>
            <string>Editor</string>
            <key>CFBundleURLIconFile</key>
            <string>temp</string>
            <key>CFBundleURLName</key>
            <string>com.parent-app</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>parent</string>
            </array>
        </dict>
    </array>
    

    在您的父应用 AppDelegate 中,您应该从这里处理它:

    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    
        print("open url: \(options)")
    
        if let scheme = url.scheme, scheme == "parent" {
            // handle url scheme
    
            return true
        }
    
        return false
    }
    

    然后在您的子应用中: // 将此添加到按钮操作或您希望它触发“打开父应用程序方案”的任何位置

    let urlString = "parent://info-to-pass-to-parent"
    if let schemeURL = URL(string: urlString) {
        if UIApplication.shared.canOpenURL(schemeURL) {
            UIApplication.shared.open(schemeURL, options: [:], completionHandler: { (success) in
    
                print("open success: \(success)")
            })
        }else{
            //redirect to safari because the user doesn't have Instagram
            print("App not installed")
            UIApplication.shared.open(URL(string: "ituneLink")!)
        }
    }
    

    【讨论】:

    • 谢谢!我遵循了你的代码。一切正常。但我没有从子应用程序打开到父应用程序。我的父应用在应用商店中可用,但在子应用中不可用。如您所说,我之前没有编写任何代码父应用程序,我现在需要检查吗?现在怎么查?
    • @venkat 我更新了我的答案,我添加了您可以检查是否安装了父应用程序的位置。
    猜你喜欢
    • 2011-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-15
    • 2019-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多