1

I'm not familiar with iOS but I'm trying to find when the default, built-in camera application is focusing. To do this I create my own separate Objective-C application and following this answer here [iPhone : camera autofocus observer? but I'm not getting anything from observeValueForKeyPath in the NSLog.

#import "ViewController.h"
#import "AVFoundation/AVCaptureDevice.h"
#import "AVFoundation/AVMediaFormat.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"viewDidLoad");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// callback
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    NSLog(@"observeValueForKeyPath");
    if( [keyPath isEqualToString:@"adjustingFocus"] ){
        BOOL adjustingFocus = [ [change objectForKey:NSKeyValueChangeNewKey] isEqualToNumber:[NSNumber numberWithInt:1] ];
        NSLog(@"Is adjusting focus? %@", adjustingFocus ? @"YES" : @"NO" );
        NSLog(@"Change dictionary: %@", change);
    }
    if( [keyPath isEqualToString:@"focusMode"] ){
        AVCaptureFocusMode focusMode = [ [change objectForKey:NSKeyValueChangeNewKey] isEqualToNumber:[NSNumber numberWithInt:1] ];
        NSLog(@"focusMode? %ld", focusMode);
    }
}

// register observer
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear: animated];
    NSLog(@"viewWillAppear");
    AVCaptureDevice *camDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    int flags = NSKeyValueObservingOptionNew;
    [camDevice addObserver:self forKeyPath:@"adjustingFocus" options:flags context:nil];
    [camDevice addObserver:self forKeyPath:@"focusMode" options:flags context:nil];
}

@end

Any help much appreciated.

jimbo
  • 416
  • 1
  • 4
  • 14
  • Can you able to the live camera preview on your view controller? As per the code you provided, it looks like you just creating `AVCaptureDevice` instance and doing nothing with that. – Bluewings Jul 10 '17 at 08:57
  • @Bluewings I'd like to observe the adjustingFocus key/value when the camera is being used by the native iPhone camera application, so I am not creating a preview in my own code. Can we use KVO to observe keys from other applications? – jimbo Jul 12 '17 at 07:11
  • 1
    Nope you can not observe from other applications. As per the documentation you have to get a lock for using any AVCaptureDevice to use or manipulate it. So when other application is using any capture device you can not actually get to know about it. – Bluewings Jul 12 '17 at 07:14

1 Answers1

0

For anyone who visits this question, the answer is what Bluewings wrote as a comment. I was trying to use KVO to observe one application from another which is not possible since only one lock on a capture device is possible at one time.

jimbo
  • 416
  • 1
  • 4
  • 14