2013-01-10 1 views
2

사용자가 iPhone에서 테스트 이메일을 보낼 수있는 앱이 있습니다. 내 애플과 같이 메일 작성 기능을 활성화하는 방법을 호출iOS에서 이메일 작성기 화면 해지에 도움이 필요합니다.

-(void)displayComposerSheet 
{ 

    //set up a way to cancel the email here 
    //picker is an instance of MSMailComposeViewController already declared in the .h file 

    [picker setSubject:@"Test Mail"]; 

    // Set up recipients 

    // Attach an image to the email 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Icon" ofType:@"png"]; 
    NSData *myData = [NSData dataWithContentsOfFile:path]; 
    [picker addAttachmentData:myData mimeType:@"image/png" fileName:@"Icon"]; 

    // Fill out the email body text 
    NSString *emailBody = @"This is a test mail."; 
    [picker setMessageBody:emailBody isHTML:NO]; 

    [self presentModalViewController:picker animated:YES]; 
    NSLog(@"mail is working"); 

} 


- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{ 
    emailLabel.hidden = NO; 
    // Notifies users about errors associated with the interface 
    switch (result) 
    { 
     case MFMailComposeResultCancelled: 
      emailLabel.text = @"Mail sending canceled."; 
      break; 
     case MFMailComposeResultSaved: 
      emailLabel.text = @"Mail saved."; 
      break; 
     case MFMailComposeResultSent: 
     { 
      emailLabel.text = @"Mail sent."; 
      NSLog(@"It's away!"); 


      UIAlertView *emailAlertView = [[UIAlertView alloc] initWithTitle:@"Sent!" message:@"Mail sent successfully." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
      [emailAlertView show]; 
      [self dismissModalViewControllerAnimated:YES]; 
      [self.navigationController popViewControllerAnimated:YES]; 


     } 
      break; 
     case MFMailComposeResultFailed: 
     { 
      emailLabel.text = @"Mail sending failed."; 


     } 
      break; 
     default: 
     { 
      emailLabel.text = @"Mail not sent."; 

     } 
     break; 
    } 

} 

내 문제는 전자 메일 작성 기능이 활성화되었을 때, 나는이 기능에서 와서 내 응용 프로그램에 반환 할 수없는 생각이다. 이것의 유일한 방법은 실제로 진행하고 메시지를 보내는 것입니다. 탐색 모음의 왼쪽 상단에 기본 "취소"바 버튼이 있습니다.이 버튼은 클릭시 "임시 보관함 삭제", "초안 저장"및 "취소"의 세 가지 옵션을 제공합니다. "임시 보관함 삭제"를 선택하면 메시지 작성 화면으로 돌아가는 것 외에는 아무런 효과가 없습니다. 메일 쓰기 기능을 시작한 후 사용자가 앱으로 돌아갈 수 있도록 허용하는 방법이 있나요? 이를 수행하기 위해 "취소"바 버튼에 추가 기능을 추가하는 방법이 있습니까?

미리 답변 해 주신 모든 분께 감사드립니다.

답변

3

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result 방법으로 MFMessageComposeViewControllerDelegate을 구현해야합니다.

이 방법으로 메시지보기를 닫습니다.

편집 : 나는 MFMailComposeViewControllerDelegate와 혼동하지만, 목적은 당신의 ...didFinishWithResult: 방법에 자신의 코드를 살펴 보자 MFMessageComposeViewControllerDelegate

+0

감사합니다. 이미 제 자리에있는이 방법을 포함하도록 제 질문을 편집했습니다. – syedfa

+0

정확히 무엇을하고 싶은지 이해하지 못합니다. 취소시 어떤 동작을 수행하고 싶다면 (보기를 닫는 것을 포함해서),'MFMailComposeResultCancelled' case 문에 코드를 포함하면됩니다. – Yaman

+0

감사합니다. Yaman. 나는 당신의 제안 덕분에 그것을 알아 냈다. 나는 [self dismissModalViewControllerAnimated : YES]라는 줄을 추가했다. 당신이 언급 한 방법으로, 그리고 지금 내가 원하는대로 일하고있다! – syedfa

0

와 동일합니다 :

case MFMailComposeResultCancelled: 
     emailLabel.text = @"Mail sending canceled."; 
     break; 
    case MFMailComposeResultSaved: 
     emailLabel.text = @"Mail saved."; 
     break; 
    case MFMailComposeResultSent: 
    { 
     emailLabel.text = @"Mail sent."; 
     NSLog(@"It's away!"); 


     UIAlertView *emailAlertView = [[UIAlertView alloc] initWithTitle:@"Sent!" message:@"Mail sent successfully." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
     [emailAlertView show]; 
     [self dismissModalViewControllerAnimated:YES]; 
     [self.navigationController popViewControllerAnimated:YES]; 


    } 

을 결과가 당신이있어 MFMailComposeResultSent 경우 모달보기 컨트롤러를 닫고 메일 작성보기 컨트롤러가 사라지는 탐색 스택을 팝하는 동시에 작성보기 컨트롤러를 제공 한보기 컨트롤러를 제거하기 위해 스택을 팝합니다. 그러나 결과가 MFMailComposeResultCancelled 인 경우 일부 레이블 텍스트를 설정하기 만하면됩니다. MFMailComposeResultSaved과 동일합니다. 사용자가 취소하거나 저장할 때 작성보기 컨트롤러를 사라지게하려면 이러한 경우에 대한 메시지 작성보기 컨트롤러를 닫아야합니다.

0

당신은 당신의 뷰 컨트롤러에이 사람이 어떤 일을하는 데 도움이

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { 
    [self becomeFirstResponder]; 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

희망을이 방법을 추가해야합니다.

0

MFMailComposeViewController의 대표단을 설정하십시오. MFMailComposeViewController * mailcomposer = [[MFMailComposeViewController alloc] init];

mailcomposer.mailComposeDelegate = self;

그리고

이 위임 방법 사용 - (무효) mailComposeController : (MFMailComposeViewController *) 컨트롤러 didFinishWithResult을 (MFMailComposeResult) 결과 오류 : (NSError *) 오류 프롬프트 응답에 대한