2017-04-24 22 views
2

得到一個域的CNAME我想解決一個域名,並得到其CNAME和IP信息,如下所示:如何在iOS的

example.com - > cname.com - > IP地址

我搜索了很多,知道如何獲得IP地址,例如由CFHost。但我仍然不知道如何獲取其他信息,如CNAME。

我應該用CFHost還是別的什麼?

+0

爲什麼這個標記爲雨燕? – dfd

+0

我試圖用swift開發我的應用程序。我將刪除此標籤。 – fightinglion

回答

0

您可以使用下面的代碼爲你的目的

+ (void) getCNAME { 
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    DNSServiceRef serviceRef; 
    DNSServiceErrorType error; 
    error = DNSServiceQueryRecord(&serviceRef, 0, 0, "apple.com", kDNSServiceType_CNAME, 
            kDNSServiceClass_IN, queryCallback, NULL); 
    if (error != kDNSServiceErr_NoError){ 
     NSLog(@"DNS Service error"); 
    } 

    DNSServiceProcessResult(serviceRef); 
    DNSServiceRefDeallocate(serviceRef); 
});} 

static void queryCallback(DNSServiceRef sdRef, 
         DNSServiceFlags flags, 
         uint32_t interfaceIndex, 
         DNSServiceErrorType errorCode, 
         const char *fullname, 
         uint16_t rrtype, 
         uint16_t rrclass, 
         uint16_t rdlen, 
         const void *rdata, 
         uint32_t ttl, 
         void *context) { 

if (errorCode == kDNSServiceErr_NoError && rdlen > 1) { 
    NSMutableData *txtData = [NSMutableData dataWithCapacity:rdlen]; 

    for (uint16_t i = 1; i < rdlen; i += 256) { 
     [txtData appendBytes:rdata + i length:MIN(rdlen - i, 255)]; 
    } 

    NSString *theTXT = [[NSString alloc] initWithBytes:txtData.bytes length:txtData.length encoding:NSASCIIStringEncoding]; 
    NSLog(@"CNAME: %@", theTXT); 
}} 
+0

不適用於我。 –