Call "didFailLoadWithError" when no internet connection? - ios

I already googled but didn't find a clear answer. When is "didFailLoadWithError" called? I want to use it to show an alert when there's no internet connection. Is that the right way?
It's for my iOS app made with Xcode and Swift.

didFailLoadWithError method will get called in the following conditions:
If the URL redirects to another URL, we will receive an
NSURLErrorCancelled error.
If the page contains links to the AppStore, tapping the link will return an error (but the AppStore link will still be handled by
iOS).
If the URL is a direct link to a Video/Audio, we will receive an error (“Plug-in handled load”) even if the video/audio will play.
So the best solution to check network is to use apple Reachability code.
Update:
The best solution to check for Internet connection (Wi-Fi and cellular) is: [Reachability Swift 2][2]

Related

didFailProvisionalNavigation error not localized

I have a localized app. I use WKWebView and also make some network requests. All the app localization works as expected. But errors from webView:didFailProvisionalNavigation:withError are always in English. I haven't found any information about any bugs on WebKit or Error side, but localizedDescription is always in English. Why?
Steps to reproduce:
Just create WKWebView and assign its delegate to self;
handle webView:didFailProvisionalNavigation:withError;
Enable airplane mode, switch off wifi and try to load any page;
You'll get connection error with wrong localization.
Finally, I have found a confirmation, that it's an Apple bug.

com.google.GCKError Code=2 when connecting to ChromeCast device

I'm working on an iOS app where I now want to integrate ChromeCast support. I use the HelloGoogleVideoCast example to test my hardware setup and get inspiration for my app.
I have now stumbled on a problem that is present both in my app and in HelloGoogleVideoCast. When I connect to a device (connectToDevice method in HelloGoogleVideoCast) I get error "The operation couldn’t be completed. (com.google.GCKError error 2.)". I have power-cycled the device with no change in behavior. I never get to the callback deviceManagerDidConnect, instead deviceManager:didFailToConnectWithError is called with this error. Still, I can connect with the iOS YouTube app.
I can't find anything with substance if I google it and I can't find any good ChromeCast developer forums.
Is there anybody who has an inroad to how to solve this?
I m not sure it will help but u can try:
Download the Chromecast app from app store:
chromecast link
After u see your chromecast -> select your chromecast -> General -> make sure both check box are selected (I know the text is nothing to do with your problem).
Try now.
If the error still occur, try to reboot your chromecast.
If your error still occur, try to reset your chromecast, and then check the boxes again (don't forget!).
Make sure that you initialize the Receiver Manager in your receiver app as soon as possible. Don't wait for the window.onload. If the receiver takes too long to load and initialize the receiver manager, the connection is automatically torn down.
I had the same problem as you did and making the following changes on receiver worked for me:
I've put the script src="//www.gstatic.com/cast/sdk/libs/receiver/2.0.0/cast_receiver.js" inside head tag and before title tag;
I've put my script file which managed all cast functions after
body tag (actually I've put all script declarations, except for cast_receiver, after body tag, including jQuery);
I hope it can help you yet :)

Objective-C/CocoaHTTPServer - Is it possible to return response to the app?

I am developing an iPhone app which is using CocoaHTTPServer for making remote server communication.
The app will send the request details to the CocoaHTTPServer which will store the request locally. Once the internet connectivity is available, CocoaHTTPServer will send the request to remote server & will get the server response now CocoaHTTPServer has to send this response back to the app,
But I am confused how to implement it. Is there any inter app communication api for the same?
Any suggestions are greatly appreciated.
Well , I haven't workaround CocoaHTTP server classes so can't explain you verywell but I found there are couple of tutorials will surly guide you.
Thanks to Matt Gallagher for such a detailed article.
You can listen for a connection using NSFileHandle class
listeningHandle = [[NSFileHandle alloc]
initWithFileDescriptor:fileDescriptor
closeOnDealloc:YES];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(receiveIncomingConnectionNotification:)
name:NSFileHandleConnectionAcceptedNotification
object:nil];
[listeningHandle acceptConnectionInBackgroundAndNotify];
When receiveIncomingConnectionNotification: is invoked, each new incoming connection will get its own NSFileHandle. If you're keeping track, you can handle received message
if(CFHTTPMessageIsHeaderComplete(incomingRequest))
{
HTTPResponseHandler *handler =
[HTTPResponseHandler
handlerForRequest:incomingRequest
fileHandle:incomingFileHandle
server:self];
[responseHandlers addObject:handler];
[self stopReceivingForFileHandle:incomingFileHandle close:NO];
[handler startResponse];
return;
}
Note : please go through the full article, it has nice explanation.
Apart from this you may have look on this as well.
Hope this will give you some idea.
You question is focussing on background proces.
When an App goes into background, it get very limited time to finish things up. After that the App freezes in background. That is not a good situation to start communication.
Apple states clearly that the priority is always on the running foreground tasks.
The Notification mechanism (as listed by RDC above) is created to handle external events. During such a wake up you can send/receive a little bit of data, however you'll get minimal priority. Since timing is important in communication, I would not go for that either.
I suggest checking communication during the wakeup call and start activities then. And use the Notification mechanism to wakeup the user, that network is up again.
URL scheme can be used to send the response back to the app. The response from the remote server can be set as a parameter in the URL. The CocoaHTTPServer can invoke the other app which will be the handler of this unique URL. The below link provides more information on the same.
Inter-AppCommunication using URL scheme

