[swift] Swift로 디바이스의 연결된 외부 장치 정보 가져오기

Swift를 사용하여 iOS 디바이스에서 연결된 외부 장치 정보를 가져오는 방법을 알아보겠습니다.

1. 외부 장치 감지

외부 장치를 감지하기 위해 AVCaptureDeviceDiscoverySession 클래스를 사용합니다. 이 클래스는 주어진 미디어 타입과 포지션에 대해 연결된 장치를 검색하는 데 사용됩니다. 다음 코드는 모든 비디오 입력 장치를 검색하는 예제입니다.

import AVFoundation

func detectExternalDevices() {
    let discoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: .video, position: .unspecified)
    let devices = discoverySession.devices
    
    for device in devices {
        print("External device detected: \(device.localizedName)")
    }
}

2. 연결된 외부 장치 정보 가져오기

AVCaptureDevice 인스턴스를 사용하여 연결된 외부 장치에 대한 정보를 가져올 수 있습니다. 다음 코드는 첫 번째 비디오 입력 장치의 로컬 이름과 모델명을 가져오는 예제입니다.

import AVFoundation

func getExternalDeviceInfo() {
    guard let device = AVCaptureDevice.default(for: .video) else {
        print("No video device found")
        return
    }
    
    print("Local name: \(device.localizedName)")
    print("Model name: \(device.modelName)")
}

3. 참고 자료

위의 코드와 참고 자료를 활용하여 Swift에서 디바이스의 연결된 외부 장치 정보를 가져올 수 있습니다.