根据网络状态判断是否打开程序

来源:互联网 发布:山下智久退出news 知乎 编辑:程序博客网 时间:2024/04/29 11:04

//

//  AppDelegate+Reachability.h

//  

//

//  Created by Floating_SH on 16/1/20.

//  Copyright © 2016 K_night. All rights reserved.

//


#import "AppDelegate.h"

@class Reachability;

@interface AppDelegate (Reachability)


- (void) judgeNetework;


@end






//

//  AppDelegate+Reachability.m

//  

//

//  Created by Floating_SH on 16/1/20.

//  Copyright © 2016 K_night. All rights reserved.

//


#import "AppDelegate+Reachability.h"

#import "Reachability.h"

@implementation AppDelegate (Reachability)



- (void)judgeNetework{

    

    Reachability *reachability = [ReachabilityreachabilityForInternetConnection];


    if (reachability.currentReachabilityStatus ==NotReachable) {

        UIAlertController *successdController = [UIAlertControlleralertControllerWithTitle:@"提示"message:@"网络无连接,无法信息,请检查网络连接后运行程序"preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction *defultAction = [UIAlertActionactionWithTitle:@"确认"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction *_Nonnull action) {

            NSLog(@"无网络");

            [self exitApplication];

        }];

        [successdController addAction:defultAction];

        [self.window.rootViewControllerpresentViewController:successdController animated:YEScompletion:nil];

    }elseif (reachability.currentReachabilityStatus ==ReachableViaWiFi){

        NSLog(@"当前网络状态为WIFI");

    }else{

        NSLog(@"当前网络为WWAN");

    }

    

    //开启向通知中心发送网络状态改变后的通知

    // [reachability startNotifier];

}


//------------------ 退出程序 ---------------------


- (void)exitApplication {

    

    [UIView beginAnimations:@"exitApplication"context:nil];

    self.tab.tabBar.hidden = YES;

    [UIView setAnimationDuration:0.5];

    [UIView setAnimationDelegate:self];

    [UIView setAnimationTransition:(UIViewAnimationTransitionNone)forView:self.windowcache:NO];

    [UIView setAnimationDidStopSelector:@selector(animationFinished:finished:context:)];

    self.window.bounds =CGRectMake(0,0, 0, 0);

    [UIView commitAnimations];

}


- (void)animationFinished:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {

    

    if ([animationID compare:@"exitApplication"] == 0) {

        exit(0); 

    }

    

}


@end

0 0
原创粉丝点击