Source type 1 not available
Solution 1
This is because you are opening camera on simulator...
since the code is something like [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]
and obviously simulator don't have camera
...
Proceed giving an alert like this,
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Device has no camera."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[myAlertView show];
}
else{
//other action
}
Swift 3:
if !UIImagePickerController.isSourceTypeAvailable(.camera) {
let alertController = UIAlertController(title: nil, message: "Device has no camera.", preferredStyle: .alert)
let okAction = UIAlertAction(title: "Alright", style: .default, handler: { (alert: UIAlertAction!) in
})
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
} else {
// Other action
}
Nothing to worry, it will work on device correctly!
Solution 2
You can not use the camera with the simulator only with a real device. The simulator does not have a camera even if the Mac has one.
Use the photo library instead
imagePicker.sourceType = .photoLibrary
instead of
imagePicker.sourceType = .camera
Solution 3
The simulator won't be having camera even though you Mac has. So try using
picker.sourceType = .photoLibrary
instead of
picker.sourceType = .camera
which will show the picture collections. But not to worry, the code will run good on the real devices.
Solution 4
Are you trying to run the app in an iPhone emulator?
If that's the case, the emulator doesn't support camera functionality, and only supports getting photos from the photo library. Sounds like maybe I should build in an automatic fallback because lots of people will be trying to test their apps on the emulator.
Allen Walker
Updated on July 09, 2022Comments
-
Allen Walker almost 2 years
I have an app for iPhone and iPad, and when I try to load an
UIPickerViewController
in aUIPopoverController
for iPad I get the Exception "Source type 1 not available". getting the problem even though using the device.@try { if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; imagePicker.delegate = self; imagePicker.allowsEditing = NO; self.tempComp = component; if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { [self presentModalViewController:imagePicker animated:YES]; }else { // We are using an iPad popoverController=[[UIPopoverController alloc] initWithContentViewController:imagePicker]; popoverController.delegate = self; [popoverController presentPopoverFromRect:component.bounds inView:component permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; } }else{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Camera Non Disponibile" message:@"La camera non è disponibile" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil]; [alert show]; } } @catch (NSException *exception) { NSLog(@"Cattura eccezione %@", exception); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Eccezione" message:[NSString stringWithFormat:@"%@", exception] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil]; [alert show]; }
-
Iulian Onofrei almost 7 yearsI received this crash from a user in production. Clearly not in simulator.
-
Iulian Onofrei almost 7 yearsFrom the documentation:
if the camera is already in use, this method returns NO
-
Preetam Jadakar almost 7 years@IulianOnofrei: it should not, please confirm on device.
-
Iulian Onofrei almost 7 years@preetam, I think you didn't understand me, it's in the documentation.
-
Preetam Jadakar almost 7 years@IulianOnofrei: I got your point, I'm assuming camera is in use means, may be a video call or live streaming is going on(though not tested). anyways this case is valid where we should directly alert the user. Some app have opened a camera means doesn't mean camera is unavailable(this needs to confirm)
-
Iulian Onofrei almost 7 years@preetam, Indeed, I did not reproduce it, but since Apple says you should check it every time, and I did receive a crash in production, I'll add the check.
-
Chino Pan almost 5 yearscorrect answer, it was because you have to run this in a real device connected not on simulator.