这就是我按照@mattt 的建议对类似问题所做的。我的 Singleton InApp 助手有productDownloadURL
ivar 和一个prepareForDownload
返回AFHTTPRequestOperation
给调用者的方法:
- (AFHTTPRequestOperation * )prepareForDownload:(NSString *)productIdentifier
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:_productDownloadURL]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:productIdentifier];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
return operation;
}
我的RootViewController通过使用AFHTTPRequestOperation
和设置downloadProgress
//块发出请求success
,failure
如下所示:
AFHTTPRequestOperation *operation = [[InAppRageIAPHelper sharedHelper] prepareForDownload:productIdentifier];
[operation setDownloadProgressBlock:^(NSInteger bytesRead, NSInteger totalBytesRead, NSInteger totalBytesExpectedToRead) {
float percentDone = ((float)((int)totalBytesRead) / (float)((int)totalBytesExpectedToRead));
[(UIProgressView *)_hud.customView setProgress:percentDone];
_hud.labelText = [NSString stringWithFormat:@"%f",percentDone];
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
_hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"success.png"]];
[self performSelector:@selector(dismissHUD:) withObject:nil afterDelay:1.5];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
_hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error.png"]];
}];
[operation start];
hud是一个MBProgressHUD。MBProgressHUDModeDeterminate
您还可以使用模式增强进度显示。