-4

このコードを使用してアプリでネットワーク接続を確認しようとしていますが、常に接続失敗コードが使用されます。これは私のコードです:

- (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 でサーバーに到達できるかどうかを確認するにはどうすればよいですか? このコード機能し、サーバー側のコードは機能しますが、アプリでは機能しません。

4

2 に答える 2

3

URLWithStringスキームを含む RFC 2396 準拠の URL が必要です (例: "http"):

[NSURL URLWithString:@"http://example.com/in-app/net.test"]
于 2013-05-05T17:16:54.240 に答える
1

これを実現するさらに簡単な方法は、Apple の Reachability クラスを使用することです。このクラスは、SystemConfiguration フレームワークを使用して、利用可能なインターネット接続があるかどうかを確認します。

Apple の開発者向け Web サイトへのこれら 2 つのリンクを確認することを強くお勧めします。

于 2013-05-05T20:09:04.783 に答える