iOS development - pits encountered in the project

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 WebViewloading a web page, if the network request is slow and MBProgressHubthe animation is processed, the following situations will occur:

Case 1: Use[MBProgressHub showMessage:""]

  • Using this method you will have this situation

    the entire screen is covered

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]


  • Only cover the screen below the navigation bar

    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 WebViewthere is an attribute that can provide me to set a timeout request. I searched for it again and if not, then the WebViewload 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 webViewthe delegate method of

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    NSLog(@"%@",error.userInfo);
    [MBProgressHUD hideHudWithMessageError:error.userInfo[@"NSLocalizedDescription"]];
    [MBProgressHUD hideHUDForView:_webView];

}

webViewIn this way , the timeout request is being processed .


Prompt request timed out

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 UIControlStateDisabledunder 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 dellocnot 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 validefirstnil。

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:


Please click More Developer Tools...


这个地址是需要账号登录的,没有的伙伴申请一个即可。


红线部分即需要下载工具

红色下划线的插件


点开后


点击Mangerprofiles选择你需要的场景

这样就可以在模拟器上模拟相应的网络状态了。再不用担心,做项目的时候遇到关于网络的测试了。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325485616&siteId=291194637