开发者

Application crashing on email in device

I have added a function of "Email" in my iPhone application and it is crashing on iPhone but working on simulator. Please guide me why it is crashing on Device

this is the code

-(IBAction)sendMail{
    MFMailComposeViewController *controller = [[MFMailComposeViewCo开发者_Python百科ntroller alloc] init];
    controller.mailComposeDelegate = self;
    [controller setSubject:@"Contact"];

    [controller setToRecipients:[NSArray arrayWithObject:@"Contact@BettorsSidekick.com"]];
    [controller setMessageBody:@"" isHTML:NO];
    [self presentModalViewController:controller animated:YES];
    [controller release];
}

- (void)mailComposeController:(MFMailComposeViewController*)controller  
          didFinishWithResult:(MFMailComposeResult)result 
                        error:(NSError*)error;
{
    if (result == MFMailComposeResultSent) {
        NSLog(@"It's away!");
    }
    [self dismissModalViewControllerAnimated:YES];
}


use this code its work fine.

-(IBAction)Btn_EmailPressed:(id)sender{
    if (![MFMailComposeViewController canSendMail]) {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Email cannot be configure." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }else {
        picker = [[MFMailComposeViewController alloc] init];
        picker.mailComposeDelegate=self;
        [picker setToRecipients:nil];
        [picker setSubject:@"Email"];
        [picker setMessageBody:nil isHTML:NO];
        NSArray *toRecipients = [[NSArray alloc] initWithObjects:lblSite.text,nil];
        [picker setToRecipients:toRecipients];
        [self presentModalViewController:picker animated:YES];
    }
}


- (void)mailComposeController:(MFMailComposeViewController*)mailController didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
    NSString *msg1;
    switch (result)
    {
        case MFMailComposeResultCancelled:
            msg1 =@"Sending Mail is cancelled";
            break;
        case MFMailComposeResultSaved:
            msg1=@"Sending Mail is Saved";
            break;
        case MFMailComposeResultSent:
            msg1 =@"Your Mail has been sent successfully";
            break;
        case MFMailComposeResultFailed:
            msg1 =@"Message sending failed";
            break;
        default:
            msg1 =@"Your Mail is not Sent";
            break;
    }
    UIAlertView *mailResuletAlert = [[UIAlertView alloc]initWithFrame:CGRectMake(10, 170, 300, 120)];
    mailResuletAlert.message=msg1;
    mailResuletAlert.title=@"Message";
    [mailResuletAlert addButtonWithTitle:@"OK"];
    [mailResuletAlert show];
    [mailResuletAlert release];
    [self dismissModalViewControllerAnimated:YES];  
}


try this

MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];


    if(controller && [MFMailComposeViewController canSendMail]){

        controller.mailComposeDelegate = self;
        [controller setSubject:@"Contact"];

        [controller setToRecipients:[NSArray arrayWithObject:@"Contact@BettorsSidekick.com"]];
        [controller setMessageBody:@"" isHTML:NO];
        [self presentModalViewController:controller animated:YES];


    }

if (controller) {

    [controller release];        

}


if you not configured any email account in your iphone device may be this could be the reason of crashing because when you call mfmailcomposer it will work on simulator but it will fail on device and result will be a crash so configure the mail on device then try the code above .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