我遇到了一个奇怪的问题,在从wifi切换到3g 之后,Reachability和NSURLConnection显示错误(没有互联网连接/网络连接丢失)。虽然切换到3g后所有应用都运行良好。 我仔细检查是否有应用程序和设备的移动数据。重新安装应用程序它工作正常,请帮忙。 这是一些可达性代码:
-(BOOL)toCheckNetworkStatus
{
internetReachable = [Reachability reachabilityForInternetConnection];
[internetReachable startNotifier];
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
if(internetStatus == NotReachable ){
[self displayToastWithMessage:@"Connection lost!" AndSubtitle:@"Please check your network connection." delay:3.2];
return NO;
}
else{
return YES;
}
}
NSURLConnection代码:
NSError *error;
NSData *postData = [NSJSONSerialization dataWithJSONObject:dictionary
options:0
error:&error];
if (error)
NSLog(@"Failure to serialize JSON object %@", error);
NSURL *url = [NSURL URLWithString:palURL];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:TIMEOUTINTERVAL];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:postData];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:
^(NSURLResponse *response, NSData *data, NSError *connectionError)
{
if (data)
{
NSMutableDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"UPLOADED LOCATION RESPONSE = %@",responseDict);
}
else
{
NSLog(@"connectionError===%@",connectionError);
}
}];