- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在我的 iOS 应用程序上使用 MFMailComposeViewController 发送电子邮件。
我在 iPhone 默认邮件应用程序中配置了一个 Gmail 帐户。但是每当我尝试发送电子邮件时,我都会将 MFMailComposeViewController canSendMail
设置为 false,因此我的代码无法打开带有给定收件人、抄送和附件的邮件应用程序
#import <MessageUI/MessageUI.h>
#import "RNMail.h"
#import <React/RCTConvert.h>
#import <React/RCTLog.h>
@implementation RNMail
{
NSMutableDictionary *_callbacks;
}
- (instancetype)init
{
if ((self = [super init])) {
_callbacks = [[NSMutableDictionary alloc] init];
}
return self;
}
- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}
+ (BOOL)requiresMainQueueSetup
{
return YES;
}
RCT_EXPORT_MODULE()
RCT_EXPORT_METHOD(mail:(NSDictionary *)options
callback: (RCTResponseSenderBlock)callback)
{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate = self;
_callbacks[RCTKeyForInstance(mail)] = callback;
if (options[@"subject"]){
NSString *subject = [RCTConvert NSString:options[@"subject"]];
[mail setSubject:subject];
}
bool *isHTML = NO;
if (options[@"isHTML"]){
isHTML = [options[@"isHTML"] boolValue];
}
if (options[@"body"]){
NSString *body = [RCTConvert NSString:options[@"body"]];
[mail setMessageBody:body isHTML:isHTML];
}
if (options[@"recipients"]){
NSArray *recipients = [RCTConvert NSArray:options[@"recipients"]];
[mail setToRecipients:recipients];
}
if (options[@"ccRecipients"]){
NSArray *ccRecipients = [RCTConvert NSArray:options[@"ccRecipients"]];
[mail setCcRecipients:ccRecipients];
}
if (options[@"bccRecipients"]){
NSArray *bccRecipients = [RCTConvert NSArray:options[@"bccRecipients"]];
[mail setBccRecipients:bccRecipients];
}
if (options[@"attachment"] && options[@"attachment"][@"path"] && options[@"attachment"][@"type"]){
NSString *attachmentPath = [RCTConvert NSString:options[@"attachment"][@"path"]];
NSString *attachmentType = [RCTConvert NSString:options[@"attachment"][@"type"]];
NSString *attachmentName = [RCTConvert NSString:options[@"attachment"][@"name"]];
// Set default filename if not specificed
if (!attachmentName) {
attachmentName = [[attachmentPath lastPathComponent] stringByDeletingPathExtension];
}
// Get the resource path and read the file using NSData
NSData *fileData = [NSData dataWithContentsOfFile:attachmentPath];
// Determine the MIME type
NSString *mimeType;
/*
* Add additional mime types and PR if necessary. Find the list
* of supported formats at http://www.iana.org/assignments/media-types/media-types.xhtml
*/
if ([attachmentType isEqualToString:@"jpg"]) {
mimeType = @"image/jpeg";
} else if ([attachmentType isEqualToString:@"png"]) {
mimeType = @"image/png";
} else if ([attachmentType isEqualToString:@"doc"]) {
mimeType = @"application/msword";
} else if ([attachmentType isEqualToString:@"ppt"]) {
mimeType = @"application/vnd.ms-powerpoint";
} else if ([attachmentType isEqualToString:@"html"]) {
mimeType = @"text/html";
} else if ([attachmentType isEqualToString:@"csv"]) {
mimeType = @"text/csv";
} else if ([attachmentType isEqualToString:@"pdf"]) {
mimeType = @"application/pdf";
} else if ([attachmentType isEqualToString:@"vcard"]) {
mimeType = @"text/vcard";
} else if ([attachmentType isEqualToString:@"json"]) {
mimeType = @"application/json";
} else if ([attachmentType isEqualToString:@"zip"]) {
mimeType = @"application/zip";
} else if ([attachmentType isEqualToString:@"text"]) {
mimeType = @"text/*";
} else if ([attachmentType isEqualToString:@"mp3"]) {
mimeType = @"audio/mpeg";
} else if ([attachmentType isEqualToString:@"wav"]) {
mimeType = @"audio/wav";
} else if ([attachmentType isEqualToString:@"aiff"]) {
mimeType = @"audio/aiff";
} else if ([attachmentType isEqualToString:@"flac"]) {
mimeType = @"audio/flac";
} else if ([attachmentType isEqualToString:@"ogg"]) {
mimeType = @"audio/ogg";
} else if ([attachmentType isEqualToString:@"xls"]) {
mimeType = @"application/vnd.ms-excel";
}
// Add attachment
[mail addAttachmentData:fileData mimeType:mimeType fileName:attachmentName];
}
UIViewController *root = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
while (root.presentedViewController) {
root = root.presentedViewController;
}
[root presentViewController:mail animated:YES completion:nil];
} else {
callback(@[@"not_available"]);
}
}
#pragma mark MFMailComposeViewControllerDelegate Methods
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
NSString *key = RCTKeyForInstance(controller);
RCTResponseSenderBlock callback = _callbacks[key];
if (callback) {
switch (result) {
case MFMailComposeResultSent:
callback(@[[NSNull null] , @"sent"]);
break;
case MFMailComposeResultSaved:
callback(@[[NSNull null] , @"saved"]);
break;
case MFMailComposeResultCancelled:
callback(@[[NSNull null] , @"cancelled"]);
break;
case MFMailComposeResultFailed:
callback(@[@"failed"]);
break;
default:
callback(@[@"error"]);
break;
}
[_callbacks removeObjectForKey:key];
} else {
RCTLogWarn(@"No callback registered for mail: %@", controller.title);
}
UIViewController *ctrl = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
while (ctrl.presentedViewController && ctrl != controller) {
ctrl = ctrl.presentedViewController;
}
[ctrl dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark Private
static NSString *RCTKeyForInstance(id instance)
{
return [NSString stringWithFormat:@"%p", instance];
}
@end
iOS 版本:12.1.2
我想知道最近是否对 MFMailComposeViewController
进行了任何更改并导致了此问题?
我已经配置了邮件帐户并且工作正常。所以它不是已经发布的任何问题的副本
最佳答案
MFMailComposeViewController 直接链接到 Apple 的邮件应用程序。它只会在安装和配置 Apple 的邮件应用程序时返回 true。
如果你想打开 Gmail(或你安装的任何默认应用程序),那么你需要使用 mailto://方案
这是我完成 mailto 部分的功能 - 包括从 html 到纯文本的荒谬简单的转换!
请注意,mailto://不支持附件或 html 内容。
/// Send email using mailto
/// - Parameters:
/// - to: array of email addresses
/// - subject: subject
/// - body: body
/// - isHtml: isHtml (only <br/> and <br /> are supported)
/// - Returns: true if sent
func sendByURL(to:[String],subject:String,body:String, isHtml:Bool) -> Bool {
var txtBody = body
if isHtml {
txtBody = body.replacingOccurrences(of: "<br />", with: "\n")
txtBody = txtBody.replacingOccurrences(of: "<br/>", with: "\n")
if txtBody.contains("/>") {
print("Can't send html email with url interface")
return false
}
}
let toJoined = to.joined(separator: ",")
guard var feedbackUrl = URLComponents.init(string: "mailto:\(toJoined)") else {
return false
}
var queryItems: [URLQueryItem] = []
queryItems.append(URLQueryItem.init(name: "SUBJECT", value: subject))
queryItems.append(URLQueryItem.init(name: "BODY",
value: txtBody))
feedbackUrl.queryItems = queryItems
if let url = feedbackUrl.url {
// This is probably an unnecessary check
// You do need to add 'mailto' to your LSApplicationQuerySchemes for the check to be allowed
// <key>LSApplicationQueriesSchemes</key>
// <array>
// <string>mailto</string>
// </array>
if UIApplication.shared.canOpenURL(url){
UIApplication.shared.open(url)
return true
}
}
return false
}
关于ios - MFMailComposeViewController canSendMail 总是返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54350041/
class test { public static void main(String[] args){ Object o1 = new Object(); O
我以为我理解了 Python 中的这两个单例值,直到我看到有人在代码中使用 return l1 or l2,其中 l1 和 l2 都是链表对象,并且(s)他想如果不为 None 则返回 l1,否则返回
这个问题在这里已经有了答案: Why does the expression 0 >> (True == False) is False True >>> True == (False is Fals
为什么在 Python 中它是这样评估的: >>> False is False is False True 但是当用括号尝试时表现如预期: >>> (False is False) is False
我有一个名为“apple”的表,我编写了以下查询: select name, count(name), case when istasty is null then fal
python boolean 逻辑中的运算符优先级 print(False==True or False) #answer is True print(False==(False or True))#
请不要看条件,因为它们在这里是为了便于理解行为 为什么 result 等于 true ? boolean result = false && (false)?false:true; 我知道我们可以通过
乍一看,这篇文章可能看起来像是重复的,但事实并非如此。相信我,我已经查看了所有 Stack Overflow,但都无济于事。 无论如何,我从 Html.CheckBoxFor 得到了一些奇怪的行为。
这个问题在这里已经有了答案: python operator precedence of in and comparison (4 个答案) 关闭 6 年前。 我的一位前辈演示了它,我想知道这是否是
我最近参加了 Java 的入门测试,这个问题让我很困惑。完整的问题是: boolean b1 = true; boolean b2 = false; if (b2 != b1 != b2) S
为什么 {} == false 评估为 false 而 [] == false 评估为 true在 javascript 中? 最佳答案 这是根据 Abstract Equality Comparis
这个问题在这里已经有了答案: Why does (1 in [1,0] == True) evaluate to False? (1 个回答) 关闭7年前。 为什么使用括号时这些语句按预期工作: >>
我试过搜索这个,但我真的不知道如何表达它以查看是否有其他人发布了答案。 但是,我正在制作一个国际象棋游戏和一个人工智能来配合它,这是非常困难的,我的问题是当我检查两个棋子是否在同一个团队时我必须做 (
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
为什么 为 false || null 返回与 null || 不同的结果错误? 我可以安全地依赖 return myVar || false 如果 myVar 为 null 或 false,则返回
我正在尝试遵循 NHibernate 教程,“你的第一个基于 NHibernate 的应用程序:修订 #4”在 NHibernate Forge。 但线路:new SchemaExport(cfg).
这个问题在这里已经有了答案: Empty list boolean value (3 个答案) 关闭 4 年前。 我是 Python 的新手,不理解以下行为: 为什么要声明 [] == False
以下函数循环访问对象的值。如果值为空this.hasInvalidValue设置为true ,如果不为空 this.hasInvalidValue设置为false : user: { email:
所以我正在玩 java.lang.reflect 东西并尝试制作类似 this 的东西。这是我的问题(可能是一个错误): 将字段设置为 true 的方法的代码: private static void
当我在编程时,我的 if 语句出现了意想不到的结果。 这个代码警报怎么会是真的?我在 W3S 没有找到任何可以帮助我的东西,我真的很想知道为什么这些警报是“正确的” window.alert(fals
我是一名优秀的程序员,十分优秀!