2014-09-26 30 views
0

我想在我的應用程序中實現一項功能,即如果用戶通過電子郵件或帶5人以上的電子郵件共享應用程序鏈接,他/她可以解鎖完整版本的應用程序。我已經看到了應用程序使用的這種方法。 (看起來有可能與facebook和twitter分享使用Facebook標記和twitter提到)計算構成的郵件/郵件的收件人數量

所以,我的問題是,有沒有一種方法來實現這個邏輯,而不使用webservice或任何其他服務器端方法?

有沒有辦法知道使用默認電子郵件/消息編輯器發送的特定電子郵件或文本消息的收件人數量?

+0

嘿@sajaz,下面的答案是如何不是你在找什麼? – 2014-10-03 06:34:40

回答

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塊允許你設置一個變量(或任何)成功的電子郵件發送應該回答你的問題 親自我你ld可能會將結果存儲在NSDefault Int或類似的東西中。

相關問題