iOS邮件分享有多种方式,我用到的是openURL的方式,这个方式在分享链接时有个问题,如果链接中有用&拼接的参数,当调起邮件时会发现邮件内容中的链接在第一个&符号的地方被截断了导致链接打不开,尝试了很多方法解决了这个问题,在这里分享一下自己的经验。
1、添加邮件的基本信息
//邮件
NSMutableString *mailUrl = [[NSMutableString alloc]init];
//添加收件人
NSArray *toRecipients = [NSArray arrayWithObject: @""];
[mailUrl appendFormat:@"mailto:%@", [toRecipients componentsJoinedByString:@","]];
//添加抄送
NSArray *ccRecipients = [NSArray arrayWithObjects:@"", @"", nil];
[mailUrl appendFormat:@"?cc=%@", [ccRecipients componentsJoinedByString:@","]];
//添加密送
NSArray *bccRecipients = [NSArray arrayWithObjects:@"", nil];
[mailUrl appendFormat:@"&bcc=%@", [bccRecipients componentsJoinedByString:@","]];
//添加主题
[mailUrl appendString:[NSString stringWithFormat:@"&subject=我用「%@」分享了一张照片",[UserAdmin sharedInstance].appName]];
2、处理邮件内容(带&符号的链接)
//添加邮件内容
NSString *endStr = @"https://www.jianshu.com/u/99f4e6822dc6?cardNo=a123¶ms=a2342414&code=b12313414";
// 对特殊字符进行转义将&符号替换为%26
endStr =[endStr stringByReplacingOccurrencesOfString:@"&" withString:@"%26 "];
[mailUrl appendString:[NSString stringWithFormat:@"&body=点击下面链接查看%@ <br/><br/><br/><br/> <a href=\"%@\">%@</a> <br/><br/><br/><br/> %@,放到手机里,把家人的健康放在心里<br/>",titleType,endStr,endStr,[UserAdmin sharedInstance].appName]];
3、编码后调起邮箱
NSString *email = [mailUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email] options:@{UIApplicationOpenURLOptionUniversalLinksOnly:@NO} completionHandler:^(BOOL success) {
}];