30

私はiPhoneとiPad用のアプリを持っていますが、UIPickerViewControllerをiPad用のUIPopoverControllerにロードしようとすると、「ソースタイプ1が利用できません」という例外が発生します。 デバイスを使用していても問題が発生します。ソースタイプ1は利用できません

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

答えて

74

あなたはシミュレータ... にカメラを開いているので、コードが[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] のようなものであると明らかにシミュレータがcameraを持っていないので、これは は

、次のように警告を与えて進んで...です
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 
} 

何も心配する必要はありませんが、正しくデバイス上で動作します!

スウィフト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

これが正解です。 – VedTopkar

+0

私は生産中のユーザーからこのクラッシュを受け取りました。明らかにシミュレータにはありません。 –

+0

ドキュメンテーションから: 'カメラがすでに使用中の場合、このメソッドはNOを返します。 –

関連する問題