如何在应用商店中链接到应用程序

762

我正在制作我的iPhone游戏的免费版本。我想在免费版本中放置一个按钮,可以将人们带到应用商店中的付费版本。如果我使用标准链接

http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=300136119&mt=8

iPhone首先会打开Safari,然后再进入应用商店。我已经使用过其他应用程序,它们可以直接打开应用商店,所以我知道这是可能的。

有什么想法吗?应用商店的URL Scheme是什么?


1
对于 SWIFT 3.X 和本地化应用程序,这个答案可能会有所帮助:https://dev59.com/3XA75IYBdhLWcg3wv77A#41765175 - LukeSideWalker
27个回答

818

编辑于2016-02-02

iOS 6开始引入SKStoreProductViewController类,应用内打开App Store页面。在Swift 3.x/2.xObjective-C中的代码示例在这里

SKStoreProductViewController对象呈现一个商店,允许用户从App Store购买其他媒体。例如,您的应用程序可能显示商店以让用户购买另一个应用程序。


来自Apple Developers的新闻和公告

使用iTunes链接直接将客户带到App Store上的应用程序,您可以通过iTunes链接为客户提供直接从您的网站或营销活动访问您的应用程序的简单方法。创建iTunes链接很简单,可以设置为将客户定向到单个应用程序、所有应用程序或指定公司名称的特定应用程序。

要将客户发送到特定的应用程序:

http://itunes.com/apps/appname

要将客户发送到您在App Store上有的应用程序列表:

http://itunes.com/apps/developername

要将客户发送到包含您公司名称的特定应用程序中,请使用以下URL:

http://itunes.com/apps/developername/appname


额外说明:

您可以将http://替换为itms://itms-apps://以避免重定向。

请注意itms://会将用户发送到iTunes商店,而itms-apps://将他们发送到App Store!

有关命名的信息,请参见Apple QA1633:

https://developer.apple.com/library/content/qa/qa1633/_index.html

编辑(截至2015年1月):

itunes.com/apps链接应更新为appstore.com/apps。请参见已更新的QA1633。新的QA1629建议使用以下步骤和代码从应用程序启动商店:

  1. 在计算机上启动iTunes。
  2. 搜索要链接的项目。
  3. 在iTunes中,右键单击或按Ctrl+单击项目的名称,然后从弹出菜单中选择“复制iTunes Store网址”。
  4. 在您的应用程序中,使用已复制的iTunes URL创建一个NSURL对象,然后将此对象传递给UIApplicationopenURL:方法,以在App Store中打开您的项目。

示例代码:

NSString *iTunesLink = @"itms://itunes.apple.com/app/apple-store/id375380948?mt=8";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:iTunesLink]];

iOS10+:

 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:iTunesLink] options:@{} completionHandler:nil];

Swift 4.2

   let urlStr = "itms-apps://itunes.apple.com/app/apple-store/id375380948?mt=8"
    if #available(iOS 10.0, *) {
        UIApplication.shared.open(URL(string: urlStr)!, options: [:], completionHandler: nil)
        
    } else {
        UIApplication.shared.openURL(URL(string: urlStr)!)
    }

164
一个小技巧是使用itms://而不是http://,这样应用商店会直接打开。在iPhone上,使用http会重定向2次,而使用itms只有1次。当使用较早版本的phobos链接(见上文)时,没有任何重定向。如果使用phobos链接,请使用来自iTunes Connect的ID。您可以选择提交应用程序而不包括二进制文件。这样做可以让您在提交实际二进制文件之前获得ID。虽然我没有尝试过,但我听说这个方法可以奏效。 - quano
11
请参见:http://developer.apple.com/library/ios/#qa/qa1633/_index.html(只需删除空格即可)。 - Nathan S.
10
除了...appname应该使用哪个正确的值?是应用程序的“Bundle Display Name”吗?它是否不区分大小写?空格如何处理等等? - aroth
16
应在真实设备上进行测试。 - Protocole
8
是的,应该在真实设备上进行测试!我曾经在什么都没有发生的情况下苦苦挣扎了一个小时,然后把iPad插上并在那里进行了测试。啊。 - Kalle
显示剩余31条评论

371

如果你想直接打开应用程序的App Store页面,你应该使用:

itms-apps://...

