how to display alert when networking connection is disable? - iphone

i am new to iphone in my app i am connecting to database through web services it working correctly,but i want to displat alert when network connection is disable.please any help me how to alert when net connection is disable,
thanks in advance.

Have a look at this S.O. entry
In particular, the main point of interest for you is the first one, about using the Reachability framework to detect network connectivity.

Use UIAlertView to display alerts

When you get notified by your delegate/Observer for the network disable state, you could show message in UIAlertView.
//show an alert to let the user know that they can't connect...
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Network Status" message:#"Network is not available. Please try again later." delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alert show];
[alert release];
you could also use the Reachability API's to know the state of network connection.
iPhone SDK: Testing Network Reachability

Related

UIWebView Error Delegate Method vs Reachability

I'm in the process of submitting my app to the App Store, but I read that I must notify the user if the internet connection is down when my app needs it. The Apple page mentioned Reachability as well. Currently, though, I'm using the UIWebView delegate method didFailLoadWithError...
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:#"Error Loading" message:[error localizedDescription] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[errorAlert show];
}
...and it's working fine. My question is, will my app be rejected for not using Reachability to do this, or is it fine doing what I am currently doing?
Thanks in advance.
No, you're perfectly ok using didFailLoadWithError:.
Reachability class could be used to check if host is up (or internet connection at all) before even trying to load some page. But it is not neccessery, as long as you handle the possible errors - which obviously you do.
EDIT:
It is still a good practice to know wheather you will be able to reach a certain host or not. You could even modify GUI for each case (instead of just reporting an error). But this can always be done in update :)

My code to check if I have Internet connection doesn't work well

I have a code to detect if I have internet connection or not, and sometimes it doesn't work well. Here's the code:
-(void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
UIAlertView *alert= [[UIAlertView alloc] initWithTitle:#"Error" message:#"Couldn't connect to the Internet. Please check your connection." delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
The problem is that sometimes a web doesn't load for other things and not because it has a bad connection. I hope that there's a better way to check the WiFi. Any idea?
I suggest you follow the link and read the best marked answer given in there. It worked perfectly with me.
How to check for an active Internet connection on iOS or OSX?

Error Notification

i have a refresh button, when i press it it refreshes my xml file and puts it in the tableview, what i want is, when i dont have connection to my xml file, i want it to show me a pop up that tells me there is no conntion to the server.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"" message:#"No Connection" delegate:refreshButton cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
hope you can help
any idea how ?
Check the return code from your http request. If it is a code that signifies no network connection, you can show the alert view.
I suggest using ASIHTTPRequest if you aren't already. It lets you set failure blocks that will execute when the request doesn't complete successfully.

How to exit the app after an error in user friendly manner?

I need to protect my code against possible errors. If they arise then there's no point to run the app further, so I need to bring to the user some message and then exit the app. So, I'm checking the conditions and then bringing alert:
if (someError){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"No database file exist. App will close now." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
And in the delegate method I'm closing the app using NSAssert:
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
NSAssert(0, #"closing");
}
}
Also, I have included delegate protocol in header. However, the app just brings alert but after pressing OK it just freezes, and I'm getting some message "CoreAnimation: ignoring exception: closing". What am I missing or what other options exits?
You should not do that, it is against Apple HIG (Human Interface Guidelines):
iPhone applications should never quit programmatically because doing so looks like a crash to the user.
It is always better to provide some kind of feedback to the user about the error that occured and provide a way to restart the process without restarting the app. However, if you really, really want, you can use
exit(0);

iPhone app crashes while checking internet connectivity

In my iPhone app, I need to detect the internet Connection availability.
So I am referencing some files from "Reachability" project of Apple.
Link is given below:
http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html
I create a new project and implement the code below in viewWillAppear but the app crashes.
I included the Reachability.h, Reachability.m from Apple's demo project.
I also included SystemConfiguration Framework.
app works fine when Internet is Working. But app Crashes when Internet in not Working.
Even I checked the console but there is no notification or error shown in the console.
Reachability *r = [Reachability reachabilityWithHostName:#"www.google.com"];
NetworkStatus internetStatus = [r currentReachabilityStatus];
if ((internetStatus == ReachableViaWiFi) || (internetStatus == ReachableViaWWAN))
{
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:#"Internet Connection" message:#"Available" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[myAlert show];
[myAlert release];
}
else
{
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:#"No Internet Connection" message:#"This app require an internet connection via WiFi or cellular network to work." delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[myAlert show];
[myAlert release];
}
What could be the reason for crash?
What should I do?
Thanks!!
You're over-releasing the alerts. First, you do autorelease and then additionally release, which is too much. Just remove the two [myAlert release]; and it should work.
Here it may be the case that your code does not work on simulator because of time out. But try running it on device. Also try debugging the code as #greg rightly said. For that credit should go to #greg. Hope this helps. Let me know if it works.
Your problem is elsewhere in your code, as the code you provided along with copying Reachability.[mh] into a fresh Xcode project seems to work without crashing. When your app crashes, it must have some sort of information logged in the console. If there truly is nothing, set a breakpoint in your "startup" methods (viewWillLoad, viewDidLoad, AppDelegate stuff and step through line-by-line until you catch the problem.