我想检查我的 iPhone 上是否安装了某个应用。如果已安装,我想要启动它;否则,需要打开该应用的 App Store 链接。
请建议所需的参数并提供实现方式。
我有打开应用的 App Store 链接,但需要检查该应用是否已经在手机上安装。
请建议所需的参数并提供实现方式。
我有打开应用的 App Store 链接,但需要检查该应用是否已经在手机上安装。
尝试这段代码
Swift 2.2
@IBAction func openInstaApp(sender: AnyObject) {
var instagramHooks = "instagram://user?username=your_username"
var instagramUrl = NSURL(string: instagramHooks)
if UIApplication.sharedApplication().canOpenURL(instagramUrl!)
{
UIApplication.sharedApplication().openURL(instagramUrl!)
} else {
//redirect to safari because the user doesn't have Instagram
println("App not installed")
UIApplication.sharedApplication().openURL(NSURL(string: "https://itunes.apple.com/in/app/instagram/id389801252?m")!)
}
}
swift 3
@IBAction func openInstaApp(sender: AnyObject) {
let instagramHooks = "instagram://user?username=your_username"
let instagramUrl = URL(string: instagramHooks)
if UIApplication.shared.canOpenURL(instagramUrl! as URL)
{
UIApplication.shared.open(instagramUrl!)
} else {
//redirect to safari because the user doesn't have Instagram
print("App not installed")
UIApplication.shared.open(URL(string: "https://itunes.apple.com/in/app/instagram/id389801252?m")!)
}
}
要在另一个应用程序中打开应用程序,您需要从要打开的应用程序中公开URL方案。
如果可以使用openURL
函数(使用自定义公开的URL方案),则用户可以直接访问您的应用程序。
如果不能,您可以使用openURL
函数打开应用商店应用程序页面,提供iTunes应用程序URL。
这里包含完整的想法关于如何实现它。
您可以使用以下代码检查所需设备是否已安装应用程序:
// url = "example" (check the following image to understand)
let canOpen = UIApplication.sharedApplication().canOpenURL(NSURL(string: url as String)!)
当url
被预配置时(在您的Info.plist中为您的应用程序注册了URL方案),请注意:
因此,如果canOpen为true
,则表示该应用已安装,您可以使用以下方式打开:
UIApplication.sharedApplication().openURL(NSURL(string:url)!)
let naviURL = NSURL(string: "yandexnavi://build_route_on_map?lat_from=55.751802&lon_from=37.586684&lat_to=55.758192&lon_to=37.642817")!
let res = UIApplication.sharedApplication().openURL(naviURL)
if !res {
let appStoreURL = NSURL(string: "itms://itunes.apple.com/ru/app/andeks.navigator/id474500851?mt=8")!
UIApplication.sharedApplication().openURL(appStoreURL)
}
您可以使用URL方案iOS功能检查应用程序是否可用,然后打开iTunes URL。尝试使用以下代码解决问题:
首先在您的iTunes应用程序中设置URL方案“testapp”,然后在其他应用程序中使用以下代码打开您的应用程序:
-(IBAction) openApp:(id)sender {
// Opens the app if installed, otherwise displays an error
UIApplication *yourApplication = [UIApplication sharedApplication];
NSString *URLEncodedText = [self.textBox.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *ourPath = [@"testapp://" stringByAppendingString:URLEncodedText];
NSURL *ourURL = [NSURL URLWithString:ourPath];
if ([yourApplication canOpenURL:ourURL]) {
[yourApplication openURL:ourURL];
}
else {
//The App is not installed. It must be installed from iTunes code.
NSString *iTunesLink = @"//Your itunes Url";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:iTunesLink]];
}
}