title: Why the autofocus didn't work?
body: <p>I am developing a camera related app. Now I succeeded to capture camera preview. However, when I was trying to set autofocus to my app, it didn't work. I tried both AVCaptureFocusModeContinuousAutoFocus and AVCaptureFocusModeAutoFocus, neither of them worked. 
  By the way, I tested on iPhone 6s.</p>
  
  <p>my ViewController.h file</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  #include &lt;AVFoundation/AVFoundation.h&gt;
  
  @interface ViewController : UIViewController
  {
      AVCaptureSession *cameraCaptureSession;
      AVCaptureVideoPreviewLayer *cameraPreviewLayer;
  }
  
  - (void) initializeCaptureSession;
  
  @end
  </code></pre>
  
  <p>my ViewController.m file</p>
  
  <pre><code>#import "ViewController.h"
  
  @interface ViewController ()
  
  @end
  
  @implementation ViewController
  
  - (void)viewDidLoad {
      [super viewDidLoad];
      // Do any additional setup after loading the view, typically from a    nib.
      [self initializeCaptureSession];
  
  }
  
  - (void) initializeCaptureSession
  {
      //Attempt to initialize AVCaptureDevice with back camera
      NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
      AVCaptureDevice *captureDevice = nil;
      for (AVCaptureDevice *device in videoDevices){
          if (device.position == AVCaptureDevicePositionBack)
          {
              captureDevice = device;
              break;
          }
      }
  
      //If camera is accessible by capture session
      if (captureDevice)
      {
          if ([captureDevice isFocusModeSupported:AVCaptureFocusModeAutoFocus]){
              NSError *error;
              if ([captureDevice lockForConfiguration:&amp;error]){
                  [captureDevice setFocusPointOfInterest:CGPointMake(0.5f, 0.5f)];
                  [captureDevice setFocusMode:AVCaptureFocusModeAutoFocus];
                  [captureDevice unlockForConfiguration];
              }else{
                  NSLog(@"Error: %@", error);
              }
          }
  
  
          //Allocate camera capture session
          cameraCaptureSession = [[AVCaptureSession alloc] init];
          cameraCaptureSession.sessionPreset = AVCaptureSessionPresetMedium;
  
          //Configure capture session input
          AVCaptureDeviceInput *videoIn = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:nil];
          [cameraCaptureSession addInput:videoIn];
  
          //Configure capture session output
          AVCaptureVideoDataOutput *videoOut = [[AVCaptureVideoDataOutput alloc] init];
          [videoOut setAlwaysDiscardsLateVideoFrames:YES];
          [cameraCaptureSession addOutput:videoOut];
  
  
          //Bind preview layer to capture session data
          cameraPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:cameraCaptureSession];
          CGRect layerRect = self.view.bounds;
          cameraPreviewLayer.bounds = self.view.bounds;
          cameraPreviewLayer.position = CGPointMake(CGRectGetMidX(layerRect), CGRectGetMidY(layerRect));
          cameraPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
  
          //Add preview layer to UIView layer
          [self.view.layer addSublayer:cameraPreviewLayer];
  
          //Begin camera capture
          [cameraCaptureSession startRunning];
  
      }
  
  }
  
  
  - (void)didReceiveMemoryWarning {
      [super didReceiveMemoryWarning];
      // Dispose of any resources that can be recreated.
      [cameraCaptureSession stopRunning];
  }
  
  
  @end
  </code></pre>