デバイスがインターネットに接続されているかどうかを確認しようとしています。そうでない場合は、アラートがポップアップし、ユーザーは「再試行」を押して再試行する必要があります。私のコードでは、最初にアラート ビューがポップアップしたときに「再試行」を押してから、2 回目にアラートがポップアップしたときに、1 秒後に自動的に閉じます。しかし、「再試行」を押したときにのみ却下されると思われます。
これが私のコードです:
.h:
#import <UIKit/UIKit.h>
@class Reachability;
@interface ViewController : UIViewController{
Reachability* internetReachable;
Reachability* hostReachable;
UIAlertView *networkAlert;
}
-(void) checkNetworkStatus;
@end
.m:
#import "ViewController.h"
#import "Reachability.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
networkAlert = [[UIAlertView alloc]initWithTitle: @"Unstable Network Connection"
message: @"Unstable network connection."
delegate: self
cancelButtonTitle: nil
otherButtonTitles: @"Try Again", nil];
[self checkNetworkStatus];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) checkNetworkStatus
{
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
NSLog(@"The internet is down.");
[networkAlert show];
break;
}
case ReachableViaWiFi:
{
NSLog(@"The internet is working via WIFI.");
break;
}
case ReachableViaWWAN:
{
NSLog(@"The internet is working via WWAN.");
break;
}
}
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
NSLog(@"user pressed Button Indexed 0");
// Any action can be performed here
[self checkNetworkStatus];
}
}
@end
私の英語で申し訳ありませんが、よろしくお願いします!!!