gpt4 book ai didi

ios - Mail Composer 不会关闭 iOS

转载 作者:行者123 更新时间:2023-11-28 18:59:13 26 4
gpt4 key购买 nike

我有带按钮的 ViewController 和按钮上的操作:

        - (IBAction)clickMe:(id)sender {
MailHelper *helper = [[MailHelper alloc] init];
[helper setAllData:self];
}

此外,还有用于邮件撰写的辅助类(MailHelper.h):

    #import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>

@interface MailHelper : UIViewController<MFMailComposeViewControllerDelegate>

@property MFMailComposeViewController* mailView;
- (void)setAllData:(UIViewController *)ctrl;

@end

和实现(MailHelper.m):

- (void)setAllData:(UIViewController *)ctrl {

mailView = [[MFMailComposeViewController alloc] init];
mailView.mailComposeDelegate = self;

mailView.toRecipients = @[@"mail@email.com"];
[mailView setSubject:@"Subject"];

[ctrl presentViewController:mailView animated:YES completion:nil];
}

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {

[self dismissViewControllerAnimated:YES completion:nil];
}

我可以打开邮件编辑器,但在发送邮件、保存草稿或删除草稿时应用程序崩溃。有什么想法吗?

最佳答案

MFMailComposeViewController请求被解雇 您正在调用 self 上的解雇方法但是self正在展示 te MFMailComposeViewController .变化:

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {

[self dismissViewControllerAnimated:YES completion:nil];
}

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {

[controller dismissViewControllerAnimated:YES completion:nil];
}

要解决重新分配问题,请添加以下内容:

导入<objc/runtime.h>在你的 .m 文件中制作 self与传递的 Controller 相关联:

static void * MailHelperKey = &MailHelperKey;

- (void)setAllData:(UIViewController *)ctrl {
mailView = [[MFMailComposeViewController alloc] init];
mailView.mailComposeDelegate = self;

objc_setAssociatedObject(mailView, MailHelperKey, self, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
....
}


-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {

[controller dismissViewControllerAnimated:YES completion:nil];
objc_removeAssociatedObjects(controller);
}

这将确保只要呈现控件未被释放您的邮件助手。不值得从美丽的价格,但应该工作。

关于ios - Mail Composer 不会关闭 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28089167/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com