Search code examples
iphoneobjective-cnsurlconnectionnsurl

How to avoid URL NOT SUPPORTED error on iphone


I am calling a URL through POST HTTP method. When I am not connected to 3G or WiFi, it will never fetch the URL correctly and will never get the response from the URL correctly. Connection is made through post:

  NSString *URL = [NSString stringWithFormat:@"http://www.abc.com/webservices/client_server.cfc];
[[NSURLCache sharedURLCache] removeAllCachedResponses];

NSString *time_stamp =[NSString stringWithFormat:@"time_stamp=%@", self.today];

NSData *postData = [time_stamp dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];

NSMutableURLRequest *URLRequest = [[[NSMutableURLRequest alloc] init] autorelease];
[URLRequest setURL:[NSURL URLWithString:URL]];
[exhURLRequest setHTTPMethod:@"POST"];
[URLRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[URLRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[URLRequest setHTTPBody:postData];


self.connection =[[[NSURLConnection alloc] initWithRequest:URLRequest delegate:self] autorelease];

NSAssert(self.connection != nil, @"Failure to create URL connection.");

I am checking if I get the response through :

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// check for HTTP status code for proxy authentication failures
// anything in the 200 to 299 range is considered successful

NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

if (([httpResponse statusCode]/100) == 2) {
    self.data = [NSMutableData data];

} else {
    NSDictionary *userInfo = [NSDictionary dictionaryWithObject:
                              NSLocalizedString(@"HTTP Error",
                                                @"Error message displayed when receving a connection error.")
                                                         forKey:NSLocalizedDescriptionKey];
    NSError *error = [NSError errorWithDomain:@"HTTP" code:[httpResponse statusCode] userInfo:userInfo];
    [self handleError:error];
}
}

But some how when the URL is not connected lets say when I am in the metro or something, I cannot connect to URL and it throws me "URL NOT SUPPORTED". How can I avoid such a scenario and allow the app to push forward without throwing such an error ? If anybody has used offline redirection to the app without keep on waiting the user on the front screen.


Solution

  • Download apple’s Reachability sample code. Add Reachability.h+.m to your project. Link SystemConfiguration framework. Then use their Reachability class like so.

    #import "Reachability.h"
    

    for WiFi:

    -(void)doStuff{
        Reachability *wifi = [Reachability reachabilityForLocalWiFi];
        if (wifi.currentReachabilityStatus == ReachableViaWiFi){
            // do connected stuff
        }
        else {
            // do offline stuff
        }
    }
    

    For Wifi or 3G:

    -(void)doStuff{
        NetworkStatus connectivity = [Reachability reachabilityForInternetConnection].currentReachabilityStatus;
        if (connectivity == ReachableViaWiFi || connectivity == ReachableViaWWAN){
            // do connected stuff
        }
        else {
            // do offline stuff
        }
    }
    

    If you will be continually doing network tasks add the reachability object to your class. That will improve performance and allow you to use it's notifier.