wifi is connected and internet is not available iOS - ios

My question is same as this question, but don't get a proper answer.
If I am connected to wifi, but internet is not available/gone it should be notify. Alslo tested using Apple Reachability demo but still its not working.
Steps to reproduce:
1) connect iPhone using tethering on another device.
2) run the apple rechability demo.
3) turn off the cellular data on another device on wich iPhone is connected.

You can simply call the below code and then call NSURLConnection delegate methods. This way even if wifi is connected and internet not available it can be detected.
NSMutableURLRequest *headerRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com"] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0];
headerRequest.HTTPMethod = #"HEAD";
self.headerConnection = [[NSURLConnection alloc] initWithRequest:headerRequest delegate:self];

Related

Network connection lost when uploading large videos

I am developing an video sharing app. When I am trying to upload videos larger than 10MB NSURLConnection get lost and I got error NSURLErrorDomain Code=-1005 "The network connection was lost." . Video, less than 10 MB are uploading.Can resolution of video be an issue here?? Resolution of my video is 1280*720 .
Can anyone help me to understand issue here. This is my code for connection
NSString *urlString = [[NSString alloc]initWithString:[NSString stringWithFormat:WEBSERVICE_URL]];
urlString=[urlString stringByAddingPercentEscapesUsingEncoding:
NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:100];
[request setHTTPMethod:#"POST"];
This is not duplicate of NSURLConnection GET request returns -1005, "the network connection was lost" because
I am not using third party library for connection and not using simulator. I had tried every thing from that answer.
You might be hitting various timeouts—either the NSURLConnection timeout or a timeout on the server side. iOS may ignore the timeout value you specify, BTW. Or you could just be losing the network connection because your device decided to power down the cellular or Wi-Fi hardware to conserve power. Hard to say which. Or your Wi-Fi connection could have dropped because it missed too many beacons from the access point for some reason. Or....
My advice would be to upload large files a piece at a time, and then assemble the pieces on the server. That way, when (not if) the connection fails, you can re-upload the chunk that only partially transferred, and then continue uploading additional chunks until you've uploaded the whole thing.
See also the Content-Range HTTP header. Obviously, this requires your server-side code to support that functionality, but it is worth the extra effort. :-)
BTW, you forgot to set the Content-Type header.

NSURLConnection times out after reopening app

In my app I use a NSURLConnection to upload some content.
Everything works fine except when I reopen the app after like one hour.
The NSURLConnection will timeout every time at the first try. After that the app works fine.
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:delegate];
[connection start];
Using [NSURLConnection sendAsynchronousRequest:request...] also leads to a timeout.
On another thread on the Apple developer forum somebody suggested to run a packet trace, which I did. The request does not seem to be sent.
I'm a bit out of ideas, do you have one ?
Thanks a lot!

Error Domain=NSURLErrorDomain Code=-1003 "A server with the specified hostname could not be found

I get this error when I try running the app on a device. I do not get this error when I run it on the simulator and the post request works fine. This is code snippet I have:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:300.f];
[request setHTTPMethod:#"POST"];
NSData* data = [paramString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
Any help is appreciated. Thanks
With Xcode 11.2.1, working on a macOS project, I had the same issue.
The fix here was:
Click on project in project explorer
Select the Signing & Capabilities tab
Check "Network: Outgoing connections (client)" checkbox in the AppSandbox section
This error would typically suggest a DNS related issue. Check the DNS settings on your device and confirm you can browse the internet using Safari or another browser (double check by visiting a site you have not visited before to ensure it is not cached).
If there is a url you can GET (instead of POST) on the same server, try visiting it directly in Safari on the device.
The simulator uses your computers network connection, so I recommend checking the System Preferences > Network > Advanced > Proxies > Automatic Proxy Configuration : You must disable Automatic conf.

AFNetworking lib not loading data from cache in offline mode

I am developing a content reading app in which some data is displayed in a tale view and respective detail views.
Now I have already completed the app but there is a small bug.
I am using AFNetworking library for online data load and offline caching.
I have defined caching policy as described by following code.
Reachability *reach = [Reachability reachabilityWithHostname:#"google.com"];
if ([reach isReachable])
{
// Reachable
request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60.0];
}
else
{
request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReturnCacheDataDontLoad
timeoutInterval:60.0];
}
But I am having a small problem that if I load some data online and then turn off the internet connection and close the app.
And afterwords if I restart the app again in offline mode, the cached data should load but it is not happening.
I also have tried changing the caching policies as defined in the following link:
http://blog.originate.com/blog/2014/02/20/afimagecache-vs-nsurlcache/
app deployment target : iOS 6.0
devices : universal
xcode version : 6.1
AFNetworking lib version : 2.0
Any suggestion is appreciated, thanks in advanced

AFNetworking lib not loading data in offline mode in iOS

I am developing a content reading app in which some data is displayed in a tale view and respective detail views.
I have already completed the app but there is a small bug.
I am using AFNetworking library for online data load and offline caching.
I have defined caching policy as described by following code:
Reachability *reach = [Reachability reachabilityWithHostname:#"google.com"];
if ([reach isReachable]) {
// Reachable
request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60.0];
}
else{
request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReturnCacheDataDontLoad
timeoutInterval:60.0];
}
But, I am having a small problem that if I load some data online and then turn off the internet connection and close the app, and afterwords if I restart the app again in offline mode, the cached data should load but it is not happening.
I also have tried changing the caching policies as defined in this link.
app deployment target : iOS 6.0
devices : universal
xcode version : 6.1
AFNetworking lib version : 2.0
Replace all of the code you posted and just set the request cache policy to NSURLRequestUseProtocolCachePolicy.
As it stands, you're currently making a synchronous reachability query on every request. Either that or worse—it's returning immediately, but always returning before a real reachability state can be determined.

Resources