Using Mock Camera in Xcode Simulator: A Step-by-Step Guide

Jerry PM
3 min readAug 26, 2024

--

Use MockImagePicker for testing purposes.

Testing camera functionality in an iOS app can be challenging, especially when running on the Xcode simulator, which doesn’t have access to a real camera. However, with the MockImagePicker, you can simulate camera interactions and streamline your testing process. In this article, we’ll walk through how to use MockImagePicker in your iOS project to make camera testing easier and more efficient.

Why Use MockImagePicker?

MockImagePicker is a tool that allows developers to simulate the behavior of UIImagePickerController, which is commonly used for selecting images or capturing photos and videos in iOS apps. By using MockImagePicker, you can test your app's camera-related features without needing physical devices or relying on manual image selection every time you run tests.

Setting Up MockImagePicker in Your Project

1. Install MockImagePicker:

First, you need to add MockImagePicker to your project. You can do this by adding it via CocoaPods, Swift Package Manager, or manually including it in your project.

pod 'MockImagePicker'

2. Configure MockImagePicker:

Once you’ve integrated MockImagePicker into your project, it’s time to set it up in your view controller where you handle image picking.

#if targetEnvironment(simulator)
import MockImagePicker
typealias UIImagePickerController = MockImagePicker
typealias UIImagePickerControllerDelegate = MockImagePickerDelegate
#endif

Use that for testing camera in your simulator, this “UIImagePickerController” will replace existing picker and all function

Here’s an example implementation

which uses UIImagePickerController to simulate camera functionality. We will then modify this implementation to use MockImagePicker instead, enabling more controlled and efficient testing

#if targetEnvironment(simulator)
import MockImagePicker
typealias UIImagePickerController = MockImagePicker
typealias UIImagePickerControllerDelegate = MockImagePickerDelegate
#endif

import UIKit

class AVCapturePhotoController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

override func viewDidLoad() {
super.viewDidLoad()

// Setup a tap gesture to trigger the camera
let tap = UITapGestureRecognizer(target: self, action: #selector(showMockCamera))
view.addGestureRecognizer(tap)
view.isUserInteractionEnabled = true
}

// Function to present the UIImagePickerController
@objc func showMockCamera() {
let picker = UIImagePickerController()
picker.sourceType = .camera
picker.delegate = self
present(picker, animated: true)
}

// Delegate method to handle the selected image
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
if let image = info[.originalImage] as? UIImage {
print("Got image 🖼 with PNG size = " + (image.pngData()?.count.description ?? "unknown"))
} else {
print("No image 😕")
}
dismiss(animated: true)
}

// Delegate method to handle cancellation of the picker
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true)
}

}

❌ Command the code of UIImagePickerController if code not availabe liket this example code.


// private func getImage(fromSourceType sourceType: UIImagePickerController.SourceType) {
// if UIImagePickerController.isSourceTypeAvailable(sourceType) {
// let imagePickerController = UIImagePickerController()
// imagePickerController.delegate = self
// imagePickerController.sourceType = sourceType
// if sourceType == .camera {
// imagePickerController.allowsEditing = false
// imagePickerController.showsCameraControls = true
// imagePickerController.cameraCaptureMode = .photo
// }
// self.present(imagePickerController, animated: true, completion: nil)
// }
// }

Or you can use like this

private func getImage() {
// if UIImagePickerController.isSourceTypeAvailable(sourceType) {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.sourceType = .camera
// if sourceType == .camera {
imagePickerController.allowsEditing = false
imagePickerController.showsCameraControls = true
// imagePickerController.cameraCaptureMode = .photo
// }
self.present(imagePickerController, animated: true, completion: nil)
// }
}

Conclusion

There are several functions of UIImagePickerController that may not work, so the solution is to comment out the non-functional code

--

--