这样它将直接在设备中打开App Store应用程序,而不是首先进入iTunes,再仅打开App Store(当只使用itms://时)


编辑:2017年4月。itms-apps://在iOS10中实际上又起作用了。我进行了测试。

编辑:2013年4月。这在iOS5及以上版本中不再起作用。只需使用

https://itunes.apple.com/app/id378458261

并且没有更多的重定向。


2
截至今日,当我使用 http://... 时,我看到两个重定向。这个答案对我来说非常完美——itms-apps://... 直接在设备上打开App Store应用程序,无需任何重定向。 - Josh Brown
4
Rocotilos,你确定这不再起作用了吗?你能发一个链接吗? - johndodo
9
在iOS系统中,“itms”打开的是iTunes,而“itms-apps”则会打开App Store。如果需要提示用户进行更新,这是一个关键的区别! (适用于iOS 10) - mcm
2
itms-apps 通过 openURL: 在 iOS 10.2 中为我打开 App Store 应用程序。看起来没有问题。 - pkamb
1
自从10.3版本以来,这种方法可以使用,但必须通过一个中间的显示弹出窗口“用应用商店打开此URL”?参考:https://blog.branch.io/the-problem-with-apple-app-store-redirects-on-safari-ios-10-3/ - gabry
显示剩余9条评论

62

iOS 6以后,使用SKStoreProductViewController类是正确的方法。

Swift 5.x:

func openStoreProductWithiTunesItemIdentifier(_ identifier: String) {
    let storeViewController = SKStoreProductViewController()
    storeViewController.delegate = self

    let parameters = [ SKStoreProductParameterITunesItemIdentifier : identifier]
    storeViewController.loadProduct(withParameters: parameters) { [weak self] (loaded, error) -> Void in
        if loaded {
            // Parent class of self is UIViewContorller
            self?.present(storeViewController, animated: true, completion: nil)
        }
    }
}
private func productViewControllerDidFinish(viewController: SKStoreProductViewController) {
    viewController.dismiss(animated: true, completion: nil)
}
// Usage:
openStoreProductWithiTunesItemIdentifier("1234567")

Swift 3.x:

func openStoreProductWithiTunesItemIdentifier(identifier: String) {
    let storeViewController = SKStoreProductViewController()
    storeViewController.delegate = self

    let parameters = [ SKStoreProductParameterITunesItemIdentifier : identifier]
    storeViewController.loadProduct(withParameters: parameters) { [weak self] (loaded, error) -> Void in
        if loaded {
            // Parent class of self is UIViewContorller
            self?.present(storeViewController, animated: true, completion: nil)
        }
    }
}

func productViewControllerDidFinish(_ viewController: SKStoreProductViewController) {
    viewController.dismiss(animated: true, completion: nil)
}
// Usage:
openStoreProductWithiTunesItemIdentifier(identifier: "13432")

你可以这样获取应用程序的iTunes项目标识符:(而不是静态的)

Swift 3.2

var appID: String = infoDictionary["CFBundleIdentifier"]
var url = URL(string: "http://itunes.apple.com/lookup?bundleId=\(appID)")
var data = Data(contentsOf: url!)
var lookup = try? JSONSerialization.jsonObject(with: data!, options: []) as? [AnyHashable: Any]
var appITunesItemIdentifier = lookup["results"][0]["trackId"] as? String
openStoreProductViewController(withITunesItemIdentifier: Int(appITunesItemIdentifier!) ?? 0)

Swift 2.x:

func openStoreProductWithiTunesItemIdentifier(identifier: String) {
    let storeViewController = SKStoreProductViewController()
    storeViewController.delegate = self

    let parameters = [ SKStoreProductParameterITunesItemIdentifier : identifier]
    storeViewController.loadProductWithParameters(parameters) { [weak self] (loaded, error) -> Void in
        if loaded {
            // Parent class of self is UIViewContorller
            self?.presentViewController(storeViewController, animated: true, completion: nil)
        }
    }
}

func productViewControllerDidFinish(viewController: SKStoreProductViewController) {
    viewController.dismissViewControllerAnimated(true, completion: nil)
}
// Usage
openStoreProductWithiTunesItemIdentifier("2321354")
Objective-C:
static NSInteger const kAppITunesItemIdentifier = 324684580;
[self openStoreProductViewControllerWithITunesItemIdentifier:kAppITunesItemIdentifier];

- (void)openStoreProductViewControllerWithITunesItemIdentifier:(NSInteger)iTunesItemIdentifier {
    SKStoreProductViewController *storeViewController = [[SKStoreProductViewController alloc] init];

    storeViewController.delegate = self;

    NSNumber *identifier = [NSNumber numberWithInteger:iTunesItemIdentifier];

    NSDictionary *parameters = @{ SKStoreProductParameterITunesItemIdentifier:identifier };
    UIViewController *viewController = self.window.rootViewController;
    [storeViewController loadProductWithParameters:parameters
                                   completionBlock:^(BOOL result, NSError *error) {
                                       if (result)
                                           [viewController presentViewController:storeViewController
                                                              animated:YES
                                                            completion:nil];
                                       else NSLog(@"SKStoreProductViewController: %@", error);
                                   }];

    [storeViewController release];
}

#pragma mark - SKStoreProductViewControllerDelegate

- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController {
    [viewController dismissViewControllerAnimated:YES completion:nil];
}
您可以像这样获取kAppITunesItemIdentifier(应用程序的iTunes项标识符):(而不是静态的)
NSDictionary* infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString* appID = infoDictionary[@"CFBundleIdentifier"];
    NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?bundleId=%@", appID]];
    NSData* data = [NSData dataWithContentsOfURL:url];
    NSDictionary* lookup = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    NSString * appITunesItemIdentifier =  lookup[@"results"][0][@"trackId"]; 
    [self openStoreProductViewControllerWithITunesItemIdentifier:[appITunesItemIdentifier intValue]];

4
在 Swift 中,不要忘记导入和委托:import StoreKitclass RateMeViewController: UIViewController, SKStoreProductViewControllerDelegate { - djdance
这仍然可以工作,但是当您点击“撰写评论”时,它不起作用。 您必须点击右上角的“商店”按钮,然后再点击“撰写评论”。 有人知道解决方法吗? - socca1157
“撰写评论”尝试打开另一个模态框并失败,可能是因为您已经在模态框中呈现。但是苹果公司强制执行此操作。因此似乎没有适当的方法使评论工作。这使得整个概念无效。 - AmitP
那个 JSON 不总是返回数据,有时会返回计数为零。在提交之前获取应用程序 ID 不可靠。 - Amber K

42

苹果刚刚宣布了appstore.com网址。

https://developer.apple.com/library/ios/qa/qa1633/_index.html

有三种类型的App Store短链接,分别是两种形式,一种用于iOS应用程序,另一种用于Mac应用程序:

公司名称

iOS:http://appstore.com/ 例如,http://appstore.com/apple

Mac:http://appstore.com/mac/ 例如,http://appstore.com/mac/apple

应用程序名称

iOS:http://appstore.com/ 例如,http://appstore.com/keynote

Mac:http://appstore.com/mac/ 例如,http://appstore.com/mac/keynote

公司的应用程序

iOS:http://appstore.com// 例如,http://appstore.com/apple/keynote

Mac: http://appstore.com/mac// 例如,http://appstore.com/mac/apple/keynote

大多数公司和应用程序都有一个规范的App Store简短链接。这个规范的URL是通过更改或删除某些字符(其中许多是非法的或在URL中具有特殊含义的字符(例如,“&”))创建的。

创建App Store简短链接,请按照以下规则使用您的公司或应用程序名称:

删除所有空格

将所有字符转换为小写

删除所有版权(©)、商标(™)和注册标志(®)符号

用“and”替换“&”

删除大部分标点符号(请参见清单2中的集合)

用其元素字符(u、a等)替换重音和其他“装饰”字符(ü,å等)

保留所有其他字符不变。

清单2必须删除的标点符号。

!¡"#$%'()*+,-./:;<=>¿?@[]^_`{|}~

以下是一些示例,以演示进行的转换。

App Store

公司名称示例

Gameloft => http://appstore.com/gameloft

Activision Publishing, Inc. => http://appstore.com/activisionpublishinginc

陈氏摄影与软件 => http://appstore.com/chensphotographyandsoftware

应用程序名称示例

Ocarina => http://appstore.com/ocarina

Where’s My Perry? => http://appstore.com/wheresmyperry

Brain Challenge™ => http://appstore.com/brainchallenge


1
这实际上是目前 Mac App Store 上唯一可靠运行的东西。 - Radu Simionescu
了解这一点很好,但不幸的是,在iOS上,这将通过Safari重定向(并显示一个丑陋的“无法验证服务器身份”的消息)。 - sudo make install
请注意 - "这些应用商店短链接仅供参考,不能保证链接到特定的应用或公司。"基本上是随机给定的。 - durazno

41

自2015年夏季起...

-(IBAction)clickedUpdate
{
    NSString *simple = @"itms-apps://itunes.apple.com/app/id1234567890";
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:simple]];
}

