[SOLVED] AVCaptureDevice – iphone/ipad as video source

Issue

Can AVCaptureDevice be used (objective-c or Swift) to access an iOS device as a camera source, when it is connected via a lightning cable, very much like Quicktime does for OSX Yosemite?

Quicktime select camera source image

If not, is there any other way to capture it?

I’m using AVCaptureDevice.devices() (in swift) but it only lists the built-in Mac camera and mic.

Solution

Found the solution for this (thanks Chris Adamson), after looking at a presentation on WWDC where Apple announced this capability – fast forward to 4:09.

The following CMI property needs to be set before AVCaptureDevice can detect the iOS device as a camera/capture device (example in Swift):

var prop : CMIOObjectPropertyAddress = CMIOObjectPropertyAddress(
        mSelector: CMIOObjectPropertySelector(kCMIOHardwarePropertyAllowScreenCaptureDevices),
        mScope: CMIOObjectPropertyScope(kCMIOObjectPropertyScopeGlobal),
        mElement: CMIOObjectPropertyElement(kCMIOObjectPropertyElementMaster))
    
var allow:UInt32 = 1
    
CMIOObjectSetPropertyData( CMIOObjectID(kCMIOObjectSystemObject),
        &prop,
        0,
        nil,
        UInt32(sizeofValue(allow)),
        &allow)

After this is done (e.g. in your AppDelegate), the standard registration of observers can be done, and the iOS camera will show up in the list of available devices

// register for notifications when a new device is connected
notifications.registerObserver(AVCaptureSessionDidStartRunningNotification, forObject: session, block: {note in
        
    var devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeMuxed)
                +  AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo) as! [AVCaptureDevice]

    for device in self.devices {
        if device.modelID == "iOS Device" {
            // device is your AVCaptureDevice... use it as usual
        }
    }
})

Answered By – Gonçalo Borrêga

Answer Checked By – David Goodson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *