开发者

Check whether an email is valid

开发者 https://www.devze.com 2023-03-21 07:07 出处:网络
Am developing an app in iphone in which reports can be sent through email. Have implemented it using MFMailComposeViewController framework. Is there a method to check whether the email is valid or not

Am developing an app in iphone in which reports can be sent through email. Have implemented it using MFMailComposeViewController framework. Is there a method to check whether the email is valid or not. I want to send out the repors only to a valid mail address. And also I want to check th开发者_开发百科e status of the mail whether it is sent successfully or not?


MFMailComposerViewController doesn't have any provision to get the email address from it. You can set it via setToRecipients: method. But you can't access what the user has actually typed inside the address fields.

And, the message will be actually queued in the Mail application's outbox. And the doc clearly states that it doesn't provide any way for you to know whether the mail is actually sent or not.

Another thing is that, if you allow the user to mail something to some mail address, then its his responsiblity to enter a correct mail address. If not, he won't get the mail. Its upto him. Why do you even care?

An undocumented way: It seems this post shows an undocumented way to get access to the email text field by recursively looping through the mail composer's subviews. This method may lead to your app's rejection by Apple, or may cease to work in future if Apple changes the MFMailComposeViewController's implementation.

Reference: A similar SO post here.


I just use this code:

-(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;
}


Use this method to check for validity of email text...

- (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

0

精彩评论

暂无评论...
验证码 换一张
取 消