在我的應用程序中,我有一個abouts頁面。我想放置一個圓形矩形按鈕,當我按下它時,我將能夠通過嵌入的電子郵件地址向公司發送電子郵件。撰寫iPhone應用程序中的郵件
是否有任何教程要做到這一點?
在此先感謝。
在我的應用程序中,我有一個abouts頁面。我想放置一個圓形矩形按鈕,當我按下它時,我將能夠通過嵌入的電子郵件地址向公司發送電子郵件。撰寫iPhone應用程序中的郵件
是否有任何教程要做到這一點?
在此先感謝。
您必須鏈接到MessageUI
框架並使用類MFMailComposeViewController
。不要忘記導入框架(#import <MessageUI/MessageUI.h>
)。
下面的代碼:
的OBJ-C:
(不要忘記將messageUI框架添加到您的項目!)
先導入信息庫:
#import <MessageUI/MessageUI.h>
然後標記你的自我作爲這樣的委託:
@interface MYViewController() <MFMailComposeViewControllerDelegate>
然後拉起作曲家(如果用戶設置電子郵件的設備上):
- (IBAction)emailButtonPressed:(id)sender
{
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *composeViewController = [[MFMailComposeViewController alloc] initWithNibName:nil bundle:nil];
[composeViewController setMailComposeDelegate:self];
[composeViewController setToRecipients:@[@"[email protected]"]];
[composeViewController setSubject:@"example subject"];
[self presentViewController:composeViewController animated:YES completion:nil];
}
}
然後辦理委託回調並關閉作曲家:
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
//Add an alert in case of failure
[self dismissViewControllerAnimated:YES completion:nil];
}
SWIFT 3:
進口相關庫:
import MessageUI
標記您的視圖控制器作爲像這樣的委託:
class MyViewController: UIViewController, MFMailComposeViewControllerDelegate {
拉起作曲家(如果用戶設置電子郵件的設備上):
@IBAction func emailButtonAction(_ sender: UIButton) {
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients(["[email protected]"])
mail.setSubject("Example Subject")
mail.setMessageBody("<p>Test</p>", isHTML: true)
present(mail, animated: true)
}
}
處理代表回調並關閉作曲者:
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true)
}
這是iOS中打開郵件撰寫代碼:
來源:http://sickprogrammersarea.blogspot.in/2014/03/open-mail-composer-programmatically-in.html
#import "ViewController.h"
@interface ViewController()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(send:) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Send Mail" forState:UIControlStateNormal];
button.frame = CGRectMake(100.0, 350.0, 100.0, 40.0);
[self.view addSubview:button];
}
- (void) send:(id)sender{
MFMailComposeViewController *comp=[[MFMailComposeViewController alloc]init];
[comp setMailComposeDelegate:self];
if([MFMailComposeViewController canSendMail])
{
[comp setToRecipients:[NSArray arrayWithObjects:@"[email protected]", nil]];
[comp setSubject:@"From my app"];
[comp setMessageBody:@"Hello bro" isHTML:NO];
[comp setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentViewController:comp animated:YES completion:nil];
}
else{
UIAlertView *alrt=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:nil cancelButtonTitle:@"" otherButtonTitles:nil, nil];
[alrt show];
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
if(error)
{
UIAlertView *alrt=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:nil cancelButtonTitle:@"" otherButtonTitles:nil, nil];
[alrt show];
[self dismissModalViewControllerAnimated:YES];
}
else{
[self dismissModalViewControllerAnimated:YES];
}
}
我不確定我們是否應該在這裏使用self [dismissModalViewControllerAnimated:YES];相反,我認爲我們應該使用[controller dismissModalViewControllerAnimated:YES]; – 2015-04-29 19:27:24
我似乎在iOS 6創建composerVC當'崩潰***在NSDictionary的斷言失敗* _UIRecordArgumentOfInvocationAtIndex(NSInvocation *,NSUInteger,BOOL)(),/ SourceCache /UIKit_Sim/UIKit-2380.17/UIAppearance.m:1118'我使用[本答案](http:// stackoverflow。com/a/19867457/1219956) – Fonix 2014-06-03 09:00:42
這個問題與UIAppearance有關,上述仍然是實施發送電子郵件的正確方法。 – Matjan 2014-11-02 23:10:59