Alternatives to NSHost in iPhone app

前端 未结 3 1623
广开言路
广开言路 2020-12-09 23:54

I\'m currently using this code

    NSHost *host = [NSHost hostWithAddress:hostname];
if (host == nil) {
    host = [NSHost hostWithName:hostname];
    if (ho         


        
相关标签:
3条回答
  • 2020-12-10 00:12

    http://developer.apple.com/iphone/library/qa/qa2009/qa1652.html

    Got a great little answer through the Developer Support system, this worked perfectly.

    0 讨论(0)
  • 2020-12-10 00:15

    Look at this: http://blog.zachwaugh.com/post/309927273/programmatically-retrieving-ip-address-of-iphone

    0 讨论(0)
  • You can use CFHost to achieve the same. On the top of the CFHost Reference is a cookbook recipe for making the lookup.

    The following code does very, very basic synchronous resolution (as yours above would with NSHost). Note that you don't want to do this since it can render your app unresponsive because it doesn't return until it's resolved or the timeout hits.

    Use asynchronous lookup instead (CFHostSetClient and CFHostScheduleWithRunLoop as described in the CFHost documentation above). Also, depending on what you're planning to do, you may want to look into using the reachability APIs. Check out the WWDC sessions on networking available on the iPhone developer website.

    Boolean result;
    CFHostRef hostRef;
    CFArrayRef addresses;
    NSString *hostname = @"www.apple.com";
    hostRef = CFHostCreateWithName(kCFAllocatorDefault, (CFStringRef)hostname);
    if (hostRef) {
        result = CFHostStartInfoResolution(hostRef, kCFHostAddresses, NULL); // pass an error instead of NULL here to find out why it failed
        if (result == TRUE) {
            addresses = CFHostGetAddressing(hostRef, &result);
        }
    }
    if (result == TRUE) {
        NSLog(@"Resolved");
    } else {
        NSLog(@"Not resolved");
    }
    
    // Don't forget to release hostRef when you're done with it
    
    0 讨论(0)
提交回复
热议问题