在调用相机后idleTimerDisabled失效的问题

在调用相机后idleTimerDisabled失效的问题

相关资料:

问题

  • 前几天有人在群里边说,设置idleTimerDisabled=YES后,在使用相机后不会阻止锁屏(很好奇怎么回事)。

解决

  • 1、完全不了解情况,先试了一下(代码来源):

      - (void)viewDidLoad
      {
      	[super viewDidLoad];
    
      	self.button = [UIButton buttonWithType:UIButtonTypeSystem];
      	[self.button setTranslatesAutoresizingMaskIntoConstraints:NO];
      	[self.button setTitle:@"Show ImagePicker" forState:UIControlStateNormal];
      	[self.button addTarget:self action:@selector(buttonTouchHandler:) forControlEvents:UIControlEventTouchUpInside];
      	[self.view addSubview:self.button];
      	[self.view setNeedsUpdateConstraints];
      }
    
      - (void)buttonTouchHandler:(UIButton *)button
      {
      	[self logIdleTimerDisabled];
      	UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
      	imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
      	imagePickerController.delegate = self;
      	[self presentViewController:imagePickerController animated:YES completion:nil];
      }
    
      - (void)updateViewConstraints
      {
      	[super updateViewConstraints];
    
      	if (self.viewConstraints)
      	{
      		[self.view removeConstraints:self.viewConstraints];
      	}
    
      	NSMutableDictionary *views = [@{ @"button": self.button} mutableCopy];
      	NSMutableArray *constraints = [NSMutableArray array];
      	[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[button]-20-|" options:0 metrics:nil views:views]];
      	[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[button]-|" options:0 metrics:nil views:views]];
    
      	self.viewConstraints = [constraints copy];
      	[self.view addConstraints:self.viewConstraints];
      }
    
      - (void)logIdleTimerDisabled
      {
      	BOOL idleTimerDisabled = [UIApplication sharedApplication].idleTimerDisabled;
      	NSLog(@"idleTimerDisabled = %d", idleTimerDisabled);
      }
    
      #pragma mark - UIImagePickerControllerDelegate
      - (void)imagePickerController:(UIImagePickerController *)picker  didFinishPickingMediaWithInfo:(NSDictionary *)info
      {
      	[self logIdleTimerDisabled];
      	[self dismissViewControllerAnimated:YES completion:^{
      		[self logIdleTimerDisabled];
      	}];
      }
    
      - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
      {
      	[self logIdleTimerDisabled];
      	[self dismissViewControllerAnimated:YES completion:^{
      		[self logIdleTimerDisabled];
      	}];
      }   
    

    注:Appdelegate中已近设置idleTimerDisabled为YES,Xcode会阻止设备休眠。
    Log:

      2017-03-23 13:22:25.594 UIImagePickerControllerBug[30239:3267578] idleTimerDisabled = 1
      2017-03-23 13:22:28.476 UIImagePickerControllerBug[30239:3267578] Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.
      2017-03-23 13:22:32.312 UIImagePickerControllerBug[30239:3267578] idleTimerDisabled = 1
      2017-03-23 13:22:32.846 UIImagePickerControllerBug[30239:3267578] idleTimerDisabled = 0   
    
  • 2、上边的『代码来源』原文中有作者的一些猜测:

      I have attached a simplified Xcode project to demonstrate the bug.
    
      	1. Run UIImagePickerControllerBug on a device not connected to Xcode (because Xcode always keeps the device from sleeping).  
      	2. Observe that the app does not fall asleep.
      	3. Press the Show ImagePicker Button.
      	4. Take a photo or just press cancel
      	5. Observe that the app will now fall asleep.   
    
  • 3、在『stackoverflow.com』查找的时候,发现其中有人在很早以前就提出了这个问题(由于是前些天的事情有些链接找不到了),不知道是出于什么原因官方一直没有解决。

解决

  • 解决办法就是相机的完成或者取消代理中延时设置,详见:

      - (void)resetIdleTimerDisabled
      {
      	[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
      }
    
      #pragma mark - UIImagePickerControllerDelegate
    
      - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
      {
      	[self dismissViewControllerAnimated:YES completion:^{
      		[self performSelector:@selector(resetIdleTimerDisabled) withObject:nil afterDelay:1.0];
      	}];
      }
    
      - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
      {
      	[self dismissViewControllerAnimated:YES completion:^{
      		[self performSelector:@selector(resetIdleTimerDisabled) withObject:nil afterDelay:1.0];
      	}];
      }  
    

备注

  • Xcode会阻止设备休眠。
posted @ 2017-03-23 13:41  苦海中的渡轮  阅读(996)  评论(0编辑  收藏  举报