检查电子邮件是否有效

时间:2011-07-17 06:05:29

标签: iphone objective-c ios email mfmailcomposeviewcontroller

正在iphone中开发一个可以通过电子邮件发送报告的应用程序。 使用MFMailComposeViewController框架实现了它。有没有办法检查电子邮件是否有效。我想将报告发送到有效的邮件地址。而且我想检查邮件的状态是否成功发送?

3 个答案:

答案 0 :(得分:3)

MFMailComposerViewController 没有任何条款可以从中获取电子邮件地址。您可以通过 setToRecipients:方法进行设置。但是您无法访问用户在地址字段中实际键入的内容。

并且,该消息实际上将在 Mail 应用程序的发件箱中排队。该文件明确指出,它没有提供任何方式让您知道邮件是否实际发送。

另一件事是,如果您允许用户将某些内容邮寄到某个邮件地址,那么他有责任输入正确的邮件地址。如果没有,他将不会收到邮件。它由他决定。为什么你甚至关心?

未记录的方式:似乎this post通过递归循环邮件编辑器的子视图显示了一种无证方式来访问电子邮件文本字段。此方法可能会导致您的应用被Apple拒绝,或者如果Apple更改 MFMailComposeViewController的实施,可能会在将来停止工作。

参考: A similar SO post here

答案 1 :(得分:2)

我只是使用这段代码:

-(BOOL) validEmail:(NSString *)email {

    //Based on the string below
    //NSString *strEmailMatchstring=@”\\b([a-zA-Z0-9%_.+\\-]+)@([a-zA-Z0-9.\\-]+?\\.[a-zA-Z]{2,6})\\b”;

    //Quick return if @ Or . not in the string
    if([email rangeOfString:@"@"].location==NSNotFound || [email rangeOfString:@"."].location==NSNotFound)
        return NO;

    //Break email address into its components
    NSString *accountName=[email substringToIndex: [email rangeOfString:@"@"].location];
    email=[email substringFromIndex:[email rangeOfString:@"@"].location+1];

    //’.’ not present in substring
    if([email rangeOfString:@"."].location==NSNotFound)
        return NO;
    NSString *domainName=[email substringToIndex:[email rangeOfString:@"."].location];
    NSString *subDomain=[email substringFromIndex:[email rangeOfString:@"."].location+1];

    //username, domainname and subdomain name should not contain the following charters below
    //filter for user name
    NSString *unWantedInUName = @" ~!@#$^&*()={}[]|;’:\"<>,?/`";
    //filter for domain
    NSString *unWantedInDomain = @" ~!@#$%^&*()={}[]|;’:\"<>,+?/`";
    //filter for subdomain 
    NSString *unWantedInSub = @" `~!@#$%^&*()={}[]:\";’<>,?/1234567890";

    //subdomain should not be less that 2 and not greater 6
    if(!(subDomain.length>=2 && subDomain.length<=6)) return NO;

    if([accountName isEqualToString:@""] || [accountName rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:unWantedInUName]].location!=NSNotFound || [domainName isEqualToString:@""] || [domainName rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:unWantedInDomain]].location!=NSNotFound || [subDomain isEqualToString:@""] || [subDomain rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:unWantedInSub]].location!=NSNotFound)
        return NO;

    return YES;
}

答案 2 :(得分:-1)

使用此方法检查电子邮件文本的有效性......

- (BOOL)validateEmail:(NSString *)emailStr {
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:emailStr]; }

https://stackoverflow.com/a/7123957/1463604