0

Here is my code:

//login method
- (int) authenticateClient {
NSString *loginWeb = [NSString stringWithFormat:(@"http://192.168.118.1/login.php?uname=%@&pass=%@&submit=Log%%20In"), user, pass];
NSURL *login = [NSURL URLWithString:loginWeb];
NSData *loginData = [NSData dataWithContentsOfURL: login];
NSString *result = [[NSString alloc] initWithData:loginData encoding:NSUTF8StringEncoding];
NSMutableURLRequest *loginRequest = [[NSMutableURLRequest alloc] init];
[loginRequest setURL:login];
[loginRequest setTimeoutInterval:1];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:loginRequest delegate:self startImmediately:YES];
[connection start];
   return [result integerValue];
}

My question is, when simulating having no network connection, my app freezes up and doesn't load or time out in the specified interval (1 second just to see if it works).

I read something about start immediately forces in to run on the current threat causing everything else to "pause" until the action is complete.

I have two questions:

1.) What is a better method to have the URL basically run in the background instead of pausing everything?

2.) How can I get a timeout method that actually works?

Much appreciation! Thanks!

Mike Abdullah
  • 14,933
  • 2
  • 50
  • 75
  • Your question doesn't make sense. You have two lumps of code that both try to achieve the same thing. And there is no need to `-start` an `NSURLConnection` when it's already started – Mike Abdullah Apr 09 '13 at 15:49

1 Answers1

0

Well, one second as timeout is really low. If you want to simulate different network conditions, you can use something like this. You don't need to this dance:

NSData *loginData = [NSData dataWithContentsOfURL: login];
NSString *result = [[NSString alloc] initWithData:loginData encoding:NSUTF8StringEncoding];

You don't need this as well, since the connection has started already:

[connection start];

Also from the documentation:

NSURLConnection’s delegate methods—defined by the NSURLConnectionDelegate Protocol protocol—allow an object to receive informational callbacks about the asynchronous load of a URL request. Other delegate methods provide facilities that allow the delegate to customize the process of performing an asynchronous URL load. These delegate methods are called on the thread that started the asynchronous load operation for the associated NSURLConnection object.

So for your questions:

  • Implement the NSURLConnection’s delegate
  • Check this & this.
Community
  • 1
  • 1
Rui Peres
  • 25,741
  • 9
  • 87
  • 137
  • I hate to say it, but I don't fully understand this answer... Is there a way you can show me an example in code? My app still freezes if a connection cannot be made, and the default/minimum timeout method is 240 seconds (so 1 second is irrelevant unfortunately). I don't want the user to have to wait 4 minutes for the method to timeout, and I can't use a countdown timer if the thread freezes until the connection method returns either successful or not. :( – Jordan Harris Nov 13 '12 at 04:18