以编程方式点击HTML href以更新应用程序

5

我正在计划更新一款企业应用,需要进行ad-hoc分发。

对于更新,苹果公司建议用户访问一个HTML页面并点击链接:

href="itms-services://?action=download-manifest&url=http://example.com/
manifest.plist"

请参考http://help.apple.com/iosdeployment-apps/#app43ad871e

我不想这样做。我希望应用程序在启动时以编程方式检查更新,并使用UIAlertView向用户发出提示,表示有可用的更新。

在didFinishLaunching中,我已经实现了以下内容。复杂的plist解析来自于此处找到的示例plist的结构:http://help.apple.com/iosdeployment-apps/#app43ad78b3

NSLog(@"checking for update");
NSData *plistData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://example.com/MyApp.plist"]];
if (plistData) {
    NSLog(@"finished checking for update");
    NSError *error;
    NSPropertyListFormat format;
    NSDictionary *plist = [NSPropertyListSerialization propertyListWithData:plistData options:NSPropertyListImmutable format:&format error:&error];
    if (plist) {
        NSArray *items = [plist valueForKey:@"items"];
        NSDictionary *dictionary;
        if ([items count] > 0) {
            dictionary = [items objectAtIndex:0];
        }
        NSDictionary *metaData = [dictionary objectForKey:@"metadata"];

        float currentVersion = [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] floatValue];
        float newVersion = [[metaData objectForKey:@"bundle-version"] floatValue];
        NSLog(@"newVersion: %f, currentVersion: %f", newVersion, currentVersion);
        if (newVersion > currentVersion) {
            NSLog(@"A new update is available");
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Update available" message:@"A new update is available." delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"UPDATE", nil];
            [alert show];
        }       
    }
}

然后我有我的UIAlertView委托方法:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        NSLog(@"downloading full update");
        UIWebView *webView = [[UIWebView alloc] init];
        [webView loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"itms-services://?action=download-manifest&url=http://example.com/MyApp.plist"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0]];
    }
}

有几个问题:

  • 我知道 [alert show] 不应该在 application didFinish 中被调用,但我会稍后更改它。
  • 我不知道 plistData 将被下载多快以及这种下载如何影响应用程序。
  • 更重要的是,我的警报视图委托方法不起作用,并且更新没有下载。即使我使用 @property (nonatomic, strong) UIWebView *webView 引入 webView,该方法也没有做任何事情。
  • 我认为 Dropbox 已经正确配置了 MIME,因为我可以通过 google Chrome 下载 .ipa。

所以我真正需要的是一个使用 NSURLConnection(NSURLRequest 等)的方法来复制用户点击 HTML href 的行为。之后我认为完整的更新将发生。

1个回答

5

您可以使用以下方法自动打开一个URL:

[[UIApplication sharedApplication] openURL:...];

我不确定它是否适用于itms-services:urls,但它适用于其他定制的URL方案,如tel:,fb:等等。所以除非苹果公司专门阻止它,否则它应该是有效的。


1
苹果似乎已经开始阻止以这种方式使用itms-services。幸运的是,用户仍然可以被要求点击涉及itms-services的href链接。 - David Braun
我也尝试在应用程序内打开itms-services URL,但未成功。我猜这只是意味着用户需要多点击几次才能找到并按下网页上的下载按钮。不确定为什么他们会阻止这个操作,如果它可以从网页上完成的话。 - Justin Caldicott
这在模拟器上似乎不起作用,但在真实设备上没问题。让我困惑了一段时间。 - Justin Caldicott
@JustinCaldicott 这是因为模拟器上没有 App Store.app。 - Nick Lockwood
@JustinCaldicott,实际上很抱歉,这并不是那个原因(itms-apps://不能通过应用商店)。这可能只是因为您无法在模拟器上安装 ad-hoc 构建(它们是为 ARM 构建的,而不是 x86,并且具有错误的配置文件类型)。 - Nick Lockwood

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