50

I have an app for iPhone and iPad, and when I try to load an UIPickerViewController in a UIPopoverController 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];
}
tkanzakic
  • 5,499
  • 16
  • 34
  • 41
Allen Walker
  • 866
  • 1
  • 9
  • 17
  • If it happens on a device, maybe the source is available but the camera isn't? `isCameraDeviceAvailable` was relatively new then. – benc Jan 24 '19 at 03:32

4 Answers4

122

This is because you are opening a camera on the simulator(or on a device not having camera)... since the code is something like [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] and obviously, the simulator doesn't have a 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!

Preetam Jadakar
  • 4,479
  • 2
  • 28
  • 58
  • 2
    I received this crash from a user in production. Clearly not in simulator. – Iulian Onofrei Sep 22 '17 at 15:17
  • 1
    From the documentation: `if the camera is already in use, this method returns NO` – Iulian Onofrei Sep 22 '17 at 15:30
  • @IulianOnofrei: it should not, please confirm on device. – Preetam Jadakar Sep 25 '17 at 07:21
  • @preetam, I think you didn't understand me, it's [__in the documentation__](https://developer.apple.com/documentation/uikit/uiimagepickercontroller/1619144-issourcetypeavailable). – Iulian Onofrei Sep 25 '17 at 07:24
  • @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) – Preetam Jadakar Sep 25 '17 at 08:43
  • 1
    @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. – Iulian Onofrei Sep 25 '17 at 08:59
  • correct answer, it was because you have to run this in a real device connected not on simulator. – Chino Pan Aug 29 '19 at 01:34
  • basically, any device having a camera. – Preetam Jadakar Jan 03 '23 at 04:13
7

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
adiga
  • 34,372
  • 9
  • 61
  • 83
zaid afzal
  • 119
  • 1
  • 5
2

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.

SCouto
  • 7,808
  • 5
  • 32
  • 49
Raunak Priya
  • 111
  • 1
  • 3
1

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.