iPhone 应用程序中 NSHost 的替代品 [英] Alternatives to NSHost in iPhone app

查看:27
本文介绍了iPhone 应用程序中 NSHost 的替代品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用此代码

    NSHost *host = [NSHost hostWithAddress:hostname];
if (host == nil) {
    host = [NSHost hostWithName:hostname];
    if (host == nil) {
        [self setMessage:@"Invalid IP address or hostname:"];
        return;
    }
}

为我正在开发的网络应用程序检索我的 IP 地址,但是我知道 NSHost 是一个将被拒绝的私有 API.任何人都可以帮助我处理此代码以在不使用 NSHost 的情况下产生相同的结果吗?我不确定从哪里开始.

to retrive my IP Address for a networking app I'm working on, however I'm aware that NSHost is a private API that will be rejected. Can anyone help me with working this code to produce the same results without using NSHost? I'm not really sure where to start.

根据下面看起来几乎完美的建议,我已将此代码添加到我的应用程序中以代替上面的代码

Following suggestions that seem damn near perfect below I've added this code into my app in the place of the code above

    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");
}

我已经删除了第 4 行(因为我已经从其他地方获得了这些信息)但是我收到了基于 CFHostRef 未声明的错误.我该如何解决?这似乎是我唯一的大障碍,因为其他错误仅基于此后无法看到 hostRef.从头开始,我也未声明 kCFHostAddresses.

I've removed the 4th line (as I have this information from elsewhere already) but I get errors being based around CFHostRef being undeclared. How would I resolve that? It seems to be my only big hurdle, as other errors are only based upon the lack of being able to see hostRef after that. Scratch that I also get kCFHostAddresses undeclared.

推荐答案

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

通过开发者支持系统得到了一个很好的小答案,效果很好.

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

这篇关于iPhone 应用程序中 NSHost 的替代品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