如何在App Store(iTunes)中链接到我的应用程序?

27
我希望我的应用程序可以有一个功能,让用户通过电子邮件向朋友发送我的应用程序的iTunes链接。如何实现?
谢谢。
5个回答

47
与通常看到的冗长混乱的URL不同,您可以创建更简单和更合理的App Store链接。iTunes Store有一种隐藏的URL格式,这种格式更加合理。根据您要链接到的内容,您只需要构建其中一种格式的URL:
  1. 艺术家姓名或App Store开发者姓名:http://itunes.com/Artist_Or_Developer_Name
  2. 专辑名称:http://itunes.com/Artist_Name/Album_Name
  3. 应用程序:http://itunes.com/app/App_Name
  4. 电影:http://itunes.com/movie/Movie_Title
  5. 电视:http://itunes.com/tv/Show_Title
只需在创建的电子邮件正文中包含此格式的URL即可。
(请注意,空格可能会导致问题,但我发现完全省略它们对我有用 - http://itunes.com/app/FrootGroove 会重定向到名为“Froot Groove”的应用程序。)

请注意,如果此方法对您不起作用,则可以使用iTunes链接生成器此处

您的代码将类似于以下内容(从我的代码中提取,已匿名化且未经测试)

NSString* body = [NSString stringWithFormat:@"Get my app here - %@.\n",myUrl];

#if __IPHONE_OS_VERSION_MIN_REQUIRED <= __IPHONE_2_2
[NSThread sleepForTimeInterval:1.0];
NSString* crlfBody = [body stringByReplacingOccurrencesOfString:@"\n" withString:@"\r\n"];
NSString* escapedBody = [(NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,  (CFStringRef)crlfBody, NULL,  CFSTR("?=&+"), kCFStringEncodingUTF8) autorelease];

NSString *mailtoPrefix = [@"mailto:xxx@wibble.com?subject=Get my app&body=" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

// Finally, combine to create the fully escaped URL string
NSString *mailtoStr = [mailtoPrefix stringByAppendingString:escapedBody];

// And let the application open the merged URL
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailtoStr]];
#endif

iPhone 3.0 可以做更好的事情,但我现在还不能谈论那些。


哇,我从来不知道你可以这样做。谢谢你的帖子! - Lounges
一个快速的问题 - 如果我的应用程序显示名称(在info.plist中)和iTunes名称(如在iTunes Connect中指定)不同,我应该在URL中使用哪个?再次感谢 - lostInTransit
我不确定,但怀疑是iTunes的名称。 - Jane Sales
6
OS 3.0有哪些新功能? - David Maymudes
这确实可以工作,但它似乎会重定向到搜索页面。如果你是该搜索的唯一匹配项,那么你就赢了。如果不是,那就遗憾了。 - Jane Sales
那个链接制作器参考是正确的方法。Google Play在https://developer.android.com/distribute/tools/promote/badges.html上有一个类似的页面。 - infomaniac50

4

在 OS 3.0 中,您可以使用 MessageUI 框架来实现此操作,而无需离开应用程序(对于早期版本的设备,使用 Jane 的代码作为后备):

- (void)sendEmail
{
    NSString* body = [NSString stringWithFormat:@"Get my app here - %@.\n",myUrl];

#if __IPHONE_OS_VERSION_MIN_REQUIRED <= __IPHONE_2_2
    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    if (mailClass != nil && [mailClass canSendMail])
    {
        MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
        picker.mailComposeDelegate = self;
        picker.subject = @"Get my app";
        [picker setToRecipients:[NSArray arrayWithObject:@"xxx@wibble.com"];
        [picker setMessageBody:body isHTML:NO];

        [self presentModalViewController:picker animated:NO];
        [picker release];
    } else {
        [NSThread sleepForTimeInterval:1.0];
        NSString* crlfBody = [body stringByReplacingOccurrencesOfString:@"\n" withString:@"\r\n"];
        NSString* escapedBody = [(NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,  (CFStringRef)crlfBody, NULL,  CFSTR("?=&+"), kCFStringEncodingUTF8) autorelease];

        NSString *mailtoPrefix = [@"mailto:xxx@wibble.com?subject=Get my app&body=" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        // Finally, combine to create the fully escaped URL string
        NSString *mailtoStr = [mailtoPrefix stringByAppendingString:escapedBody];

        // And let the application open the merged URL
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailtoStr]];
    }
#endif
}

#pragma mark -
#pragma mark Mail Composer Delegate
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{
    if (result == MFMailComposeResultFailed) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[error localizedDescription] message:[error localizedFailureReason] delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", @"OK") otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
    [self dismissModalViewControllerAnimated:YES];
}

请注意,您的类必须采用MFMailComposeViewControllerDelegate协议。您还可以包含附件,在正文中使用HTML等。

3
现在您可以使用appstore.com/APP_NAME在iTunes中启动应用程序。这适用于桌面和iOS设备。但是,这不像其他方法那样可靠。请参见此处的答案如何为苹果AppStore创建虚拟URL?

1
这段代码会根据应用名称自动生成应用商店链接,无需其他操作,只需拖放即可:
NSCharacterSet *trimSet = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLKMNOPQRSTUVWXYZ0123456789"] invertedSet];    
NSArray *trimmedAppname = [[NSString stringWithString:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"]] componentsSeparatedByCharactersInSet:trimSet];
NSString *appStoreLink = @"http://itunes.com/app/"; 
for (NSString *part in trimmedAppname) appStoreLink = [NSString stringWithFormat:@"%@%@",appStoreLink,part];
NSLog(@"App store URL:%@",appStoreLink);

它会给你一个链接,像这样 http://itunes.com/app/angrybirds


1
你不能只做这个。苹果还会用连字符替换所有特殊字符(如撇号、逗号、和符号等)。 - lostInTransit
好的,我没有这些,只有空格。但是对于大多数情况,可以从应用程序名称中提取URL,而不需要知道应用程序ID或其他任何信息。我会在有机会时更新代码。这个代码对于大多数情况都可以工作,但并非所有情况都适用。 - Tibidabo
我更新了代码,现在应该过滤掉所有非字母数字字符。感谢你的提示! - Tibidabo

0

顺便提一下,通过访问应用商店并点击“告诉朋友”按钮,您可以找到应用程序的ID链接,然后将电子邮件发送给自己。我发现这非常有用。


或者在iTunes(桌面应用程序)中右键单击应用程序名称,然后复制链接。这也会给您链接 :) - lostInTransit

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