0

Previous code

[NSURLConnection sendSynchronousRequest:request returningResponse:&response error: NULL]; return ([response statusCode] == 200) ? YES : NO;

Code using now

+(BOOL)isConnectNetwork{

NSString *urlString = @"http://www.google.com/";
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"HEAD"];
NSHTTPURLResponse *response;



  NSURLSession *session = [NSURLSession sharedSession];
   NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:
       ^(NSData *data, NSURLResponse *response, NSError *error) {
           // ...
    }];
   [task resume];
      }

i get this error 2020-11-06 13:07:36.125607+0000 App[8518:1786305] [NetworkInfo] Signal strength query returned error: Error Domain=NSPOSIXErrorDomain Code=13 "Permission denied", descriptor: <CTServiceDescriptor 0x280000600, domain=1, instance=1> 2020-11-06 13:16:03.381478+0000 App[8518:1786223] Could not load IOSurface for time string. Rendering locally instead.

Kofi Sammie
  • 3,237
  • 1
  • 17
  • 17

1 Answers1

1

Doing this sort of network check is highly discouraged, because it is error-prone. It is usually much better to just assume the network is working until a real network request (something that you actually care about) fails, and then handle that failure appropriately (error message, background color change, whatever).

The reason not to do this is that on mobile devices, networks can work one second and not work one second later. So doing unnecessary network checks like this provides no benefit, but can potentially make your app less usable (particularly if you gate any features behind that check, which again, is highly discouraged).

My advice would be to just delete this method entirely, and just pretend that it always returns YES. In the long run, you'll be much happier with that approach. Handle errors when they happen. Don't try to seek them out.

That said, if you must do this for some reason, the problem you'll face is that NSURLSession tasks are asynchronous, but you're trying to use it synchronously. So to make that work, you would need to

  • Wrap all of this code with a dispatch_async block onto a different dispatch queue.
  • Outside of that block, block the thread that the method is running on so that the method won't return. You would typically do that by waiting on a semaphore.
  • Inside the completion block, set the value of a __block BOOL variable based on whether the request completed successfully or failed, then post to the semaphore to make the outer code stop waiting.

But again, that's highly discouraged. I've done this as a workaround in spots where there was no other way (involving swizzling code that I didn't control), but unless there's no other way, please don't do that. :-)

dgatwood
  • 10,129
  • 1
  • 28
  • 49
  • How would you implement it using what i have.? @dgatwood – Kofi Sammie Jul 04 '21 at 09:28
  • Is this iOS? If so, I wouldn't. This approach is likely to get your app rejected by Apple if they catch it. When I say "please don't do that," I mean "If you do that, you're taking a major risk." – dgatwood Jul 04 '21 at 15:47