views:

620

answers:

2

I have a long - (void)viewDidLoad { } function that uses the internet to load a page, and whenever the application loads the URL, the screen is black. I have a UIActivityIndicator setup to show that the address is loading, but I don't know how to prevent it from seeming as if the application is not responding, and I don't want to show the network activity indicator [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; because the screen will still be black, just with a loader in the top corner. My code is below:

- (void)viewDidLoad {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    email.text = [defaults objectForKey:@"mom_email"];
    pass.text = [defaults objectForKey:@"mom_pass"];
    if (email.text != nil && pass.text != nil) {
        [self login:loginBtn];
    }
}
- (IBAction)login:(id)sender {
    [email resignFirstResponder];
    [pass resignFirstResponder];
    [loader startAnimating]; // Does nothing though
    NSString *em = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef)email.text,NULL,(CFStringRef)@"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8);
    NSString *ps = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef)pass.text,NULL,(CFStringRef)@"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8);
    NSURL *url = [[NSURL alloc] initWithScheme:@"http" host:@"localhost" path:[[NSString alloc] initWithFormat:@"/app/login.php?email=%@&pass=%@", em, ps]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *loginResult = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    [loader stopAnimating];
    NSRange range = [loginResult rangeOfString:@"1"];
    if (range.location != NSNotFound) {
        [self dismissModalViewControllerAnimated:YES]; // Doesn't do anything if function called from viewDidLoad
    }
}
A: 

You can maybe add a text saying something is loading. Or you can also play with the NSURLConnection delegate methods to provide a progress bar instead of just a progress indicator.

Macmade
+1  A: 

Instead of blocking in the viewDidLoad, try starting up a new thread which does the loading. Then you just init the view with text that says things are loading and update whatever content is necessary once it is available.

Nathan S.