30

Ich habe eine App für iPhone und iPad, und wenn ich versuche, eine UIPickerViewController in einer UIPopoverController für iPad laden, bekomme ich die Ausnahme "Quellentyp 1 nicht verfügbar". bekommen das Problem, obwohl das Gerät verwendet.Quellentyp 1 nicht verfügbar

@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]; 
} 

Antwort

74

Dies ist, weil Sie die Kamera auf Simulator Öffnungs ... da der Code ist so etwas wie [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] und offensichtlich haben Simulator nicht camera ... Gehen Sie eine Warnung wie diese geben,

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 
} 

Nichts zu sorgen, es wird am Gerät richtig funktionieren!

Swift 3:

if !UIImagePickerController.isSourceTypeAvailable(.camera){ 

    let alertController = UIAlertController.init(title: nil, message: "Device has no camera.", preferredStyle: .alert) 

    let okAction = UIAlertAction.init(title: "Alright", style: .default, handler: {(alert: UIAlertAction!) in 
    }) 

    alertController.addAction(okAction) 
    self.present(alertController, animated: true, completion: nil) 

} 
else{ 
    //other action 
} 
+4

Dies ist die richtige Antwort. – VedTopkar

+0

Ich habe diesen Absturz von einem Benutzer in der Produktion erhalten. Klar nicht im Simulator. –

+0

Aus der Dokumentation: 'Wenn die Kamera bereits verwendet wird, gibt diese Methode NO zurück –