2011-02-13 34 views
29

Bir MFMailComposeViewController kurulum ettik ve iPhone'da gayet güzel çalışıyor, ancak iPad'de o söyleyerek çöküyor: MFMailComposeViewController - iPad

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target... 

Peki neden bu nil modal görünümü yaratacak?

MFMailComposeViewController *message = [[MFMailComposeViewController alloc] init]; 
    [message setMessageBody:@"My message here" isHTML:NO]; 
    [message setToRecipients:[NSArray arrayWithObject:@"[email protected]"]]; 
    [message setSubject:@"Request Info"]; 
    message.mailComposeDelegate = self; 
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
     message.modalPresentationStyle = UIModalPresentationFormSheet; 
    [self presentModalViewController:message animated:YES]; 
    [message release]; 

Herhangi bir fikir? MFMailComposeViewController gibi

cevap

73

Loos nedense oluşturulan ve dolayısıyla nil değeri vardır değildi. onu sunmadan önce nil olup olmadığını kontrol edin (bu geçici çözüm burada ... neyin yanlış gittiğini cevap vermez rağmen).

posta besteci oluşturmak ve +canSendMail yöntemi kullanarak sunmak denemeden önce posta gönderebilir durumunda da kontrolü yapılması gerekmektedir (hayır posta hesabı cihazda kurmak eğer örneğin HAYIR dönecektir):

if ([MFMailComposeViewController canSendMail]){ 
    // Create and show composer 
} 
else{ 
    // Show some error message here 
} 
+0

Ama neden yaratılmış olmaz? Tahsis/init çağırıyorum. – RyanJM

+19

Bu, örneğin – Vladimir

+1

cihazda bir posta hesabı kurulumuna izin vermediyse - Teşekkürler Vladimir - Yorumunuz – Brodie

11

cihazınızın nesne başlatıldı olmayacak

olduğunu

/*! 
@method  canSendMail 
@abstract Returns <tt>YES</tt> if the user has set up the device for sending email. 
@discussion The client may continue to set the recipients and content if the return value was <tt>YES</tt>. If <tt>NO</tt> 
      was the result, the client has a couple options. It may choose to simply notify the user of the inability to 
      send mail, or it may issue a "mailto" URL via <tt>-[UIApplication openURL:]</tt>. 
*/ 

+ (BOOL)canSendMail __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0); 
sonu: sen MFMailComposeViewController.h sınıftan aşağıdaki yorum, MFMailComposerViewController nesne oluşturmak görmeden, canSendMail kontrol koymak olmalıdır posta göndermek için kurulum. Bu iOS varsayılan posta uygulaması b'cause olacakları

+1

teşekkür ederiz. Bu yapardı. Bu cihaz çalışır durumda ve bu nedenle e-posta kurulumumda yok. Güzel yakalayış! – RyanJM

2

herhangi bir posta kimliği ile henüz yapılandırılmamış did't. Yani posta kimliğinizden herhangi birini yapılandırın ve deneyin. Bu

if ([MFMailComposeViewController canSendMail]){ 
    MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init]; 
    controller.mailComposeDelegate = self; 
    [controller setToRecipients:[NSArray arrayWithObject:eMail]]; 
    [self presentViewController:controller animated:YES completion:nil]; 
} 
else{ 
    UIAlertView *anAlert = [[UIAlertView alloc] initWithTitle:@"error" message:@"No mail account setup on device" delegate:self cancelButtonTitle:nil otherButtonTitles:nil]; 
    [anAlert addButtonWithTitle:@"Cancel"]; 
    [anAlert show]; 
} 

gibi

onun size yardım umuyoruz.

4
if ([MFMailComposeViewController canSendMail]){ 
    //execute your code 
else{ 
    UIAlertView *anAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"No mail account setup on device" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];  
    [anAlert addButtonWithTitle:@"OK"]; 
    [anAlert show]; 
} 

Uyarı alıyorsanız, posta hesabınızı telefonunuzda yapılandırın.

-3

Test cihazınızda posta hesabı oluşturulmadı.

if ([MFMailComposeViewController canSendMail]){ 

//execute your code else{ 
UIAlertView *anAlert = [[UIAlertView alloc] initWithTitle:@"error" message:@"No mail account setup on device" delegate:self cancelButtonTitle:nil otherButtonTitles:nil]; 
    [anAlert addButtonWithTitle:@"Cancel"]; 
    [anAlert show]; 
}