2011-12-26 112 views
0

餘米的iPhone電子書應用程序的工作添加電子郵件功能。iPhone:如何在iPhone電子書應用程序

在我的應用程序的書籍使用HTML創建的。 在Index.html頁面中,我必須將電子郵件功能發送到哪個電子郵件(發送)特定章節的內容。

現在使用的WebView我顯示的index.html和我創建UIActionSheet這將顯示電子郵件按鈕。

請給我建議,我怎麼能識別不同環節的指數發送特定章節的 電子郵件。

+1

你發展與PhoneGap的您的應用程序? – Maulik 2011-12-26 13:24:36

回答

5

下面的代碼將工作,即使你沒有在你的設備配置的電子郵件...

下面是代碼:

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

然後實現委託方法如下....

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

NJOY編碼....:d

相關問題