0

我正在使用 ASIHTTPRequest 与我的网站进行交互。我基本上是将 ASIHttpRequests 添加到队列中。如果有互联网连接,一切正常。但是,我猜如果请求失败(即没有互联网连接),操作会被删除

有没有办法仅在有互联网连接时排队和执行请求?如果没有互联网连接,请求应该排队并在连接返回时自动发送。如果这是不可能的,那么完成我在这里尝试的最佳方法是什么?

谢谢!

这就是我现在设置队列的方式:

NSURL *url = [NSURL URLWithString:@"http://www.tst.com.au/test.php?op=updateField"];

        ASIFormDataRequest *startHourRequest = [ASIFormDataRequest requestWithURL:url];

        NSDateFormatter *apiformatter = [[ [NSDateFormatter alloc] init] autorelease];
        [apiformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSString *webdate = [apiformatter stringFromDate:date];
        [startHourRequest setDelegate:appDelegate];
        [startHourRequest setPostValue:webdate forKey:@"fieldvalue"];
        [startHourRequest setPostValue:[appDelegate shiftuid] forKey:@"uid"];
        [startHourRequest setPostValue:editAttribute forKey:@"fieldname"];
        [startHourRequest setDidFinishSelector:@selector(requestDone:)];
        [startHourRequest setDidFailSelector:@selector(requestWentWrong:)];
        [[appDelegate queue] addOperation:startHourRequest];
4

1 回答 1

1

是的,您可以使用 Reachability 类很容易地做到这一点:

首先,您将在发送初始请求之前检查 Internet 可访问性:

Reachability *reachability = [Reachability reachabilityForInternetConnection];    
NetworkStatus internetStatus = [reachability currentReachabilityStatus];
if (internetStatus != NotReachable) {
    //Do your request
}
else {
    //save your url to an array for use when the internet returns
}

并在视图控制器或您发送请求的任何地方执行此操作以侦听 Internet 更改

- (void)viewDidLoad

    [[NSNotificationCenter defaultCenter]
     addObserver:self 
     selector:@selector(networkStatusChanged:) 
     name:kReachabilityChangedNotification 
     object:nil];

    internetReachable = [Reachability reachabilityForInternetConnection];
    [internetReachable startNotifier];
}

- (void)networkStatusChanged:(NSNotification *)notice {
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    if (internetStatus != NotReachable && [yourUrlsToSend count] > 0) {
        //send off your requests now that you have internet

    }
}

这一切都取决于包括来自苹果的 reqchability 文件,可以在此处找到:在此处输入链接描述。希望有帮助

于 2012-06-10T15:04:49.433 回答