30

Tôi có một ứng dụng cho iPhone và iPad và khi tôi cố gắng tải UIPickerViewController trong một UIPopoverController cho iPad, tôi nhận được Ngoại lệ "Loại nguồn 1 không khả dụng". gặp sự cố ngay cả khi sử dụng thiết bị.Loại nguồn 1 không khả dụng

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

Trả lời

74

Điều này là do bạn đang mở camera trên mô phỏng ... từ mã được cái gì đó như [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] và rõ ràng mô phỏng không có camera ... Tiến hành đưa ra một cảnh báo như thế này,

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 
} 

Không có gì phải lo lắng, nó sẽ hoạt động trên thiết bị chính xác!

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

Đây là câu trả lời đúng. – VedTopkar

+0

Tôi đã nhận được sự cố này từ người dùng trong quá trình sản xuất. Rõ ràng không phải trong giả lập. –

+0

Từ tài liệu: 'nếu máy ảnh đã được sử dụng, phương thức này trả về NO' –

Các vấn đề liên quan