1

このサイトで利用可能な投稿のいくつかを試しました。しかし、私のiPhoneアプリでは、インターネットに接続されていない場合、エラーは表示されません。ローディングビューを表示するだけです。私はこれに次のコードを使用しました

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
error=nil;
if(error != nil)
{

    UIAlertView * alert;
    if([[error localizedDescription] isEqualToString:@"no Internet connection"])
    {
        alert = [[UIAlertView alloc] initWithTitle:@"No Internet connection" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }

    else
    {
        alert = [[UIAlertView alloc] initWithTitle:@"Connection failed" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
    [alert release];
} 
}

この警告は、インターネットに接続していない場合でも表示されることはありません。誰でも私を助けることができますか?前もって感謝します

4

3 に答える 3

4

Reachability については、Apple のこのサンプル コードを参照してください。

そして、特定の必要なメソッドを実装します。

于 2012-06-26T07:03:58.420 に答える
0

NSUrlConnectionが現在サポートしているエラー コードは次のとおりです。

インターネット接続がない場合のエラー コードは NSURLErrorNotConnectedToInternet = -1009 です。

NSError* エラーが発生したときに、条件でそれを使用できます

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    if(error != nil)
    {
        if ([error code] == NSURLErrorNotConnectedToInternet) {
          //not connected to internet
       }
    }

}
于 2012-06-26T05:43:07.613 に答える
0

このコードを使用...

これを定義する

#define AF_INET     2

これを実装し、

- (BOOL) connectedToNetwork

{

struct sockaddr_in zeroAddress;
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sin_len = sizeof(zeroAddress);
zeroAddress.sin_family = AF_INET;

// Recover reachability flags
SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
SCNetworkReachabilityFlags flags;

BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
CFRelease(defaultRouteReachability);

if (!didRetrieveFlags)
{    
    printf("Error. Could not recover network reachability flags\n");
    return 0;
}   
BOOL isReachable = flags & kSCNetworkFlagsReachable;
BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
return (isReachable && !needsConnection) ? YES : NO;

}

bool を使用して確認し、

    BOOL check = [self connectedToNetwork];

そして、この状態をチェックして、

    if (!check) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning!" message:@"Your internet connection is currently unavailable. Please try to connect later."
                                                   delegate:self cancelButtonTitle:@"Done" otherButtonTitles:nil];
    [alert show];
    [alert release];
}
于 2012-06-26T07:26:27.483 に答える