我通过使用MFMessageComposeViewController得到此错误

时间:2015-01-19 10:51:59

标签: ios iphone

我使用MFMessageComposeViewController收到此错误 当我们点击inviteButton时,我们必须转到Message Composer,但我们收到错误,这样可以帮助我们..........

 - (IBAction)inviteButton:(id)sender {
            //![MFMessageComposeViewController canSendText]
            if(true){
                MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
                controller.body = @"Hello";
                controller.recipients = [NSArray arrayWithObjects:@"+1234567890", nil];
                controller.messageComposeDelegate = self;
                [self presentViewController:controller animated:YES completion:nil];
            }
        }

        - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
            {
                switch (result) {
                    case MessageComposeResultCancelled:
                        break;

                    case MessageComposeResultFailed:
                    {
                        UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to send SMS!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                        [warningAlert show];
                        break;
                    }

                    case MessageComposeResultSent:
                        break;

                    default:
                        break;
                }

                [self dismissViewControllerAnimated:YES completion:nil];
            }

n如下所示收到错误-------->>>>>

2015-01-19 16:12:25.060 SourceSage[3407:51781] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target <TellFriendViewController: 0x7f97d8deaac0>.'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000105dc5f35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001052a7bb7 objc_exception_throw + 45
    2   UIKit                               0x0000000103810ed3 -[UIViewController _presentViewController:withAnimationController:completion:] + 2916
    3   UIKit                               0x0000000103812d81 __62-[UIViewController presentViewController:animated:completion:]_block_invoke + 132
    4   UIKit                               0x0000000103812ca5 -[UIViewController presentViewController:animated:completion:] + 229
    5   SourceSage                          0x00000001029cb497 -[TellFriendViewController inviteButton:] + 343
    6   UIKit                               0x00000001036e08be -[UIApplication sendAction:to:from:forEvent:] + 75
    7   UIKit                               0x00000001036e08be -[UIApplication sendAction:to:from:forEvent:] + 75
    8   UIKit                               0x00000001037e7410 -[UIControl _sendActionsForEvents:withEvent:] + 467
    9   UIKit                               0x00000001037e67df -[UIControl touchesEnded:withEvent:] + 522
    10  UIKit                               0x0000000103726308 -[UIWindow _sendTouchesForEvent:] + 735
    11  UIKit                               0x0000000103726c33 -[UIWindow sendEvent:] + 683
    12  UIKit                               0x00000001036f39b1 -[UIApplication sendEvent:] + 246
    13  UIKit                               0x0000000103700a7d _UIApplicationHandleEventFromQueueEvent + 17370
    14  UIKit                               0x00000001036dc103 _UIApplicationHandleEventQueue + 1961
    15  CoreFoundation                      0x0000000105cfb551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    16  CoreFoundation                      0x0000000105cf141d __CFRunLoopDoSources0 + 269
    17  CoreFoundation                      0x0000000105cf0a54 __CFRunLoopRun + 868
    18  CoreFoundation                      0x0000000105cf0486 CFRunLoopRunSpecific + 470
    19  GraphicsServices                    0x0000000107ab29f0 GSEventRunModal + 161
    20  UIKit                               0x00000001036df420 UIApplicationMain + 1282
    21  SourceSage                          0x0000000102a747c3 main + 115
    22  libdyld.dylib                       0x00000001065bc145 start + 1
    23  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

3 个答案:

答案 0 :(得分:2)

在按钮点击事件中将您的代码替换为下面的内容。

 - (IBAction)inviteButton:(id)sender 
  {
      if(![MFMessageComposeViewController canSendText]) {
        UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Your device doesn't support SMS!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [warningAlert show];
        return;
       }

   NSArray *recipents = @[@"12345678", @"72345524"];
   NSString *message = [NSString stringWithFormat:@"Just sent the %@ file to your email. Please check!", @"File"];

   MFMessageComposeViewController *messageController =       [[MFMessageComposeViewController alloc] init];
   messageController.messageComposeDelegate = self;
  [messageController setRecipients:recipents];
  [messageController setBody:message];

// Present message view controller on screen

  if ( messageController )
  {
      [self.navigationController presentViewController:messageController animated:YES completion:nil];
  }
  else
  {
      // Gracefully handle the error
  }
 }

答案 1 :(得分:0)

请尝试以下代码:

- (IBAction)inviteButton:(id)sender 
{
            //![MFMessageComposeViewController canSendText]
    if(true)
    {
           MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
           controller.body = @"Hello";
           controller.recipients = [NSArray arrayWithObjects:@"+1234567890", nil];
           controller.messageComposeDelegate = self;
           if(controller)
           [self presentViewController:controller animated:YES completion:nil];
     }
}

答案 2 :(得分:0)

替换

[self presentViewController:viewcontroller animated:YES completion:nil];

[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:viewcontroller animated:YES completion:nil];