以编程方式检测 iPhone 上是否安装了应用程序

发布于 2024-09-25 07:28:11 字数 143 浏览 3 评论 0原文

我处于这种情况,我必须在 iPhone 应用程序中显示一个按钮,其中显示“打开 myApp”(如果设备上安装了 myApp)或“下载 myApp”(如果设备上未安装 myApp)。为此,我需要检测设备上是否安装了应用程序(具有已知的自定义 URL)。我该怎么做?提前致谢。

I am in this situation where I have to display a button which says "Open myApp" (if myApp is installed on the device) or it says "Download myApp" (if myApp is not installed on the device) in an iphone app. To do this, I need to detect whether an app (with a known custom URL) has been installed on the device. How can I do this? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

-残月青衣踏尘吟 2024-10-02 07:28:11

2014 年 1 月 8 日更新 - 您可以做的 3 件事

实际上我不得不再次为客户做这件事。他们希望用户能够从主应用程序打开第二个应用程序(如果已安装)。

这是我的发现。使用 canOpenURL 方法检查应用是否已安装或/然后使用 openURL 方法

  1. 打开 iOS 设备上安装的应用
  2. 将用户直接带到应用商店将他们指向应用程序/您的开发人员应用程序列表
  3. 将他们转至网站

每个场景可用的所有代码示例

//Find out if the application has been installed on the iOS device
- (BOOL)isMyAppInstalled { 
    return [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"nameOfMyApp:"]]; 
} 

- (IBAction)openOrDownloadApp { 
    //This will return true if the app is installed on the iOS device
    if ([self myAppIsInstalled]){
        //Opens the application
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"nameOfMyApp:"]]; 
    } 
    else { //App is not installed so do one of following:

        //1. Take the user to the apple store so they can download the app
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms://itunes.com/apps/nameOfMyApp"]]; 

        //OR

        //2. Take the user to a list of applications from a developer
        //or company exclude all punctuation and space characters. 
        //for example 'Pavan's Apps'
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms://itunes.com/apps/PavansApps"]];

        //OR

        //3. Take your users to a website instead, with maybe instructions/information
         [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.pavan.com/WhyTheHellDidTheAppNotOpen_what_now.html"]];

    } 
}

选择一个选项,我刚刚用选择宠坏了您。选择一款适合您的要求的产品。
就我而言,我必须在程序的不同区域使用所有三个选项。

UPDATED 8th January 2014 - 3 things you can do

I actually had to do this for a client again. They wanted users to be able to open their second app from the main app if it had been installed.

This is my finding. Use the canOpenURL method to check if an app is installed or/and then use the openURL method to

  1. Open the application installed on the iOS device
  2. Take the user to the app store directly pointing them to the app/your list of developer apps
  3. Take them to a website instead

All code samples available for each scenario

//Find out if the application has been installed on the iOS device
- (BOOL)isMyAppInstalled { 
    return [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"nameOfMyApp:"]]; 
} 

- (IBAction)openOrDownloadApp { 
    //This will return true if the app is installed on the iOS device
    if ([self myAppIsInstalled]){
        //Opens the application
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"nameOfMyApp:"]]; 
    } 
    else { //App is not installed so do one of following:

        //1. Take the user to the apple store so they can download the app
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms://itunes.com/apps/nameOfMyApp"]]; 

        //OR

        //2. Take the user to a list of applications from a developer
        //or company exclude all punctuation and space characters. 
        //for example 'Pavan's Apps'
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms://itunes.com/apps/PavansApps"]];

        //OR

        //3. Take your users to a website instead, with maybe instructions/information
         [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.pavan.com/WhyTheHellDidTheAppNotOpen_what_now.html"]];

    } 
}

Choose one option, I've just spoiled you with choice. Choose one that fits your requirements.
In my case I had to use all three options in different areas of the program.

夏天碎花小短裙 2024-10-02 07:28:11

如果您的应用程序的 URL 方案是“myapp:”,则

BOOL myAppInstalled = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"myapp:"]];

(需要 iOS 3.0。)

If the URL scheme for your app is "myapp:", then

BOOL myAppInstalled = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"myapp:"]];

(Requires iOS 3.0.)

纵情客 2024-10-02 07:28:11

检查应用程序是否安装在设备中

1) 在 info.plist 中添加 LSApplicationQueriesSchemes,如下例

在此处输入图像描述

2) 和 URL 类型

在此处输入图像描述

3)现在检查应用程序是否安装

- (IBAction)openAppPressed:(UIButton *)sender {
    NSString *urlString = @"XYZAPP://";
    NSURL *url = [NSURL URLWithString:urlString];

    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url];
    }
    else {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itunes link for download app"]];
    }
}

To check app is install in device or not

1) In info.plist add LSApplicationQueriesSchemes as below example

enter image description here

2) and in URL Types

enter image description here

3) Now to check app is install or not

- (IBAction)openAppPressed:(UIButton *)sender {
    NSString *urlString = @"XYZAPP://";
    NSURL *url = [NSURL URLWithString:urlString];

    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url];
    }
    else {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itunes link for download app"]];
    }
}
赴月观长安 2024-10-02 07:28:11

您可以在需要此应用程序嗅探的任何页面的头部添加一个简单的元标记。

有关更多信息,请访问此处:

http:// /developer.apple.com/library/ios/#documentation/AppleApplications/Reference/SafariWebContent/PromotingAppswithAppBanners/PromotingAppswithAppBanners.html

You can add a simple meta tag in the head of any page that needs this app-sniffing.

For more info, go here:

http://developer.apple.com/library/ios/#documentation/AppleApplications/Reference/SafariWebContent/PromotingAppswithAppBanners/PromotingAppswithAppBanners.html

夏有森光若流苏 2024-10-02 07:28:11

对于那些使用 canOpenURL 的人来说,从这里迁移到 openURL:options:completionHandler: 始终是安全的,

NSString *urlString = @"XYZAPP://";
NSURL *url = [NSURL URLWithString:urlString];
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
        if (!success) {
            [[UIApplication sharedApplication] openURL:appStoreUrl options:@{} completionHandler:nil];
        }
}];

因为这不需要您提前声明方案。

已弃用的 canOpenURL 已经有一些奇怪的限制,因为 Twitter 很久以前就用它来检测数百个应用程序。

For those using canOpenURL it is always safe to migrate from this to openURL:options:completionHandler:

NSString *urlString = @"XYZAPP://";
NSURL *url = [NSURL URLWithString:urlString];
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
        if (!success) {
            [[UIApplication sharedApplication] openURL:appStoreUrl options:@{} completionHandler:nil];
        }
}];

because that doesn't require you to declare the scheme ahead of time.

canOpenURL which is deprecated already has some odd limitations because Twitter used it to detect hundreds of apps long ago.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文