How to validate email addresses in Objective-C

Our free API to validate e-mail addresses is really easy to use in Objective-C

 

Validate e-mail in Objective-C with the NSURLSession object


#import <Foundation/Foundation.h>

NSDictionary *headers = @{ @"x-rapidapi-host": @"mailcheck.p.rapidapi.com",
                           @"x-rapidapi-key": @"YOUR-API-KEY" };

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:
                        [NSURL URLWithString:@"https://mailcheck.p.rapidapi.com/?domain=EMAIL-OR-DOMAIN"]
                            cachePolicy:NSURLRequestUseProtocolCachePolicy
                            timeoutInterval:15.0];
[request setHTTPMethod:@"GET"];
[request setAllHTTPHeaderFields:headers];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                    if (error) {
                        NSLog(@"%@", error);
                    } else {
                        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                        NSLog(@"%@", httpResponse);
                    }
                            }];
[dataTask resume];
                    

This will return a JSON-array with information about the domain, and if you should block it or not.

 

Validate e-mail in Objective-C with the Unirest object


NSDictionary *headers = @{@"X-RapidAPI-Host": @"mailcheck.p.rapidapi.com", @"X-RapidAPI-Key": @"YOUR-API-KEY"};
UNIUrlConnection *asyncConnection = [[UNIRest get:^(UNISimpleRequest *request) {
   [request setUrl:@"https://mailcheck.p.rapidapi.com/?domain=EMAIL-OR-DOMAIN"];
   [request setHeaders:headers];
}] asJsonAsync:^(UNIHTTPJsonResponse *response, NSError *error) {
   NSInteger code = response.code;
   NSDictionary *responseHeaders = response.headers;
   UNIJsonNode *body = response.body;
   NSData *rawBody = response.rawBody;
}];
                    

 

You can of course just send it as a regular GET-request to that URL also, in your favorite way, as long as you have the API-key in the header.

You will receive a JSON response, telling you if this is a disposable e-mail to block, or if there are other issues with the domain:

 


{
   "valid": true,
   "block": true,
   "disposable": true,
   "domain": "cocovpn.com",
   "text": "Disposable e-mail",
   "reason": "Heuristics (1b)",
   "mx_host": "mail56.cocovpn.com",
   "mx_info": "Using MX pointer mail56.cocovpn.com from DNS with priority: 0",
   "mx_ip": "109.236.84.18"
}
               

This will tell you if you should block the domain cocovpn.com or not, and the reason to why the API thinks so (disposable e-mail, etc).

To get started for free, and get an API key, click here!