我是父母&我的设备中的子应用程序。我需要检查并打开父应用程序。在我的应用程序委托中(当应用程序启动时),我正在检查应用程序是否已安装,然后打开父应用程序,否则转到应用程序商店。
我尝试过以下代码来检查您的设备中是否安装了应用。
我创建了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!)
但是它没有打开我的父应用程序。
答案 0 :(得分:0)
父应用的信息plist应该是这样的:
<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")!)
}
}