Which Camera API to use within iMessage Extension

I would like to present a live camera view within the MessagesViewController. There are quite a few different camera APIs to choose from, for live preview I picked the below code which works fine in a standard app. (Tested on iOS 10 as well.)


Which API should I be using to present a live camera preview?


I have added the plist item for camera permission message.


Thanks in advance!



-(void)addLiveCamera:(UIView*)camView {
    self.cameraCaptureSession = [AVCaptureSession new];
    NSArray *availableCameraDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    AVCaptureDevice *backCameraDevice=nil,*frontCameraDevice=nil;
    for( AVCaptureDevice *device in availableCameraDevices ) {
        if( device.position == AVCaptureDevicePositionBack ) {
            backCameraDevice = device;
        } else if ( device.position == AVCaptureDevicePositionFront ) {
            frontCameraDevice = device;
        }
    }
    NSError *error = nil;
    AVCaptureDeviceInput *cameraInput = [AVCaptureDeviceInput deviceInputWithDevice:frontCameraDevice error:&error];
    if( [self.cameraCaptureSession canAddInput:cameraInput] ) {
        [self.cameraCaptureSession addInput:cameraInput];
        AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.cameraCaptureSession];
        previewLayer.videoGravity = AVLayerVideoGravityResize;
        previewLayer.frame = camView.bounds;
        [camView.layer addSublayer:previewLayer];
        camView.backgroundColor = [UIColor clearColor];
        [self.cameraCaptureSession startRunning];
    }
}

Accepted Reply

It does appear that the camera capture isn't working correctly in Seed 1. This issue should be resolved in Seed 2.

Replies

Yes, iMessage extensions have an exception to use the camera. I'll file a bug about getting the documentation updated to clarify that in the overview.

Still not working for me, the app crashes with an exit code 1 when I call

AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo)

How did you do it?

This is not working, when requesting permission the app crashes with exit code 1 before prompting any message

Yes. Same thing happens to me. Topic here: https://forums.developer.apple.com/thread/53718

As of the GM and final release of IOS 10, I am not able to get a live preview to show in an iMessage app using the API at the start of this thread. Also there is not much info to be found elsewhere regarding this and the documentation referenced by pdm has still not been updated.


So I'm wondering if someone can confirm that live photo preview and capture is indeed allowed and working for iMessage apps in the final release? And if so, is there something special that needs to be done in the context of an iMessage app to get the live preview?


I personally haven't (yet) seen any other iMessage apps on the app store that do this, but notably Apple's own pinned camera app does.


Thanks for any help anyone can provide.

Update for anyone struggling with this: I was finally able to get a live preview using the method above but it is SLOOOOOOOOW to load it on my 6S Plus - so much so that it makes me think its not working a good amount of the time. Not sure why this is as the built-in Apple camera preview is lightning fast.


Also, not sure how I got this to finally appear, but I did add a frame sizing call to the previewLayer in the viewDidLayoutSubviews method so this might have helped.