检查 iPhone 的网络连接

发布于 2024-10-16 01:08:11 字数 1865 浏览 2 评论 0原文

我正在 iPhone 中开发应用程序,它动态地从网站获取数据,我正在检查 Wifi 和 GPRS 是否已连接,但是当 wifi 无法访问时,应用程序崩溃。

我正在使用这种方法来检查连接。

+ (BOOL) isConnected {  
    // Part 1 - Create Internet socket addr of zero
    struct sockaddr_in zeroAddr;
    bzero(&zeroAddr, sizeof(zeroAddr));
    zeroAddr.sin_len = sizeof(zeroAddr);
    zeroAddr.sin_family = AF_INET;

    // Part 2- Create target in format need by SCNetwork
    SCNetworkReachabilityRef target = 
    SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *) &zeroAddr);

    // Part 3 - Get the flags
    SCNetworkReachabilityFlags flags;
    SCNetworkReachabilityGetFlags(target, &flags);

    // Part 4 - Create output
    NSString *sNetworkReachable;
    if (flags & kSCNetworkFlagsReachable)
        sNetworkReachable = @"YES";
    else
        sNetworkReachable = @"NO";

    NSString *sCellNetwork;
    if (flags & kSCNetworkReachabilityFlagsIsWWAN)
        sCellNetwork = @"YES";
    else
        sCellNetwork = @"NO";


    // Get host entry info for given host
    struct hostent *remoteHostEnt = gethostbyname("google.com");
    if(remoteHostEnt == nil ) return NO;

    // Get address info from host entry
    struct in_addr *remoteInAddr = (struct in_addr *) remoteHostEnt->h_addr_list[0];

    // Convert numeric addr to ASCII string
    char *sRemoteInAddr = inet_ntoa(*remoteInAddr);

    NSString *s = [[NSString alloc]
                 initWithFormat:
                 @"Network Reachable: %@\n"
                 @"Cell Network: %@\n"
                 @"Remote IP: %s\n",
                 sNetworkReachable,
                 sCellNetwork,
                 sRemoteInAddr];


    // Add text
    [sCellNetwork release];
    [sNetworkReachable release];

    NSLog(@"Message:%@",s);

    return [sNetworkReachable isEqualToString:@"YES"];  
} 

谢谢 瓦迪韦卢

I am developing app in iPhone which is fetch the data from website dynamically, I am checking whether Wifi and GPRS are connected but when wifi is not reachable, App crash.

I am using this method to check connection.

+ (BOOL) isConnected {  
    // Part 1 - Create Internet socket addr of zero
    struct sockaddr_in zeroAddr;
    bzero(&zeroAddr, sizeof(zeroAddr));
    zeroAddr.sin_len = sizeof(zeroAddr);
    zeroAddr.sin_family = AF_INET;

    // Part 2- Create target in format need by SCNetwork
    SCNetworkReachabilityRef target = 
    SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *) &zeroAddr);

    // Part 3 - Get the flags
    SCNetworkReachabilityFlags flags;
    SCNetworkReachabilityGetFlags(target, &flags);

    // Part 4 - Create output
    NSString *sNetworkReachable;
    if (flags & kSCNetworkFlagsReachable)
        sNetworkReachable = @"YES";
    else
        sNetworkReachable = @"NO";

    NSString *sCellNetwork;
    if (flags & kSCNetworkReachabilityFlagsIsWWAN)
        sCellNetwork = @"YES";
    else
        sCellNetwork = @"NO";


    // Get host entry info for given host
    struct hostent *remoteHostEnt = gethostbyname("google.com");
    if(remoteHostEnt == nil ) return NO;

    // Get address info from host entry
    struct in_addr *remoteInAddr = (struct in_addr *) remoteHostEnt->h_addr_list[0];

    // Convert numeric addr to ASCII string
    char *sRemoteInAddr = inet_ntoa(*remoteInAddr);

    NSString *s = [[NSString alloc]
                 initWithFormat:
                 @"Network Reachable: %@\n"
                 @"Cell Network: %@\n"
                 @"Remote IP: %s\n",
                 sNetworkReachable,
                 sCellNetwork,
                 sRemoteInAddr];


    // Add text
    [sCellNetwork release];
    [sNetworkReachable release];

    NSLog(@"Message:%@",s);

    return [sNetworkReachable isEqualToString:@"YES"];  
} 

Thanks
Vadivelu

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

虚拟世界 2024-10-23 01:08:11

为什么要搞乱 IP 地址?您可以直接调用 SCNetworkReachabilityCreateWithName()

SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, "google.com");

Why mess with IP addresses? You can call SCNetworkReachabilityCreateWithName() directly instead:

SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, "google.com");
梦行七里 2024-10-23 01:08:11

developer.apple.com 上搜索 Reachability 示例应用程序,

还有开源 NPReachability 类,它执行相同的操作,但使用块作为处理程序。

Search for the Reachability sample application on developer.apple.com

There is also the Open Source NPReachability class on GitHub which does the same thing but uses a block as a handler.

够运 2024-10-23 01:08:11

您可以使用隐藏的 UIWebView 并向其加载网站。然后使用 UIWebView.h 中的方法来检测加载是否成功。

You could use a hidden UIWebView and load a website to it. Then use methods from the UIWebView.h to detect, whether loading was successful or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文