2011-09-02 71 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所以你可以看看它是如何實現的。