繁体   English   中英

如何以编程方式检查并打开iOS 8中的现有应用程序?

[英]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方案。

如果可以使用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)!)

在此输入图像描述

您可以使用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]];
    }

}

您需要使用URL Schemes。 这个想法如下:

为您的应用设置网址标识符可让其他应用识别。 就像那样,您要打开的应用程序需要有一个自定义URL标识符。 谷歌它为您所需的应用程序的自定义URL标识符。 使用以下链接可参考如何使用URL方案。

参考

用于检查应用的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)

                }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM