-1

我想知道如何通过电子邮件发送 webview 链接。然后,当收件人收到电子邮件时,他们可以在 Safari 中打开链接。提前致谢!

我把这个 BOOL 放在我的代表中

 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ([[[request URL] scheme] isEqual:@"mailto"]) { 
    [[UIApplication sharedApplication] openURL:[request URL]]; return NO; 
} 
return YES; 
}

MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init]; 
[composer setMailComposeDelegate:self]; 
if ([MFMailComposeViewController canSendMail]) { 
    [composer setToRecipients:[NSArray arrayWithObjects:@"", nil]];
    [composer setSubject:@""]; 
    [composer setMessageBody:webview isHTML:YES]; 
    [composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve]; 
    [self presentModalViewController:composer animated:YES];
    [composer release];
}
4

2 回答 2

1

您在列表中做两件不同的事情:

  • 如果您想在 web 视图中显示MFMailComposeViewController点击mailto:链接,则代码的第一部分(在 中shouldStartLoadWithRequest)应该可以工作,除非您必须更改isEqualisEqualToString

    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
      if ([[[request URL] scheme] isEqualToString:@"mailto"]) {  //Change in this line
          [[UIApplication sharedApplication] openURL:[request URL]]; return NO; 
      } 
      return YES; 
    }
    

    (请注意,使用此功能时用户将离开您的应用程序)

  • 如果您想将用户点击的链接发送给某人,您可以使用类似

    -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
    MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init]; 
    [composer setMailComposeDelegate:self]; 
    if ([MFMailComposeViewController canSendMail]) { 
        [composer setToRecipients:[NSArray arrayWithObjects:@"", nil]];
        [composer setSubject:@"Link"]; 
        [composer setMessageBody:request.URL isHTML:YES]; 
        [composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve]; 
        [self presentModalViewController:composer animated:YES];
        [composer release];
    }
    
    return YES; 
    }
    
于 2012-09-30T10:18:32.073 回答
0
 MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
        [composer setMailComposeDelegate:self];
        if ([MFMailComposeViewController canSendMail]) {
            [composer setToRecipients:[NSArray arrayWithObjects:@"", nil]];
            [composer setSubject:@""];
            [composer setMessageBody:webview.request.URL.absoluteString isHTML:YES];
            [composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
            [self presentModalViewController:composer animated:YES];
            [composer release];
        }
        else
            [composer release];
    }
}
于 2012-10-04T23:12:08.493 回答