用'id'和你的十位数替换'id1234567890'

  1. 所有设备上都能完美运行。

  2. 它会直接进入应用商店,没有重定向

  3. 对于所有国家商店都可以。

  4. 确实应该转而使用 loadProductWithParameters,但是如果链接的目的是要更新你当前所在的应用程序:可能更好地使用这种“老式”的方法。


此外,如果您想向用户展示您的应用程序目录,可以使用以下代码来指向您的开发者页面: NSString *simple = @"itms-apps://itunes.com/apps/YOUR_COMPANY_NAME"; - TheBestBigAl
3
我该在哪里找到这个ID? - mcfly soft
3
2015年夏天?!感觉整个世界都住在同一个国家。 :( - nacho4d

37

5
为 itunes.apple.com/linkmaker 点赞,大家在设备上测试链接而不是模拟器上,我之前在测试模拟器上浪费了时间。 - karthik
3
谢谢关于模拟器的信息,我会将其添加到答案中。 - dulgan
1
这是现在的 https://tools.applemediaservices.com/app-store/ - rwcorbett

31

这段代码在iOS上生成App Store链接

NSString *appName = [NSString stringWithString:[[[NSBundle mainBundle] infoDictionary]   objectForKey:@"CFBundleName"]];
NSURL *appStoreURL = [NSURL URLWithString:[NSString stringWithFormat:@"itms-apps://itunes.com/app/%@",[appName stringByReplacingOccurrencesOfString:@" " withString:@""]]];

在 Mac 上将 itms-apps 替换为 http:

NSURL *appStoreURL = [NSURL URLWithString:[NSString stringWithFormat:@"http:/itunes.com/app/%@",[appName stringByReplacingOccurrencesOfString:@" " withString:@""]]]; 

iOS中打开URL:

[[UIApplication sharedApplication] openURL:appStoreURL];

苹果电脑:

[[NSWorkspace sharedWorkspace] openURL:appStoreURL];

这个可以运行,但你还需要从应用程序名称中删除像破折号这样的特殊字符。 - TotoroTotoro
我们能否在一个页面中打开并展示开发者上传的所有应用程序? - Imran
这是唯一一个对我有效的即插即用解决方案,最小化重定向并最大化处理应用程序名称中的空格。 - LordParsley

29

19

只使用应用程序ID,这个方法完美地适用于我:

 NSString *urlString = [NSString stringWithFormat:@"http://itunes.apple.com/app/id%@",YOUR_APP_ID];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];

重定向次数为零。


13

许多答案建议使用 'itms' 或 'itms-apps',但这种做法并没有得到苹果公司的特别推荐。他们只提供了以下打开应用商店的方式:

清单1 从iOS应用程序启动应用商店

NSString *iTunesLink = @"https://itunes.apple.com/us/app/apple-store/id375380948?mt=8";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:iTunesLink]];

请参考此答案发布日期为2014年3月的更新内容:https://developer.apple.com/library/ios/qa/qa1629/_index.html

对于支持iOS 6及以上版本的应用程序,苹果提供了一个内置机制来展示App Store:SKStoreProductViewController

- (void)loadProductWithParameters:(NSDictionary *)parameters completionBlock:(void (^)(BOOL result, NSError *error))block;

// Example:
SKStoreProductViewController* spvc = [[SKStoreProductViewController alloc] init];
spvc.delegate = self;
[spvc loadProductWithParameters:@{ SKStoreProductParameterITunesItemIdentifier : @(364709193) } completionBlock:^(BOOL result, NSError *error){ 
    if (error)
        // Show sorry
    else
        // Present spvc
}];

请注意,在iOS6上,如果出现错误,可能不会调用完成块。这似乎是iOS 7中解决的一个错误。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接