Search code examples
iphonexcodeuiwebview

iphone: How to add Email functionality in iPhone Ebook App


I m working on iPhone eBook app.

In my app books are created using HTML. In Index.html page i have to put the email functionality which email(send) the contents of particular chapter.

Now by using webview i show the index.html and in that i have created UIActionSheet which will show the Email button.

Please suggest me that how can i identify the index of different links to send email of particular chapter.


Solution

  • Below code will work even if you have not configured email in your device.

    Here is the code:

    - (IBAction) sendEmail:(id)sender
    {
        Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    if (mailClass != nil)
    {
        // We must always check whether the current device is configured for sending emails
        if ([mailClass canSendMail])
        {
            [self displayComposerSheet];
        }
        else
        {
            [self launchMailAppOnDevice];
        }
    }
    else
    {
        [self launchMailAppOnDevice];
    }
    }
    
    -(void)displayComposerSheet 
    {
        MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
        picker.mailComposeDelegate = self;
    
        [picker setSubject:@"Hello from DShah!"];
    
        NSArray *toRecipients = [NSArray arrayWithObject:@"[email protected]"]; 
        NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil]; 
        NSArray *bccRecipients = [NSArray arrayWithObject:@"[email protected]"]; 
    
        [picker setToRecipients:toRecipients];
        [picker setCcRecipients:ccRecipients];  
        [picker setBccRecipients:bccRecipients];
    
        NSString *path = [[NSBundle mainBundle] pathForResource:@"userdata" ofType:@"abc"];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.abc", @"userdata"]]; 
        NSData *myData = [NSData dataWithContentsOfFile:fullPath];
        [picker addAttachmentData:myData mimeType:@"csv" fileName:@"userdata.abc"];
    
        NSString *emailBody = @"It is raining in sunny California!";
        [picker setMessageBody:emailBody isHTML:NO];
    
        [self presentModalViewController:picker animated:YES];
        [picker release];
    }
    
    -(void)launchMailAppOnDevice
    {
        NSString *recipients = @"mailto:[email protected]&subject=Hello from DShah!";
        NSString *body = @"&body=It is cold in Winter!";
    
        NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
        email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
    }
    

    Then implement the Delegate method as below....

    - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
    {
        switch (result)
        {
            case MFMailComposeResultCancelled:
                message = @"Result: canceled";
                break;
            case MFMailComposeResultSaved:
                message = @"Result: saved";
                break;
            case MFMailComposeResultSent:
                message = @"Result: sent";
                break;
            case MFMailComposeResultFailed:
                message = @"Result: failed";
                break;
            default:
                message = @"Result: not sent";
                break;
        }
    
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Email Demo" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    
        [self dismissModalViewControllerAnimated:YES];
    }