0

iOS MailComposer을 열 때 rhodes 기본 확장 프로그램 확장을 쓰려고했습니다. 열기. 이제 코드는 "작동"하지만 MFMailComposeViewController가 표시되지와 엑스 코드 경고 보여줍니다Ios MFMailComposeViewController not displa

Attempt to present <MFMailComposeViewController: 0x12b60840> on <Iosmailto: 0xc1898d0> whose view is not in the window hierarchy! 

내가 UIViewControllers 계층 구조에 있어야 읽을 수 있지만 내가 로즈 루비 코드 또는 명확한-C의 확장에서이를 제공 할 수 없습니다 싸개. 지금 내가 [UIApplication sharedApplication] .keyWindow.rootViewController에서 MFMailComposeController의 UIViewController를 제공하기 위해 노력하고있어하지만 메일 작곡가는 아직 보여주지. iosmailto.h에서

인터페이스 : iosmailto.m에서

@interface Iosmailto : UIViewController<MFMailComposeViewControllerDelegate> 
{ 
} 
    @property(nonatomic, assign) id delegate; 
    //-(IBAction)send_mail:(id)sender; 
@end 

구현 :

@implementation Iosmailto 

    - (void)viewDidLoad { 
     [super viewDidLoad]; 
     [self send_mail]; 
    } 

    - (void)send_mail { 
     if ([MFMailComposeViewController canSendMail]) { 

      MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init]; 
      composer.mailComposeDelegate = self; 
      [composer setSubject:[NSString stringWithUTF8String:subj]]; 

      NSArray *recipients_array = [NSArray arrayWithObject:[NSString stringWithUTF8String:recipients]]; 

      [composer setToRecipients:recipients_array]; 

      NSString *composerBody = [NSString stringWithUTF8String:message]; 
      [composer setMessageBody:composerBody isHTML:NO]; 

      [composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve]; 
      [self presentModalViewController:composer animated:YES]; 

      [composer release]; 
     } else { 

      NSLog(@"Device is unable to send email in its current state."); 
     } 
    } 
    /* some unnecessary for this question methods */ 

@end 

그리고 데스 호출 iosmailto.m 이잖아 정의 C 함수 :

// find root vc 
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController; 

// find topmost vc 
while (topController.presentedViewController) { 
    topController = topController.presentedViewController; 
} 

iosmailer = [[Iosmailto alloc] init]; 

iosmailer.delegate = topController; 
[topController presentViewController:iosmailer animated:YES completion:nil]; 

이 앱은 AppStore 용 앱이 아닙니다. 필요할 경우 해킹을 환영합니다. Iosmailto에 대한

업데이트 초기화 :

- (id)init 
{ 
    self = [super initWithNibName:nil bundle:nil]; 
    return self; 
} 

업데이트 2 (UIViewController에 래퍼 제외)이 코드의 짧은 버전은 내 출발점이었다. 그것도 효과가 없습니다.

UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController; 

while (topController.presentedViewController) { 
    topController = topController.presentedViewController; 
} 
if ([MFMailComposeViewController canSendMail]) { 
    MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init]; 
    composer.mailComposeDelegate = (id<MFMailComposeViewControllerDelegate>)topController ; 

    [composer setSubject:[NSString stringWithUTF8String:subj]]; 
    NSArray *recipients_array = [NSArray arrayWithObject:[NSString stringWithUTF8String:recipients]]; 
    [composer setToRecipients:recipients_array]; 
    NSString *composerBody = [NSString stringWithUTF8String:message]; 
    [composer setMessageBody:composerBody isHTML:NO]; 
    [composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve]; 
    [topController presentModalViewController:composer animated:YES]; 

    [composer release]; 
} else { 
} 
+0

당신의 init 메소드 Iosmailto에 대한 어떤 모습의 현재이란? initWithNib : (사용자의 서브 클래스에서 상속 된 UIViewController에 정의 됨) 또는 사용자 지정 init 메서드가 [super initWithNib :]를 호출하는지 확인해야합니다. – Idles

+0

init을 사용하여 업데이트 함 – rootatdarkstar

+0

래퍼가없는 코드에 대한 업데이트 수행 – rootatdarkstar

답변

5

사용이 코드 뷰 컨트롤러

[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:composer 
                            animated:YES 
                            completion:nil]; 
1

가 좋아 나는 문제가 당신이 그것을 초기화하는 .xib 파일을 사용하지 않기 때문에 당신의 UIViewController는보기 속성에 아무것도하지 않는 것을 생각 : 그리고이 같은 경고가 하나 더 있습니다. See this question은 프로그래밍 방식으로 UIView를 만들고이를 UIViewController의 view 속성에 지정합니다. 나는 이것이 올바른 길이라고 생각합니다. 그들의 -(void)loadView 메소드 구현을 참조하십시오.

Iosmailto UIViewController에 내용이 없으므로 유스 케이스가 조금 이상합니다. 단지 MFMailComposeViewController 주위의 래퍼로 사용하고 있습니다. C 메소드를 다시 구현하는 것을 고려할 수도 있습니다. 아무것도 직접 표시하지 않는이 불필요한 UIViewController의 간접 참조 불필요한 레이어없이 MFMailComposeViewController를 직접 만듭니다.