您應該使用MFMailComposeViewController
,它被設計用於從應用程序發送電子郵件。這裏是你如何使用它的代碼示例:
#import "ViewController.h"
#import <MessageUI/MFMailComposeViewController.h>
@interface ViewController() <MFMailComposeViewControllerDelegate>
@end
@implementation ViewController
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self sendEmail];
}
- (void) sendEmail {
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController* mailController = [[MFMailComposeViewController alloc] init];
mailController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor]};
mailController.mailComposeDelegate = self;
[mailController setSubject:NSLocalizedString(@"Feedback", @"")];
[mailController setTitle:NSLocalizedString(@"Feedback", @"")];
[mailController setToRecipients:@[@"[email protected]"]];
[mailController setMessageBody:@"Hello & ampersand" isHTML:NO];
if (mailController) {
[self presentViewController:mailController animated:YES completion:nil];
}
}
else {
//show UIAlertView
}
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
//Handle completion
}
@end
有不會與&
或'
任何問題
編輯:
如果你仍然想使用Gmail應用嘗試下面的代碼,它爲我工作:
NSString *messageBodyRaw = @"Hi I want sell this application & want be free\n\n";
NSString *messageBody = [messageBodyRaw urlEncodeUsingEncoding:NSUTF8StringEncoding];
NSString *urlFinal = [NSString stringWithFormat: @"googlegmail:///co?subject=%@&body=%@", strSubject, messageBody];
NSURL *emailUrlFinal = [NSURL URLWithString:urlFinal];
[[UIApplication sharedApplication] openURL:emailUrlFinal];
NSString + URLEncoding.h:
#import <Foundation/Foundation.h>
@interface NSString (URLEncoding)
-(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding;
@end
的NSString + URLEncoding.m:
#import "NSString+URLEncoding.h"
@implementation NSString (URLEncoding)
-(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding {
return (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)self,
NULL,
(CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ",
CFStringConvertNSStringEncodingToEncoding(encoding)));
}
@end
你制定機構與'isHTML:NO'參數? – alexburtnik
@alexburtnik我使用[[UIApplication sharedApplication] openURL:emailUrlFinal]發送郵件; 在這裏,我無法找到這樣的選項。 –
用完整的代碼示例來編輯你的問題,你如何做到這一點。 「emailUrlFinal」究竟是什麼? – alexburtnik