我试图做它恢复和排队从网络连接丢失时的位置相同的点。我的问题不在于可达性,而在于恢复下载。我花了整个周末,但没有运气。
非常感谢您的帮助。如何恢复ASINetworkQueue?
下面的代码有方法commenceDownloadIfReachabilityIsOkay
当网络恢复但进度条不移动时,它会被调用,但我没有看到任何活动来判断下载是否恢复。
调用此方法frin IBAction为在控制器
- (void) downloadFile:(UIProgressView*)locprogressView;
{
// check for internet connection
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
internetReachable = [[Reachability reachabilityForInternetConnection] retain];
[internetReachable startNotifier];
// check if a pathway to a random host exists
hostReachable = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
[hostReachable startNotifier];
if (![self asiqueue]) {
[self setAsiqueue:[[[ASINetworkQueue alloc] init] autorelease]];
}
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/Tests/the_great_american_novel.txt"];
self.request = [ASIHTTPRequest requestWithURL:url];
_progressView=locprogressView;
self.progressView= locprogressView;
[asiqueue cancelAllOperations];
[asiqueue setDownloadProgressDelegate:self.progressView];
[asiqueue setDelegate:self];
[asiqueue setRequestDidFinishSelector:@selector(queueComplete:)];
[asiqueue setShowAccurateProgress:YES];
// [queue setr
[request setDelegate:self];
// [request setDidReceiveDataSelector:@selector(didReceiveData:)];
[request setDidFinishSelector:@selector(requestDone:)];
[request setDidFailSelector:@selector(requestWentWrong:)];
[request setAllowResumeForFileDownloads:YES];
[[self asiqueue] addOperation:request]; //queue is an NSOperationQueue
[[self asiqueue]go ];
}
,此方法被从checkNetworkStatus
叫,我希望它继续下载
- (void)commenceDownloadIfReachabilityIsOkay
{
if (self.asiqueue && self.hostActive && self.internetActive)
{
[[self asiqueue]go ];
}
}
这种方法只是不断轮询检查网络状态 - 取自How to check for an active Internet connection on iOS or OSX?
- (void) checkNetworkStatus:(NSNotification *)notice
{
// called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
NSLog(@"The internet is down.");
self.internetActive = NO;
break;
}
case ReachableViaWiFi:
{
NSLog(@"The internet is working via WIFI.");
self.internetActive = YES;
break;
}
case ReachableViaWWAN:
{
NSLog(@"The internet is working via WWAN.");
self.internetActive = YES;
break;
}
}
NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
switch (hostStatus)
{
case NotReachable:
{
NSLog(@"A gateway to the host server is down.");
self.hostActive = NO;
break;
}
case ReachableViaWiFi:
{
NSLog(@"A gateway to the host server is working via WIFI.");
self.hostActive = YES;
break;
}
case ReachableViaWWAN:
{
NSLog(@"A gateway to the host server is working via WWAN.");
self.hostActive = YES;
break;
}
}
[self commenceDownloadIfReachabilityIsOkay];
}
哇是的,这就是我正确地期待的课程不正确。即使对于ios,我是否需要设置'setDownloadDestinationPath'?因为模拟器无法访问机器磁盘结构,所以可能是一个可能的值。 –
是的,你需要为iOS设置一个有效的路径。 iOS有一个完整的文件系统 - 取决于文件是什么,你可能想将它存储在你的应用程序下的文档目录中,代码将类似于'NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);' – JosephH
非常感谢您的参与。 –