Source type 1 not available
Asked Answered
A

4

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];
}
Accretion answered 26/11, 2012 at 11:35 Comment(1)
If it happens on a device, maybe the source is available but the camera isn't? isCameraDeviceAvailable was relatively new then.Lattimer
V
123

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!

Varioloid answered 5/8, 2013 at 6:35 Comment(8)
I received this crash from a user in production. Clearly not in simulator.Oblong
From the documentation: if the camera is already in use, this method returns NOOblong
@IulianOnofrei: it should not, please confirm on device.Varioloid
@preetam, I think you didn't understand me, it's in the documentation.Oblong
@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)Varioloid
@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.Oblong
correct answer, it was because you have to run this in a real device connected not on simulator.Narcose
basically, any device having a camera.Varioloid
E
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
Endogenous answered 3/7, 2019 at 8:22 Comment(0)
Z
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.

Zilpah answered 16/12, 2020 at 6:51 Comment(0)
P
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.

Peacetime answered 2/2, 2019 at 6:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.