【问题标题】:iphone: How to add Email functionality in iPhone Ebook Appiphone:如何在 iPhone 电子书应用程序中添加电子邮件功能
【发布时间】:2012-01-27 23:58:52
【问题描述】:

我正在开发 iPhone 电子书应用程序。

在我的应用程序中,书籍是使用 HTML 创建的。 在 Index.html 页面中,我必须放置通过电子邮件发送(发送)特定章节内容的电子邮件功能。

现在通过使用 webview 我显示 index.html 并且我创建了 UIActionSheet 它将显示电子邮件按钮。

请建议我如何识别要发送的不同链接的索引 特定章节的电子邮件。

【问题讨论】:

  • 您是否正在使用 PhoneGap 开发您的应用程序?

标签: iphone xcode uiwebview


【解决方案1】:

即使您没有在设备中配置电子邮件,以下代码也可以使用。

代码如下:

- (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:@"first@example.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];

    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:first@example.com&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]];
}

然后实现如下的Delegate方法......

- (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];
}

【讨论】:

    【解决方案2】:

    查看 Apple 的 MailComposer 示例。这将向您展示您需要的一切。 http://developer.apple.com/library/ios/#samplecode/MailComposer/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008865

    【讨论】:

      猜你喜欢
      • 2010-12-02
      • 1970-01-01
      • 1970-01-01
      • 2011-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-13
      相关资源
      最近更新 更多