Reputation: 774
My app crashes on the simulator but works fine on the device, when I give the source type of imagePicker as Camera. I have already added the permission for camera and photoLibrary in 'plist'.
Crash log:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Source type 1 not available'
Code:
-(void)showImgPickerWithSourceType:(UIImagePickerControllerSourceType)sourceType
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = NO;
picker.sourceType = sourceType;
picker.allowsEditing = YES;
[(UINavigationController *)[[self.window.subviews objectAtIndex:0] nextResponder] presentViewController:picker animated:YES completion:nil];
}
Does the crash occur since I am trying to access the camera on a simulator? what will be the issue here. What I have missed?
Upvotes: 5
Views: 6428
Reputation: 5435
You can use isSourceTypeAvailable
method of UIImagePickerController
to check if given source type is available or not.
+ (BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType
Returns YES if the device supports the specified source type; NO if the specified source type is not available.
Because a media source may not be present or may be unavailable, devices may not always support all source types. For example, if you attempt to pick an image from the user’s library and the library is empty, this method returns NO. Similarly, if the camera is already in use, this method returns NO.
Before attempting to use an UIImagePickerController object to pick an image, you must call this method to ensure that the desired source type is available.
You can add check for source type available or not using below lines of code:
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = sourceType;
picker.allowsEditing = YES;
[(UINavigationController *)[[self.window.subviews objectAtIndex:0] nextResponder] presentViewController:picker animated:YES completion:nil];
}
else{
//it is simulator or device with no camera source
// handle accordingly
}
Upvotes: 2
Reputation: 780
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Source type 1 not available'- This is the reason for app crash means in a simulator we cannot access a camera.Only access the camera in a device so use the following code for avoiding a crash.
-(void)showImgPickerWithSourceType: (UIImagePickerControllerSourceType)sourceType
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
{
picker.sourceType = sourceType;
}
picker.allowsEditing = YES;
[(UINavigationController *)[[self.window.subviews objectAtIndex:0] nextResponder] presentViewController:picker animated:YES completion:nil];
Upvotes: 1
Reputation: 762
You are opening camera on simulator 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{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = NO;
picker.sourceType = sourceType;
picker.allowsEditing = YES;
[(UINavigationController *)[[self.window.subviews objectAtIndex:0] nextResponder] presentViewController:picker animated:YES completion:nil];
}
Hope this may help and have a practice of adding this code always to avoid crashes in Camera faulty devices...
Upvotes: 1
Reputation: 4815
If you are working in swift then use this code.
if(UIImagePickerController.isSourceTypeAvailable(.camera))
{
let myPickerController = UIImagePickerController()
myPickerController.delegate = self
myPickerController.allowsEditing = true
myPickerController.sourceType = .camera
self.present(myPickerController, animated: true, completion: nil)
}
else
{
let actionController: UIAlertController = UIAlertController(title: "Camera is not available",message: "", preferredStyle: .alert)
let cancelAction: UIAlertAction = UIAlertAction(title: "OK", style: .cancel) { action -> Void in
//Just dismiss the action sheet
}
actionController.addAction(cancelAction)
self.present(actionController, animated: true, completion: nil)
}
Upvotes: 4
Reputation: 1292
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePickerController=[[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.allowsEditing = YES;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:^{}];
}
else
{
//Camera not available
}
Upvotes: 1
Reputation: 1102
on the simulator, you can not access the camera so you need to check if the camera is available or not
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
// do your stuff
} else {
//camera not available
}
Upvotes: 3