ReachabilityWithAddress错误,给它一个ip地址

时间:2012-02-22 11:10:35

标签: iphone ip-address connectivity

嘿我试着看看我是否可以连接到IP地址。

我的代码atm:

#import "ViewController.h"
#import "SystemConfiguration/SystemConfiguration.h"

@implementation ViewController
struct sockaddr_in ;

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

Reachability *d = [Reachability reachabilityWithAddress:const struct sockaddr_in     ???????];
NetworkStatus internetStatus = [d currentReachabilityStatus];
//NetworkStatus internetStatus = [d currentReachabilityStatus];
if ((internetStatus != ReachableViaWiFi) && (internetStatus!= ReachableViaWWAN)){
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No internet" message:@"No internet" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
   [alert release];
}
else {
    UIAlertView *notify = [[UIAlertView alloc] initWithTitle:@"Internet" message:@"There is internet!(not)" delegate:self cancelButtonTitle:@"funny" otherButtonTitles:nil];
    [notify show];
    [notify release];  

    }
}

有人可以告诉我如何使这项工作?

我不知道如何在那里插入IP地址......

2 个答案:

答案 0 :(得分:3)

我在苹果公司提供的Reachability样本中测试了这个样本,希望你能得到这个想法。

//Change the host name here to change the server your monitoring
remoteHostLabel.text = [NSString stringWithFormat: @"Remote Host: %@", @"www.apple.com"];
//commented this line in the applicationDidFinishLaunching of ReachabilityAppDelegate.m file
//hostReach = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
 struct sockaddr_in tAddr;
tAddr.sin_len = 16;
tAddr.sin_port = htons(80);
struct in_addr  address;
address.s_addr = htons(0x4a7de048);
tAddr.sin_family = AF_INET;
 //http://74.125.224.72/ this ip adress is for google
 //4a7de048 **updated** Hexadecimal representation of IP address 74.125.224.72

hostReach = [[Reachability reachabilityWithAddress:&tAddr] retain];

答案 1 :(得分:0)

此代码适用于我:

    struct sockaddr_in localWifiAddress;
    bzero(&localWifiAddress, sizeof(localWifiAddress));
    localWifiAddress.sin_len = sizeof(localWifiAddress);
    localWifiAddress.sin_family = AF_INET;
    localWifiAddress.sin_addr.s_addr = htonl(0x0A0A0A7B); // hex representation of your local IP

    Reachability *reachability = [Reachability reachabilityWithAddress:&localWifiAddress];    
    reachability.key = kLocalWiFiConnection;
    return [reachability isReachable];