代码之家  ›  专栏  ›  技术社区  ›  pythonquick

[mfmailcomposiewcontroller cansendmail]何时返回no

  •  42
  • pythonquick  · 技术社区  · 16 年前

    我的iphone应用程序正在使用mfmailcomposiewcontroller类发送带有附件的应用内电子邮件。 如果mfmailcomposeviewcontroller类的“cansendmail”方法返回true(yes),应用程序将只尝试显示邮件生成器对话框。具体来说,如果以下方法返回“是”,则显示邮件生成器,否则将向用户显示一个错误警报对话框,指出设备上没有设置电子邮件帐户:

    - (BOOL)canDeviceSendEmail
    {
        Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
        return mailClass != nil && [mailClass canSendMail];
    }
    

    一组测试人员报告说,即使在设备上设置了电子邮件帐户,他们也会收到此错误警报对话框。测试人员使用了带有OS3.1.3的iPhone3G。因此,mfmailcomposiewcontroller类必须已经存在,“cansendmail”方法必须返回no。

    因此,我的问题是:除了设备上没有设置电子邮件帐户的情况外,“cansendmail”方法在其他什么情况下可以返回no?

    谢谢

    9 回复  |  直到 8 年前
        1
  •  70
  •   byneri    15 年前

    如果设备上至少启用了一个电子邮件帐户,则以下呼叫应返回“是”:

    [MFMailComposeViewController canSendMail]
    

    相反,如果所有帐户都被禁用/删除,则返回no。

        2
  •  10
  •   kennytm    16 年前

    为了 +canSendMail 若要返回“是”,必须设置默认帐户 用于发送电子邮件 .

    (内部, +[MFMailComposeViewController canSendMail] 电话 +[MailAccountProxy defaultMailAccountForDelivery] ,它发现第一个邮件帐户 -isDefaultDeliveryAccount )

        3
  •  6
  •   Steve Moser    10 年前

    除了没有在设备上设置电子邮件帐户之外, canSendMail 也回报 NO 安装MDM配置文件并将其配置为不允许第三方应用程序发送邮件时。在这种情况下,您必须使用 openURL: 用一个 mailto: 用于启动mail.app并可选地填写“收件人”、“抄送”、“密件抄送”、“主题”和“正文”字段的URL。

    麦尔托:RFC - https://tools.ietf.org/html/rfc2368

        4
  •  3
  •   Tunvir Rahman Tusher    11 年前

    这对我有效。

    在设备转到设置中->邮件、联系人、日历->帐户

    在这里您可以看到没有添加帐户。现在 增加帐户 现在回到你的应用程序,你可以发现它这次返回是,你可以发送电子邮件。 谢谢

        5
  •  1
  •   David.Chu.ca    9 年前

    在iOS10.1的情况下,您可以先启用iCloud的邮件(在“设置”中)。然后登录您的iCloud帐户。所有的联系人和电子邮件应该可以在您的模拟器后,这些变化。

    有了变化,我可以 true 来自:

    MFMailComposeViewController.canSendMail()
    

    我正在使用Xcode 8.1和Swift 3。

        6
  •  1
  •   Vladimir    8 年前

    当您在设备上没有任何邮件帐户时,[mfmailcomposiewcontroller cansendmail]将返回no。在这种情况下,您可以通过以下代码打开邮件应用程序:

    - (void)openMailApp {
        NSString *recipients = @"mailto:?cc=&subject=";
        NSString *body = @"&body=";
        NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
        email = [email stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
    }
    
        7
  •  0
  •   Tom    16 年前

    你不能肯定 MFMailComposeViewController 一定是因为你的方法没有区别 mfmailcomposiewcontroller程序 不存在和 [MFMailComposeViewController canSendMail] 返回 NO .

    您正在测试的iphone操作系统与此无关;您的应用程序链接所针对的iphone sdk版本决定了mfmailcomposeviewcontroller在运行时是否可用。如果你想要这个类,你需要用sdk 3.0或更高版本来构建。

        8
  •  0
  •   Alfonso    16 年前

    是否包含消息传递框架?我曾经遇到过类似的问题,只是因为我忘记向xcode项目添加正确的框架。

        9
  •  0
  •   TheJeff    8 年前

    弗拉基米尔的回答没有弃用的函数:

        NSString *recipients = @"mailto:testingEmail@example.com?subject=emailsubject";
    
        NSString *body = @"&body=body:";
        NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
        email = [email stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email] options:@{} completionHandler:^(BOOL success) {
        }];