The problems encountered in the recent project and the sharing of actual solutions, as well as the sharing of a small plug-in, hope to be helpful to authors who encounter similar problems.
Question one
When WebView
loading a web page, if the network request is slow and MBProgressHub
the animation is processed, the following situations will occur:
Case 1: Use[MBProgressHub showMessage:""]
- Using this method you will have this situation
In this case, the user cannot operate, and can only kill the program and restart the program, and the user experience is extremely poor.
Scenario 2: Use[MBProgressHub showMessage:@"" toView:self.view]
-
In this way, the experience is better than Case 1. The user can use the return key to return, but the experience is not very good.
In response to this problem, the first thing that comes to my mind is whether WebView
there is an attribute that can provide me to set a timeout request. I searched for it again and if not, then the WebView
load is one request
, then there must be an operation to set the request timeout, and I found the following method
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:_webViewUrl] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5.0];
in webView
the delegate method of
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
NSLog(@"%@",error.userInfo);
[MBProgressHUD hideHudWithMessageError:error.userInfo[@"NSLocalizedDescription"]];
[MBProgressHUD hideHUDForView:_webView];
}
webView
In this way , the timeout request is being processed .
Question two
Problems in the use of timers NSTimer
: A negative number appears in the countdown when the verification code is obtained. The reason is that the timer is not properly handled.
-(void)setUpTimer
{
self.timeLable.text = @"119s";
self.sendBtn.hidden = YES;
self.timeLable.hidden = NO;
self.timeIndex = 119;
//设置定时器
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(elapsedTimer) userInfo:nil repeats:YES];
//这句代码非常重要,否则会造成未知的错误
[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
}
//执行倒数器
-(void)elapsedTimer
{
self.timeIndex = self.timeIndex - 1;
if (self.timeIndex < 0) {
self.timeLable.hidden = YES;
self.sendBtn.hidden = NO;
[self timerEndRunning];
}else
{
self.timeLable.text =[NSString stringWithFormat:@"%ds",self.timeIndex];
}
}
// 结束倒数器
- (void)timerEndRunning
{
if (self.timer != nil) {
[self.timer invalidate];
self.timer = nil;
}
}
There are two ways to display the dark color after processing the send verification code button. The first is to set the button to be unclickable and then set the background color of the button to gray. The second is to set the picture UIControlStateDisabled
under the picture when the button is created. The above Both are possible.
Do you think the above timer is processed? When you destroy the interface, you will find that it is delloc
not called. In fact, when the controller pops out of the stack, it is not destroyed. What is the reason? The reason is that the timer during the countdown process of sending the verification code is not destroyed and stopped! ! !
So the correct logic should be:
- (void)viewDidDisappear
{
if(self.timer != nil)
[self.timer valide];
self.timer = nil;
}
remember to valide
firstnil。
Question three
I would like to share with you a plug-in that simulates low network speed. In fact, Apple has officially provided us with such a plug-in. We can download it from Apple's official website.
The specific address is shown below:
这个地址是需要账号登录的,没有的伙伴申请一个即可。
点开后
这样就可以在模拟器上模拟相应的网络状态了。再不用担心,做项目的时候遇到关于网络的测试了。