/判断网络是否可用方法 及强制退出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);
}
}