- (voID)vIEwDIDLoad{ [super vIEwDIDLoad]; if(![self connected]) { // not connected NSLog(@"The internet is down"); UIAlertVIEw *connectionError = [[UIAlertVIEw alloc] initWithTitle:@"Connection Error" message:@"There is no Internet Connection" delegate:self cancelbuttonTitle:@"Retry" otherbuttonTitles:nil,nil]; [connectionError show]; [self vIEwDIDLoad]; } else { NSLog(@"Internet connection established"); UIbutton *btn = [UIbutton buttonWithType:UIbuttonTypeInfoDark]; [btn addTarget:self action:@selector(infobuttonClicked:) forControlEvents:UIControlEventtouchUpInsIDe]; self.navigationItem.rightbarbuttonItem = [[UIbarbuttonItem alloc] initWithCustomVIEw:btn]; [self start]; }}解决方法 你应该如何使用 Reachability?
>始终先尝试连接.
>如果请求失败,可达性将告诉您原因.
>如果网络出现,Reachability将通知您.然后重试连接.
要接收通知,请注册通知,然后从Apple启动可访问性类:
@implementation AppDelegate { Reachability *_reachability;}- (BOol)application:(UIApplication *)application dIDFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ [[NSNotificationCenter defaultCenter] addobserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangednotification object: nil]; _reachability = [Reachability reachabilityWithHostname: @"www.apple.com"]; [_reachability startNotifIEr]; // ...}@end
要回答通知:
- (voID) reachabilityChanged: (NSNotification *)notification { Reachability *reach = [notification object]; if( [reach isKindOfClass: [Reachability class]]) { } NetworkStatus status = [reach currentReachabilityStatus]; NSLog(@"change to %d",status); // 0=no network,1=wifi,2=wan}
如果您更喜欢使用块,请使用KSReachability.
总结以上是内存溢出为你收集整理的xcode – 如何重复可达性测试直到它工作全部内容,希望文章能够帮你解决xcode – 如何重复可达性测试直到它工作所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)