How to handle UIWebView cases were no connection, and should I use Network indicator?

What´s the preferred way of handling an app that an integral part of it is using UIWebView, the first view the user sees when app is first loaded is a UIWebView trying to access a webpage... If the connection is slow or if there is no connection at all - how should I handle this?
also, does UIWebView require you to use the network indicator (spinner) when it´s using the network?
Always try to provide feedback to the user, it helps in increasing usability. So, yes spinners(activity and network indicators) are very useful. You might also learn a lot by looking at other apps as to how they handle network errors. Facebook for example shows a nice message saying "Network Unreachable, Try again". You can implement actions like this which will always let the user know how to proceed.
As to handling network connection, Apple provides Reachability class. Look at the attached example to understand how to implement this -
https://developer.apple.com/library/ios/samplecode/Reachability/Introduction/Intro.html
you can use activityindicator to show loading....
and then stop indicator in UIWebView delegate method
-(void)webViewDidFinishLoad:(UIWebView *)webView

force application to terminate in iPhone

I am developing an iPhone application which is completely based on web data.
If it is not connected to the internet, the application is of no use.
So, I want to terminate the application when connection is not found.
NSURL *tmpURl=[NSURL URLWithString:[NSString stringWithFormat:#"%#search.php",[iGolfAppDelegate getServerPath]]];
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:tmpURl];
con=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if(con){
myWebData=[[NSMutableData data] retain];
} else {
//Yes I will provide two buttons on alertview "retry" & "close", & when user
//taps on "close" => application should terminate.
// i will send alertview & when user taps on button close then
// what to write for terminating application?
// Ok Ok. Don't terminate. User will terminate.
// user is owner of iPhone
// let him choose what to do
// wait till wifi connects
}
The question is how to terminate the application?
Is exit(0) only the option for terminating application or is there any other option available?
Apple is is absolutely clear about this topic:
There is no API provided for
gracefully terminating an iPhone
application. Under the iPhone OS, the
user presses the Home button to close
applications. Should your application
have conditions in which it cannot
provide its intended function, the
recommended approach is to display an
alert for the user that indicates the
nature of the problem and possible
actions the user could take - turning
on WiFi, enabling Location Services,
etc. Allow the user to terminate the
application at their own discretion.
See Technical Q&A QA1561
You might consider informing the user that they cannot use your application without an active network connection. Just terminating the application outright seems like a very unfriendly way of doing this; the user will simply see the app "disappear".
Every well-behaved app I've seen will at least give a notification before terminating.
I would advise you to reconsider for 3 reasons
It may appear that your app crashed.
The user may get an internet connection while your app is up. In this case a 'Retry' would be best.
I think Apple may actually not accept the app if it does that. It is for sure not what they would do if an Apple application needed an internet connection, and they do test to see what an app will do without a connection.
If (for whatever reason)you do want to do it you can use.
exit(0);
You could always just divide by zero. As a bonus, the implementation would reflect what a good idea this is.
Hope this helpful
[[NSThread mainThread] exit];
If you terminate it will look like your app has crashed!
Best to put up a message saying that there is no internet connection and give them an option to retry (in case they can get an internet connection), or choose to quite it themselves
You shouldn't do this. Take a look at "Stopping" in the Human Interface Guidelines as you could possibly fail for submitting an App that does this, or at the very least provide for a strange user experience.
The link also shows the correct way to handle this, as in the iTunes Music Store app.
Your App will be rejected if you terminate when you cannot reach the Internet.
Sorry.
-t

Resources