0

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.

lifemoveson
  • 1,621
  • 7
  • 25
  • 54
  • I am not sure I am following. You say "when I am not connected to 3G or Wifi, it will never fetch the URL correctly". You mean indeed when there is no network connection? Also, you may want to post the code where you create the request. – onnoweb Oct 18 '11 at 17:10
  • @onnoweb I have edited my question. – lifemoveson Oct 18 '11 at 17:14

3 Answers3

3

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.

NJones
  • 26,931
  • 7
  • 68
  • 87
1

some time this "URL NOT SUPPORTED error" is due to the url not encoding.

for this encode url by using this.or there are lots of ways on internet. dont encode whole url or base url (as it will encode ":" and '/' too and it may cause errors) only special charecter needs to be encode like &,@ etc.

NSString *encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
                                                                                                    NULL,
                                                                                                    (CFStringRef)pathRequest,
                                                                                                    NULL,
                                                                                                    (CFStringRef)@"!*'();:@&=+$,%#[]",
                                                                                                    kCFStringEncodingUTF8 ));
Moaz Saeed
  • 946
  • 2
  • 9
  • 23
0

How to check for an active Internet connection on iOS or OSX?

Use the solution propsed here to avoid sending a request when you don't have an active internet connection. Simple.

Community
  • 1
  • 1
Wolfert
  • 974
  • 6
  • 12