objective c - In App Email - MFMailComposeViewController - Error Message -
my code follows:
viewcontroller.m:
#import "viewcontroller.h" #import "myscene.h" #import "mainmenu.h" #import <messageui/messageui.h> @implementation viewcontroller - (ibaction)showmailpicker:(id)sender { // must check current device can send email messages before // attempt create instance of mfmailcomposeviewcontroller. if // device can not send email messages, // [[mfmailcomposeviewcontroller alloc] init] return nil. app // crash when calls -presentviewcontroller:animated:completion: // nil view controller. if ([mfmailcomposeviewcontroller cansendmail]) // device can send email. { [self displaymailcomposersheet]; } else // device can not send email. { self.feedbackmsg.hidden = no; self.feedbackmsg.text = @"device not configured send mail."; } } - (ibaction)showsmspicker:(id)sender { // must check current device can send sms messages before // attempt create instance of mfmessagecomposeviewcontroller. if // device can not send sms messages, // [[mfmessagecomposeviewcontroller alloc] init] return nil. app // crash when calls -presentviewcontroller:animated:completion: // nil view controller. if ([mfmessagecomposeviewcontroller cansendtext]) // device can send email. { [self displaysmscomposersheet]; } else // device can not send sms. { self.feedbackmsg.hidden = no; self.feedbackmsg.text = @"device not configured send sms."; } } - (void)displaymailcomposersheet { mfmailcomposeviewcontroller *picker = [[mfmailcomposeviewcontroller alloc] init]; picker.mailcomposedelegate = self; [picker setsubject:@"support brick smasher extreme"]; // set recipients nsarray *torecipients = [nsarray arraywithobject:@"help.bricksx@gmail.com"]; //nsarray *ccrecipients = [nsarray arraywithobjects:@"second@example.com", @"third@example.com", nil]; //nsarray *bccrecipients = [nsarray arraywithobject:@"fourth@example.com"]; [picker settorecipients:torecipients]; //[picker setccrecipients:ccrecipients]; //[picker setbccrecipients:bccrecipients]; // attach image email //nsstring *path = [[nsbundle mainbundle] pathforresource:@"rainy" oftype:@"jpg"]; //nsdata *mydata = [nsdata datawithcontentsoffile:path]; //[picker addattachmentdata:mydata mimetype:@"image/jpeg" filename:@"rainy"]; // fill out email body text nsstring *emailbody = @"message:"; [picker setmessagebody:emailbody ishtml:yes]; //[self presentviewcontroller:picker animated:yes completion:null]; [[[[[uiapplication sharedapplication] delegate] window] rootviewcontroller] presentviewcontroller:picker animated:yes completion:nil]; } - (void)mailcomposecontroller:(mfmailcomposeviewcontroller *)controller didfinishwithresult:(mfmailcomposeresult)result error:(nserror*)error { self.feedbackmsg.hidden = no; // notifies users errors associated interface switch (result) { case mfmailcomposeresultcancelled: self.feedbackmsg.text = @"result: mail sending canceled"; break; case mfmailcomposeresultsaved: self.feedbackmsg.text = @"result: mail saved"; break; case mfmailcomposeresultsent: self.feedbackmsg.text = @"result: mail sent"; break; case mfmailcomposeresultfailed: self.feedbackmsg.text = @"result: mail sending failed"; break; default: self.feedbackmsg.text = @"result: mail not sent"; break; } [self dismissviewcontrolleranimated:yes completion:null]; } @end
the error is: warning: attempt present <mfmailcomposeviewcontroller: 0xbf95170> on <uiviewcontroller: 0xbf78d10> view not in window hierarchy!
i'm trying send email through app. using mfmailcomposeviewcontroller
, gives error (above). when use sms part, there no problem @ all.
using: xcode 5, ios 7, iphone retina 4"
your problem here: [[[[[uiapplication sharedapplication] delegate] window] rootviewcontroller] presentviewcontroller:
first of all, you're calling viewcontroller, can [self presentviewcontroller:]
. sound maybe you're calling displaymailcomposersheet
before viewcontroller
's view visible (viewdidload
or viewwillappear
maybe), raise error you're seeing.
Comments
Post a Comment