sendAsynchronousRequest got Timeout Error on iPhone - iphone

My App allows user to take photo and upload it to a PHP server. I use sendAsynchronousRequest calls to send data (form with photo files) to the server. All those calls are working just fine on my iPad 2 when debugging. But when I debugged it on iPhone 4S, some of them get errors with error code, -1001, which represents Timeout error. I tried for thousand times in the past two days to find its root cause, but I still can't find one.
Has anybody encounters similar situation? or knows why?
The only clue I got is that if there are more than one file in the request data (original photo file and resized thumb file), it will receive time out error. Is it about request (photo file) size? or file number?

I handled the ASIHTTP request failed using this simple method.
- (void)requestFailed:(ASIHTTPRequest *)request {
NSError *error = [request error];
if([error code] == ASIRequestTimedOutErrorType ) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Error"
message:#"Connection Timed out"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
return;
} else if ( [error code] == ASIConnectionFailureErrorType) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Error"
message:#"Connection Failiure"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
return;
}
}
You can also set your connection time out in your request
like
[request_ setTimeOutSeconds:50.0];
This will wait for 50 seconds and show connection timed out error.

I solved the problem. It caused by upload big size file. The original photo token by iPhone 4s is about 5M big. If the network is not fast enough, the connection will be time out.

Related

UIAlertView after completion of NSLog

I am new to iphone..
Actually i wanted to know is it possible to put an UIAlertView after the completion of NSLog as i wanted to check whether the statement really works or not as i cant check NSLog in the iphone.
Please suggest me whether it is possible and how?
NSLog will still work on iPhone..
You can only see it in the console if you have attached the device to your mac..and then use the app.
Anyways.
You can show your AlertView after log like this.( This Nslog is an example..replace with what is your actual log)
NSLog(#" Image Loaded");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Log" message:#"Image Loaded" delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
[alert show];
[alert release];
If the NSLog statement works, it'll show up in the XCode console. So if you debug the app on your phone via XCode, you should see the NSLog. Or did you have something else on your mind ?

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?

simple alert view for connection check in a web view

dear Stackover community,
Nearly a week I'm struggling around with the fact that I must have a connection check
for my web view so I'm surviving the App review process by Apple :-)
I know that I can use the Reachablity sample but for me as a beginner I would say its not smart to deal with such a code.
I found out that it gets a bit simpler with a simple alert and code like this:
-(void)webview:(UIWebView *)webview didFailLoadWithError:(NSError *)error {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Can't connect. Please check your internet Connection"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show]; }
My problem is that I implemented this code into the .m of my Webviewcontroller and connected the delegate to the Filesowner but the App won't be that nice to me to show me the error message :-)
At the moment I'm using Xcode 4.2.1 with ARC enabled and storyboards.
Would anybody please give me a step by step guide how to get this work?
Its probably the last code I need for my App.
Thanks in advance
The delegate method is:
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;
Note it's "-(void)webView" not "-(void)webview". Just capitalize the "v" in "view" and it will work.
To answer your subsequent question:"now when i stop the loading of the page the error message comes too. Is there another tip ?"
As you can see an NSError is passed to your function. That error contains information about the failure. You can see that information by adding: NSLog(#"Epic fail with error:%#",error); ('Epic' added for dramatic effect). When you log that error you will see a code 'property' listed. Through some experimentation you will see that this code property changes based on the type of error. It seems that this code will equal -1009 for a connection error. All of the possible errors are enumerated here under "URL Loading System Error Codes". Best of all since they are defined in an enumeration you don't have to remember much of that. You can just use:
-(void)webView:(UIWebView *)webview didFailLoadWithError:(NSError *)error {
if (error.code == NSURLErrorNotConnectedToInternet){
NSLog(#"You are not connected");
}
}
TRY ...
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:[error localizedString
]
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];

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.