您好,登錄后才能下訂單哦!
iPhone提供了內嵌的Mail應用以支持電子郵件相關操作,此外還提供了MFMailComposeViewController以實現了在當前應用內編輯和發送郵件。使用內嵌的Mail應用還是MFMailComposeViewController就取決于實際的需求了,實現方法分別是:
- (void) sendEmailTo:(NSString *)to withSubject:(NSString *)subject withBody:(NSString *)body {
NSString *mailString = [NSString stringWithFormat:@"mailto:?to=%@&subject=%@&body=%@",
[to stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[subject stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[body stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailString]];
}
需要注意的是這里使用了NSString的stringByAddingPercentEscapesUsingEncoding接口,其作用就是在URL字符串中進行特殊字符的替換,比如將空格替換成%20之類的。
- (IBAction)sendMail{
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:@"In app email..."];
[controller setMessageBody:@"...a tutorial from mobileorchard.com" isHTML:NO];
[self presentModalViewController:controller animated:YES];
[controller release];
}
此外還需要實現mailComposeController:didFinishWithResult:error:的協議以進行發送成功或者失敗后的處理:
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
...
[self dismissModalViewControllerAnimated:YES];
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。