我有一个- (void)viewDidLoad { }
使用互联网加载页面的长函数,每当应用程序加载 URL 时,屏幕都是黑色的。我有一个UIActivityIndicator
设置来显示地址正在加载,但我不知道如何防止它看起来好像应用程序没有响应,我不想显示网络活动指示器[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
,因为屏幕仍然会黑色,只是在顶角有一个装载机。我的代码如下:
- (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
}
}