2011-01-10 12 views
0

私はSMSアプリケーションを実装しようとしています。私が自分のSMSを送信しようとしたとき、[self.navigationController presentModalViewController:picker animated:YES];で例外が発生しました。私はこれに非常に新しいです。みんな助けてくれますか?私のコードは以下の通りです。問題iPhone SDKのMFMessageComposerViewController

MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init]; 
picker.delegate = self; 

picker.recipients = [NSArray arrayWithObject:@"123456789"]; // your recipient number or self for testing 
picker.body = @"test from OS4"; 

[self.navigationController presentModalViewController:picker animated:YES]; 
[picker release]; 

次のように私のログメッセージは、事前に

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target <UINavigationController: 0x5b2c120>. 

おかげで、 S.

答えて

10

あるメッセージがpickerはすなわちMFMessageComposeViewControllerが正常に作成されていない、nilであることを意味します。

はすなわち、YES [MFMessageComposeViewController canSendText]戻っていることを確認します。..

if (![MFMessageComposeViewController canSendText]) { 
    // show message box for user that SMS cannot be sent 
} else { 
    MFMessageComposeViewController* picker = ...; 
    ... 
} 
+0

ありがとうございました。今私は問題が何であるか知っています – sek

1

3つのことは気にしています。

まず、MFMailComposeViewControllerDelegateを実装するようにView Controllerクラスを宣言しましたか?そして、あなたはmailComposeController:didFinishWithResult:error:を定義しましたか?

第2に、次のようなものがあります。[self presentModalViewController:picker animated:YES];

第3に、ピッカーが非ゼロであることを確認してください。

+0

はい私はデリゲートを持っていて、私はmailComposeController:didFinishWithResult:error:を定義しました。 ピッカーはゼロですが、それをゼロにする方法はありますか? – sek

+0

KennyTMのアドバイスに従った?もしそうなら、+ canSendTextは何を返しますか? – westsider

2

おそらく、あなたのiPhoneシミュレータでこれをテストしている、MFMessageComposeViewControllerは

0

nilをシミュレータとリターンでは動作しませんモーダルビューがnil例外をスローすることを主な理由は、通常、被試験デバイスに関係しています設定で電子メールアカウントが設定されていない(したがって、シミュレータで動作しないモーダルビューに関する他のコメント)。 @ KennyTMの答えはこれを処理する素晴らしい方法です。ユーザーに通知する警告ダイアログをポップアップするだけです。