[英]How to Programmatically check and open an existing app in iOS 8?
我想检查我的iphone中是否有应用程序。 如果它存在,我想启动它,否则打开应用程序的App Store链接。
请建议所需的参数以及实现方法。
我有App Store链接打开应用程序,但我需要检查手机中是否已有应用程序。
试试这段代码
迅捷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")!)
}
}
迅捷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 = "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)!)
您可以使用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]];
}
}
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)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.