2013-03-11 57 views
2

當我嘗試將電子郵件發送到不存在的地址(例如,「hello」)時,我猜想MFMail Composer應該返回MFMailComposeResultFailed,但這樣做不會發生,並且該消息似乎被正確發送(顯然,它不會被髮送)。我嘗試了與「郵件」相同的方式,在這種情況下,「郵件」回答發送郵件時發生錯誤,但是,當我在我的應用中執行相同操作時,沒有錯誤。我怎樣才能解決這個問題?MFMailViewComposer在寫入錯誤的目標地址格式時不會返回錯誤

這是我使用的代碼,當我進入郵件屏幕:

if ([MFMailComposeViewController canSendMail]) { 
    MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc]init]; 
    mailer.mailComposeDelegate = self; 
    [mailer setSubject:@"Notification"]; 
    User *user = [user_array objectAtIndex:0]; 
     NSArray *toRecipients = [NSArray arrayWithObjects:user.eMail, nil]; 
     [mailer setToRecipients:toRecipients]; 

    [mailer setMessageBody:self.label1.text isHTML:NO]; 
    [self presentViewController:mailer animated:YES completion:nil]; 
    [mailer release]; 
} 
else 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure" 
                message:@"Your device doesn't support the composer sheet" 
                delegate:nil 
              cancelButtonTitle:@"OK" 
              otherButtonTitles: nil]; 
    [alert show]; 
    [alert release]; 
} 

}

,這是響應代碼:

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error { 
NSString *string = @""; 
switch (result) 
{ 
    case MFMailComposeResultCancelled: 
     string = @"cancelled"; 
     NSLog(@"Mail cancelled: you cancelled the operation and no email message was queued."); 
     break; 
    case MFMailComposeResultSaved: 
     NSLog(@"Mail saved: you saved the email message in the drafts folder."); 
     break; 
    case MFMailComposeResultSent: 
     string = @"sent"; 
     NSLog(@"Mail send: the email message is queued in the outbox. It is ready to send."); 
     break; 
    case MFMailComposeResultFailed: 
     NSLog(@"Mail failed: the email message was not saved or queued, possibly due to an error."); 
     break; 
    default: 
     NSLog(@"Mail not sent."); 
     break; 
} 
// Remove the mail view 
int abc = MFMailComposeErrorCodeSendFailed; 
NSLog(@"ERROR LOG %i", abc); 
[self dismissViewControllerAnimated:YES completion:nil]; 
if ([string isEqualToString:@"cancelled"]) { 
    ViewController *second = [self.storyboard instantiateViewControllerWithIdentifier:@"Notif_detail"]; 
    [second setString1:self.string1]; 
    [second setString2:self.string2]; 
    [second setString3:self.string3]; 
    [second setString4:self.string4]; 
    [self.navigationController pushViewController:second animated:YES]; 
} 
else if ([string isEqualToString:@"sent"]){ 

    ViewController *second = [self.storyboard instantiateViewControllerWithIdentifier:@"View"]; 
    [self.navigationController pushViewController:second animated:YES]; 
} 

}

提前致謝!

+0

請問您可以添加一些代碼嗎?僅僅描述問題是不可能的。 – Rob 2013-03-11 16:04:21

+0

感謝羅布,我編輯了我的問題 – user2144580 2013-03-11 16:18:15

回答

2

我想didFinishWithResult被觸發時實際上沒有錯誤,因爲消息已排隊並即將發送。由於未提供郵件而從郵件服務器(smtp代碼)收到響應時,此錯誤發生在稍後的時間點。

+0

是的iDroid,你是對的,這正是它發生的,但我怎麼能在我的應用程序中處理這個錯誤?謝謝 – user2144580 2013-03-11 16:17:46

+1

你不能因爲它不關你的事。如果你想要這種控制,你必須實現你自己的發送郵件的方式,或者至少在演示作曲者之前檢查電子郵件地址格式的有效性。 – 2013-03-11 16:26:48

+0

是翻轉,我想我會嘗試手動檢查輸入電子郵件地址的格式。謝謝你們! – user2144580 2013-03-12 07:16:39