我想在我的应用程序中设置一个按钮,如果单击该按钮,应用程序可以跳转到 iOS 的默认邮箱。我想这样做,以便用户可以检查并发送他们的邮件。
此功能是否需要私有 API 或者 Apple 禁止这样做?
提前感谢您的帮助。
这可以满足您的要求:
let app = UIApplication.shared
if let url = NSURL(string: "message:"), app.canOpenURL(url) {
app.openURL(url)
}
The canOpenURL
part checks if the user has got at least one email address setup in Mail that they can send/receive from.
也许您可以像这样使用 url 方案:
NSString *email = @"mailto:?subject=YourSubject&body=MsgBody";
email = [email stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];
怎么用MFMailComposeViewController
?您可以设置其主题、收件人、消息正文、附件,然后您可以在您的应用程序中以模态方式呈现它。这也会更好,因为用户不需要离开您的应用程序。
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setSubject:subjectString];
[mailViewController setMessageBody:messageString isHTML:YES];
[self presentViewController:mailViewController animated:YES completion:nil];
}
只需记住将调用上述函数的视图控制器设置为的委托,MFMailComposeViewControllerDelegate
以便之后您可以关闭视图控制器。
-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self dismissViewControllerAnimated:YES completion:nil];
}
关于此类的 Apple 文档:http: //developer.apple.com/library/ios/#documentation/MessageUI/Reference/MFMailComposeViewController_class/Reference/Reference.html