Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

macOS App using iPhone camera

I am trying to build a simple swift (4) macOS app to use an iPhone camera connected to my Mac. I have started an blank macOS template app and have turned on sandbox to allow camera, mic and USB and added the following code to my ViewController.

import Cocoa
import AVFoundation

class ViewController: NSViewController {
@IBOutlet weak var camera: NSView!

override func viewDidLoad() {
    super.viewDidLoad()
    camera.layer = CALayer()
    let session:AVCaptureSession = AVCaptureSession()
    session.sessionPreset = AVCaptureSession.Preset.high
    let device:AVCaptureDevice = (AVCaptureDevice.default(for: AVMediaType.video))!
   // let listdevices = (AVCaptureDevice.devices())


do {
    try session.addInput(AVCaptureDeviceInput(device: device))

    //Preview
    let previewLayer:AVCaptureVideoPreviewLayer = AVCaptureVideoPreviewLayer(session: session)
    let myView:NSView = self.view
    previewLayer.frame = myView.bounds
    previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
    self.camera.layer?.addSublayer(previewLayer)
    session.startRunning()

           // print(listdevices)
            // print(device)
    } catch {
         print(device)
            }


}


override var representedObject: Any? {
    didSet {
    // Update the view, if already loaded.
    }
}


 }

In storyboard I have dropped in a Custom View.

App builds ok and uses facetime camera no problem, however with iPhone connected I dont see if as a device that AVFoundation can use. Not sure on next steps on how to get the previewLayer to select the USB camera to use aka iPhone.

p.s Needs to be landscape for all cameras orietation

like image 916
adamprocter Avatar asked Jan 30 '26 16:01

adamprocter


1 Answers

According to this Apple Developer Forum post, capturing the camera of a connected iOS device from a macOS app is not supported.

The closest you can do (as the post suggests), is to capture the screen of the iOS device while the camera (Camera.app) is running, effectively capturing the live camera preview (or you can roll your own companion camera app in iOS, if you want to remove the camera app’s UI from the captured screen).

like image 159
Nicolas Miari Avatar answered Feb 02 '26 12:02

Nicolas Miari



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!