gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:51:50 25 4
gpt4 key购买 nike

我的设备中有父子应用程序。我需要检查并打开父应用程序。在我的应用程序委托(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com