2011-09-02 72 views
2

我正在制作自动更新自己的应用程序。我想要检查文件是否存在于域中,我之前从未做过这样的事情,因此我找到了一个代码并将其改编以适合我的项目。问题是,我总是得到该文件存在。帮助制作自动更新程序

-(BOOL) webFileExists { 

int fcurVer = [[self getCurrentVersion] intValue]; 
fcurVer++; 
NSLog(@"%i", fcurVer); 
NSString *url = [NSString stringWithFormat:@"http://www.site.org/folder/version/%i.dmg", fcurVer]; 

NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0]; 
NSHTTPURLResponse* response = nil; 
NSError* error = nil; 
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
NSLog(@"statusCode = %i", [response statusCode]); 

if ([response statusCode] == 404) { 
    return NO; 

} 
else { 
    return YES; 
} 

}

任何想法?顺便说一下,如果有人也知道如何下载文件,并安装它替换旧文件,请帮助我。

回答

5

您的问题已经被Sparkle解决了。它被广泛使用,经过了用户的充分测试和信任,为什么要重新发明这个轮子?

此外,它的开源和hosted on github所以你可以看看它是如何实现的。