我使用以下方法检查我的应用是否有连接。它很简单,适合我的需求。
+ (void)checkInternet:(connection)block
{
NSURL *url = [NSURL URLWithString:@"http://www.google.com/"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"HEAD";
request.cachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;
request.timeoutInterval = 10.0;
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:
^(NSURLResponse *response, NSData *data, NSError *connectionError)
{
block([(NSHTTPURLResponse *)response statusCode] == 200);
}];
}
然而,我想要做的是,如果状态没有返回200,我想再次检查,至少几次。以1秒为间隔进行此操作的最佳方式是什么?
以下是我如何调用上述方法。
[self checkInternet:^(BOOL internet)
{
if (internet)
{
// "Internet" aka Google
}
else
{
// No "Internet" aka no Google
}
}];
答案 0 :(得分:1)
我使用Reachability来检测常规网络连接问题(请参阅答案结束)。我使用以下方法执行重试。
- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay;
你可以调整你的系统,使其具有一个新的类方法,该方法具有可选的重试次数。
NB。未测试以下内容。它只是为了给你一般的想法。
// Variable to track number of retries left. If you had a shared instance
// a property would be easier.
static NSUInteger maxConnectionTries = 0;
// New method which lets you pass a retry count.
+ (void)checkInternet:(connection)block withMaxTries:(NSUInteger)maxTries
{
maxConnectionTries=maxTries;
[self checkInternet:block];
}
// Your original code extended to retry by calling itself when code 200
// is seen on a delay of 1s. Defaults to old code when retry limit exceeded
// or non 200 code received.
+ (void)checkInternet:(connection)block
{
NSURL *url = [NSURL URLWithString:@"http://www.google.com/"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"HEAD";
request.cachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;
request.timeoutInterval = 10.0;
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:
^(NSURLResponse *response, NSData *data, NSError *connectionError)
{
if ([(NSHTTPURLResponse *)response statusCode] != 200 &&
maxConnectionRetries > 0){
maxConnectionRetries--;
[self performSelector:@selector(checkInternet:) withObject:block afterDelay:1.0];
}
else{
maxConnectionRetries = 0;
block([(NSHTTPURLResponse *)response statusCode] == 200);
}
}];
}
对于互联网连接的一般检测,最好使用可达性。请参阅here。
我从AppDelegate代码启动可访问性处理程序,然后在发生连接更改时发布本地通知。这允许应用程序始终在viewWillAppear
和viewWillDisappear
内接收连接更改通知和瞬态视图控制器,以便在他们对连接更改感兴趣时注册和取消注册本地通知。
答案 1 :(得分:0)
这是我提出的:
+ (void)checkInternet:(connection)block withMaxTries:(NSUInteger)maxTries
{
NSURL *url = [NSURL URLWithString:@"http://www.google.com/"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"HEAD";
request.cachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;
request.timeoutInterval = 10.0;
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:
^(NSURLResponse *response, NSData *data, NSError *connectionError)
{
if ([(NSHTTPURLResponse *)response statusCode] != 200 &&
maxTries > 0){
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self checkInternet:block withMaxTries:maxTries - 1];
});
}
else{
block([(NSHTTPURLResponse *)response statusCode] == 200);
}
}];
}