ios - 如何从子应用程序检查并打开设备中已安装的父应用程序?

标签 ios swift url-scheme

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

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

我创建了 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")!)
    }

}

在给定条件下,if 返回 true 且 UIApplication.shared.open(parentAppUrl!)已执行,但未打开我的父应用程序。

最佳答案

The parent app's info plist should have something like this:

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

and in your parent app AppDelegate, you should handle it from here:

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
}

then in your child app: // add this to button action or where ever you want it to trigger the 'open parent app scheme'

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")!)
    }
}

关于ios - 如何从子应用程序检查并打开设备中已安装的父应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48262255/

相关文章:

ios - 在 Swift 4 中在 Google map 上显示路径

macos - 除非应用程序已在运行,否则 URL 方案监听器无法工作

swift - 本地通知的通知服务扩展

android - Android 的 URL 方案

ios - "fb://"url scheme - 官方与否?

ios - iOS 如何以及何时杀死长时间处于后台的应用程序?

ios - 我不知道为什么在 swift3 中不调用 viewForHeaderInSection

ios - UITextView 与 NSMutableAttributedString 与链接 SwiftUI

ios - 如何在 Swift 中使用拉取刷新?

swift - UIAccessibilityTrait可调不进入increment()或decrement() swift