gpt4 book ai didi

iphone - 在 iPhone 中获取地址信息

转载 作者:太空狗 更新时间:2023-10-30 03:55:07 25 4
gpt4 key购买 nike

我准备了一个小应用程序。当我重新启动 iPhone 3G(使用 3G 互联网连接)并安装此应用程序时,getaddrinfo() 总是返回 EAI_NONAME (8)。我关闭应用程序并运行 Safari,然后运行我的应用程序 - 一切正常。有什么问题?

- (void)viewDidLoad {
[super viewDidLoad];

const char* hostname = "google.com";

struct addrinfo hints, *res;
int retval;

memset (&hints, 0, sizeof (struct addrinfo));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;

retval = getaddrinfo (hostname, NULL, &hints, &res);
if (retval == 0) {
freeaddrinfo (res);
}else if(retval == EAI_NONAME)
{
//noname
}
}

Apple 的回答:

Hello Pavel,

I'm responding regarding your finding that trying to use the BSD getaddrinfo function is problematic when trying to get a connection where only a Wireless Wide Area Network (WWAN) connection is possible (EDGE, 3G). The issue you've encountered is that for battery saving reasons, WWAN will shutdown when network services are not needed. The question then becomes, how to activate WWAN, when network services are required.

The officially supported mechanism to establish a WWAN connection is to use the CFSocketStream API (or dependent API - NSSocket, as well as CFHTTPStream, CFFTPStream, NSURLRequest, and NSURLConnection API's). This means that officially, only TCP is supported. This limitation impacts all UDP and BSD Socket based applications. The use of a BSD Connect call will not trigger the iPhone to enable WWAN. This limitation also applies to all other BSD functions which under a WiFi connection would result in a packet transmission.

However, under the current iPhone OS, once a WWAN connection is established, the use of BSD sockets and CFSocket functions, will keep the WWAN connection alive. This makes it possible for a UDP or BSD Socket based application to establish a WWAN connection by using the CFSocketStream API to establish a connection with a TCP server (including HTTP Web Servers). Once the connection is active, then the program can function as it has in the past. This is what Safari, Mail, and other Apple applications which access network services, uses to establish their connections. For this reason, when you launch Safari, then launch your application network access works. Using Safari causes the iPhone to establish a WWAN connection. When you quit Safari, WWAN does not automatically shutdown - it remains active for a short period of time. If you launch your application, WWAN services are active and your application will cause WWAN to remain active so long as there is network activity.

If your application were to use TCP, for networking, then you could modify your code to use the CFSocketStream or dependent API, for networking. Of course, if you have a whole library of BSD socket functionality, this may not be a reasonable solution for someone who wants to get their application onto the App Store real soon.

....

最佳答案

这里可能发生的是 getaddrinfo() 没有阻止等待 DNS 查找,这可能是一件好事,因为 iPhone 上的 DNS 查找可能需要很长时间。一旦您在别处完成查找,它就会为您提供缓存的答案。

看看CFHost ,这是在 iPhone 上获取此类信息的首选方式。它支持异步回调,让您知道信息何时可用,或者您也可以根据需要进行同步调用。

关于iphone - 在 iPhone 中获取地址信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1238934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com