Sample code for 30+ languages & platforms
Objective-C

Cloudfare DNS over HTTPS

See more Cloudfare Examples

Cloudflare offers a DNS over HTTPS resolver at: https://cloudflare-dns.com/dns-query

This example demonstrates how to do a DNS lookup for a domain using Cloudfare's HTTPS resolver.

Chilkat Objective-C Downloads

Objective-C
#import <CkoHttp.h>
#import <NSString.h>
#import <CkoJsonObject.h>

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

CkoHttp *http = [[CkoHttp alloc] init];

//  Send the following CURL request:   curl -H 'accept: application/dns-json' 'https://cloudflare-dns.com/dns-query?name=chilkat.io&type=A'

http.Accept = @"application/dns-json";

NSString *url = @"https://cloudflare-dns.com/dns-query?name=chilkat.io&type=A";

NSString *jsonResp = [http QuickGetStr: url];
if (http.LastMethodSuccess != YES) {
    NSLog(@"%@",http.LastErrorText);
    return;
}

NSLog(@"%@%d",@"Response Status Code: ",[http.LastStatus intValue]);

CkoJsonObject *json = [[CkoJsonObject alloc] init];
[json Load: jsonResp];
json.EmitCompact = NO;
NSLog(@"%@",[json Emit]);

if ([http.LastStatus intValue] != 200) {
    NSLog(@"%@",@"Failed.");
    return;
}

//  Sample output...
//  (See the parsing code below..)
//  

//  {
//    "Status": 0,
//    "TC": false,
//    "RD": true,
//    "RA": true,
//    "AD": false,
//    "CD": false,
//    "Question": [
//      {
//        "name": "chilkat.io.",
//        "type": 1
//      }
//    ],
//    "Answer": [
//      {
//        "name": "chilkat.io.",
//        "type": 1,
//        "TTL": 900,
//        "data": "52.25.83.238"
//      }
//    ]
//  }

//  Use this online tool to generate parsing code from sample JSON: 
//  Generate Parsing Code from JSON

int Status;
BOOL TC;
BOOL RD;
BOOL RA;
BOOL AD;
BOOL CD;
int i;
int count_i;
NSString *name = 0;
int type;
int TTL;
NSString *data = 0;
NSString *ipAddr = 0;

Status = [[json IntOf: @"Status"] intValue];
TC = [json BoolOf: @"TC"];
RD = [json BoolOf: @"RD"];
RA = [json BoolOf: @"RA"];
AD = [json BoolOf: @"AD"];
CD = [json BoolOf: @"CD"];
i = 0;
count_i = [[json SizeOfArray: @"Question"] intValue];
while (i < count_i) {
    json.I = [NSNumber numberWithInt: i];
    name = [json StringOf: @"Question[i].name"];
    type = [[json IntOf: @"Question[i].type"] intValue];
    i = i + 1;
}

i = 0;
count_i = [[json SizeOfArray: @"Answer"] intValue];
//  The domain name resolves to 1 or more IP addresses..
while (i < count_i) {
    json.I = [NSNumber numberWithInt: i];
    name = [json StringOf: @"Answer[i].name"];
    type = [[json IntOf: @"Answer[i].type"] intValue];
    TTL = [[json IntOf: @"Answer[i].TTL"] intValue];
    ipAddr = [json StringOf: @"Answer[i].data"];

    NSLog(@"%@%@",@"IP Address: ",ipAddr);
    i = i + 1;
}