iOS check server connection and successful download - iphone

I'm making an app that downloads data from a plist and uses that data in a tableview. Is there a way to check whether a connection to a url is successful and then whether the download was successful? Or maybe a tutorial that implements something like this?
Thanks

you must implement this delegate of NSURLConnection
//send the request
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
in the case of connection failure,this method will be executed
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSlog(#"no connection");
}

Related

webView and 'timeout'

I have question, in my application I need to have a connection and a webview to display the web content.
The problem is that I have a username and a password which need to be right in order to get a connection. Now I want to have an alert that comes up if the username is wrong. The
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
does not work for this, because if the username or the password is wrong it will continue loading (even though it won't display anything).
Is there a possibilty to 'tell' webview to set a time interval and then display an alert?
THANK YOU SO MUCH!
If you're authenticating with your server via HTTP authentication (per your comment above) then I would do this differently. When the user enters their username/pwd, make a request to any page (something lightweight) using NSURLConnection. If the username/password are incorrect you can handle the failures in the NSURLConnectionDelegate methods and prompt your user for a new username/password.
You can also do this with the iOS 5 version of NSURLConnection instead of the delegate methods but the docs aren't very good.
When this first request succeeds then load the web view.
EDIT: added example of using NSURLConnection to check if HTTP auth succeeds
Ok, here's a simple example. You can make this better by allowing NSURLConnection to load your intended request and if it succeeds load the response data into the webView vs initiating a new request. Read the URL Loading System docs to learn more about how to use NSURLConnection.
In your webView delegate, initiate an NSURLConnection with a request to the same domain as the webView request with the user credentials as follows:
- (void)performHTTPAuthRequest
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://%#:%##test.starsightings.com", username, password]]];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
}
// if the request succeeds, credentials must be good so load the webView
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// request completed successfully so username/password must be good
[self.webView loadRequest:theRequest];
}
// ignore the failure
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
}
// if we get an auth challenge, the credentials are bad so cancel the request and alert the user
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
[[challenge sender] cancelAuthenticationChallenge:challenge];
// alert user that username/pwd is invalid
}

Check web server is up or down in iphone

I need a little help here!!!
If string 'url' contains the address to web server, how do i check whether my web server is up or down?
I got some codes to check Internet connectivity. But even if we have internet connectivity how do we know server is up or not? Is there any better way to do that?
Thank You
You could do a request,
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:20.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest
delegate:self];
Now if you implement (along with all other delegate methods)
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
you will get either succes or error.
Post a request, get the response. If you get a response, the server is up, if not its down..

NSURLConnection basic authentication issues with didReceiveAuthenticationChallenge

Running into some weird issues with didReceiveAuthenticationChallenge. If I do something like:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:WAF_URL]
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:60];
[request setHTTPShouldHandleCookies:NO];
self.conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
I receive - (void)connection:(NSURLConnection *)connection
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge challenge which if successful, triggers connectionDidFinishLoading.
But, if I try to do a post with the request, I seem to be running into issue where my connectionDidFinishLoading is never triggered and my request times out.
This seems to be an issue only on iPhone and not on the simulator??!
My server was using NTLM challenge, which when used with Post with NSURLConnection screwed up. Removed that and it's fixed.

iPhone: How to download a raw .config file via program?

I am trying to download a xyz.config file which is created using iPhone Configuration utlity, via code.
NSString *urlString = #"http://xxxxx:xxx/ms/servlet/ConfigServer?userid=xxx&pwd=xxx&emailid=xxxxx";
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
receivedData = [[NSMutableData data] retain];
} else {
// Inform the user that the connection failed.
}
Then, didReceiveData, didReceiveResponse are written. Finally,
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(#"Succeeded! Received %d bytes of data",[receivedData length]);
NSString *responseString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
NSLog(#"Response : %#", responseString );
// release the connection, and the data object
[receivedData release];
[connection release];
}
With the above code, i'm getting response as string of the complete configuration file. But my Aim to download this file as raw data, which will automatically launch device profile to ask for installing this profile.
NOTE: I am able to provide the same URL in Safari browser on the device, and download the
raw file directly to install it on the device. I don't want to use Safari browser to download it, it should be done via my communication code.
I also tried with ASIHTTPRequest like below, but unable to download that file directly from the URL via this code as well.
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setRequestMethod:#"GET"];
[request startSynchronous];
Please help!
Thank you.
The Mobile Safari browser app on the iOS has a lot more freedom than your app. Case in point, on the latest iOS 4.3, Mobile Safari has the Nitro JavaScript engine which boost 2x performance increase. But the UIWebView which is available to your app doesn't have that engine. Your app is in a sandbox, therefore, you cannot write outside of that box.

send xml data to server and get response

i am making an app in which user's data is send to a server...the data should be in xml format.
presently i have made a string and put all into it...like following format
NSString *s=[[NSString alloc]initWithFormat:#"%#%#",name.text,address.text];
(this is just an example i have made a string with full xml tags including xml version tag)
and then send this through http post method....
i did it but dont know how to get response of server...please help ...any code will be helpful.....
waiting for answer
Take a look at NSURLConnection. You essentially create a connection, register a delegate, kick off the request and build the response as the data is passed to your delegate.
I'm working from memory here, but essentially:
Create an NSURLRequest for your request to your server
Create an NSURLConnection using the initWithRequest:(NSURLRequest *)request delegate:(id)delegate init method, passing a suitable delegate.
The request will be made, and the response will be passed back to your delegate in:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
I've not tested this, but something like:
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"SERVER"]
[request setHTTPMethod:#"POST"];
[request setHTTPBody:#"Your XML"];
NSURLConnection * conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
Then in your delegate you can build the response using the data provided in the didReceiveData:(NSData *)data and didReceiveResponse:(NSURLResponse *)response methods.