计算组成的邮件/邮件的收件人数

时间:2014-09-26 10:29:28

标签: ios objective-c mfmailcomposeviewcontroller

我想在我的应用程序中实现一项功能,即如果用户通过电子邮件或超过5人的短信共享应用程序链接,他/她可以解锁应用程序的完整版本。我已经看到了应用程序使用的这种方法。 (似乎可以通过Facebook和Twitter分享使用Facebook标记和推特提及)

所以,我的问题是,有没有办法在不使用Web服务或任何其他服务器端方法的情况下实现此逻辑?

有没有办法知道使用默认电子邮件/消息编写器发送的特定电子邮件或短信的收件人数?

1 个答案:

答案 0 :(得分:0)

在我的某个应用中,我会在会议/ NSArray电子邮件中向所有收件人发送多封电子邮件 当电子邮件完成发送时,如果电子邮件已成功发送,已取消,已保存到草稿等,则其委派方法会报告 然后,我有一个块,在发送时运行以将此人标记为已发送并通过电子邮件发送

以下代码会发送单个电子邮件,其中附件包含数组中的多个用户

-(void)createEmailTo:(NSArray *)people path:(NSString *)path fileName:(NSString *)fileName subject:(NSString *)subject messageBody:(NSString *)messageBody{
//Create Email, see ccode later
FTAEmailMachine *emailComposer = [[FTAEmailMachine alloc] initMailTo:people
                                                             subject:subject
                                                         messageBody:messageBody
                                                          pathToFile:path
                                                            fileName:fileName];
//attach a dimiss block for SENT mail machines delagates, this scrolls through people in array sending them each a mail
emailComposer.dismissBlockSent = ^{
    NSOrderedSet *contactsActions =[self.contactEmailArray[self.arrayCounter] valueForKey:@"contactsActions"];
    if (self.arrayCounter <= self.emailArrayCount) {
        for (FTAAction *action in contactsActions) {
            [action setActionNoticeSent:YES]; // <-------- this is the important bit your after, i am setting a varible on the message being SENT
        }
    }
    //adjust the array count of people
    self.emailArrayCount -= 1;
    self.arrayCounter += 1;
    //Send the next mail in the array
    if (self.emailArrayCount > 0) {
        NSArray *contactEmailArray = [[NSArray alloc] initWithObjects:[self.contactEmailArray[self.arrayCounter] valueForKey:@"contactEmail"], nil];

                [self createEmailTo:contactEmailArray
                               path:path
                           fileName:fileName
                            subject:[self.contactEmailArray[self.arrayCounter] valueForKey:@"subject"]
                        messageBody:[self.contactEmailArray[self.arrayCounter] valueForKey:@"messageBody"]];
    }else{
        [FTAAppIdioms deleteFileFrom:path];
    }
};
//this is the blovcks that initiates if the mail was cancelled and not sent ** notice there isnt [action setActionNoticeSent:YES]; as before
emailComposer.dismissBlockCancelled = ^{

    //When the email is dismissed after being actually sent
    self.emailArrayCount -= 1;
    self.arrayCounter += 1;

    if (self.emailArrayCount > 0) {
        NSArray *contactEmailArray = [[NSArray alloc] initWithObjects:[self.contactEmailArray[self.arrayCounter] valueForKey:@"contactEmail"], nil];
        [self createEmailTo:contactEmailArray
                       path:path
                   fileName:fileName
                    subject:[self.contactEmailArray[self.arrayCounter] valueForKey:@"subject"]
                messageBody:[self.contactEmailArray[self.arrayCounter] valueForKey:@"messageBody"]];
    }else{

        [FTAAppIdioms deleteFileFrom:path];
    }
};

//present email as view
[self presentViewController:emailComposer
                   animated:NO
                 completion:^{}];

}

我的电子邮件机.h文件看起来像这样,请注意块

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

@interface FTAEmailMachine : UIViewController
<MFMailComposeViewControllerDelegate>

//designated init
-(instancetype)initMailTo:(NSArray *)people subject:(NSString *)subject messageBody:(NSString *)mesageBody pathToFile:(NSString *)path fileName:(NSString *)fileName;

//dismiss block
@property(nonatomic, copy) void (^dismissBlockSent)(void);
@property(nonatomic, copy) void (^dismissBlockCancelled)(void);
@end

执行工作的电子邮件机器.m文件中的主要方法如下所示:

-(void)sendMailTo:(NSArray *)people subject:(NSString *)subject messageBody:(NSString *)mesageBody pathToFile:(NSString *)path fileName:(NSString *)fileName{

//check if device can send mail
if ([MFMailComposeViewController canSendMail]) {

    MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc]init];
    mailComposer.mailComposeDelegate = self;

    [mailComposer setSubject:subject];
    [mailComposer setMessageBody:mesageBody isHTML:YES];
    if (people) {
        [mailComposer setToRecipients:people];
    }

    //check if there is an attachment
    if (fileName) {
        // Determine the file name and extension
        NSArray *filepart = [fileName componentsSeparatedByString:@"."];
        NSString *extension = [filepart objectAtIndex:1];

        // Determine the MIME type
        NSString *mimeType;
        if ([extension isEqualToString:@"pdf"]) {
            mimeType = @"application/pdf";
        } else if ([extension isEqualToString:@"csv"]) {
            mimeType = @"text/csv";
        } else if ([extension isEqualToString:@"plist"]) {
            mimeType = @"image/plist";
        } else if ([extension isEqualToString:@"jpg"]) {
            mimeType = @"image/jpeg";
        } else if ([extension isEqualToString:@"png"]) {
            mimeType = @"image/png";
        } else if ([extension isEqualToString:@"doc"]) {
            mimeType = @"application/msword";
        } else if ([extension isEqualToString:@"ppt"]) {
            mimeType = @"application/vnd.ms-powerpoint";
        } else if ([extension isEqualToString:@"html"]) {
            mimeType = @"text/html";
        }

        //attach the file to the email
        [mailComposer addAttachmentData:[NSData dataWithContentsOfFile:path]
                               mimeType:mimeType
                               fileName:fileName];
    }

    [self presentViewController:mailComposer
                       animated:YES
                     completion:nil];

} else {
    //crferate a slert if device cant send mail
    [FTAAppIdioms createOkAlertWithTitle:@"Email"
                                 message:@"Sorry your device can't send emails"
                          viewController:self
                                     tag:0];
}
}

那么重要的一点就是委托方法,注意解雇块:

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

switch (result)
{
    case MFMailComposeResultCancelled:
        //notice this dimissblock action
        [[self presentingViewController] dismissViewControllerAnimated:YES
                                                            completion:self.dismissBlockCancelled];
        break;
    case MFMailComposeResultSaved:

        break;
    case MFMailComposeResultSent:
        //notice this dimissblock action
        [[self presentingViewController] dismissViewControllerAnimated:YES
                                                            completion:self.dismissBlockSent];

        break;
    case MFMailComposeResultFailed:

        break;
    default:
    {
        [FTAAppIdioms createOkAlertWithTitle:@"Email"
                                     message:@"Sending Failed - Unknown Error"
                              viewController:self
                                         tag:0];
    }
        break;
}
[[self presentingViewController] dismissViewControllerAnimated:YES
                                                    completion:nil];
}

希望你可以看到添加dismiss块允许你设置一个变量(或其他)成功发送的电子邮件,它应该回答你的问题 我个人可能会将结果存储在NSDefault Int或类似的东西中。