我尝试使用此代码检查我的应用程序中的网络连接,但它始终使用连接不成功的代码。这是我的代码:
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: @"example.com/in-app/net.test" ]];
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
if([serverOutput isEqualToString:@"internet is working"]){
UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Conection Succesful" message:@"You have succesfully connected to our server. "
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertsuccess show];
[alertsuccess release];
} else {
UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Connection Unsuccesful" message:@"failed to connect"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertsuccess show];
[alertsuccess release];
}
}
再说一次,我如何检查在 Objective-C 中服务器是否可以访问?此代码应该可以工作并且服务器端代码可以工作,但在应用程序中却不能。