ios 初学笔记 之 检测网络是否可用 并 强制退出APP 方法

/判断网络是否可用方法 及强制退出APP

-(void)panduan{

    //第一步,创建url

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",[GMUserInfo sharedGMUserInfo].mcurl,@"dgapsp.ashx"]];

    //第二步,创建请求

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];

    [request setHTTPMethod:@"POST"];

    NSString *str = [NSString stringWithFormat:@""];//设置URL参数 

    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];

    [request setHTTPBody:data];

    //第三步,连接服务器

    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];

    

}

接收到服务器回应的时候调用此方法

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

{

    NSHTTPURLResponse *res = (NSHTTPURLResponse *)response;

}

接收到服务器传输数据的时候调用,此方法根据数据大小执行若干次

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{


}

数据传完之后调用此方法

-(void)connectionDidFinishLoading:(NSURLConnection *)connection

{

    

}

网络请求过程中,出现任何错误(断网,连接超时等)会进入此方法

-(void)connection:(NSURLConnection *)connection

 didFailWithError:(NSError *)error

{

    NSLog(@"----%@",[error localizedDescription]);

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:errors preferredStyle:UIAlertControllerStyleAlert];


    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {

        //强制退出APP 调用

//        [self animationFinished:@"exitApplication" finished:@0 context:@""];

    }];

    

    [alert addAction:okAction];

    // 弹出对话框

    [self presentViewController:alert animated:true completion:nil];

    

}

//强制退出APP 创建

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

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

        exit(0);

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值