2018-05-23

//异步
-(void)loadDataFromNetworking
{
[MBProgressHUD showMessage:KLoading toView:self.view];

//接口路径
NSString *path = @"http://ip.taobao.com/service/getIpInfo.php?ip=myip";
//路径-+参数
NSString *pathWithPhoneNum = [NSString stringWithFormat:@"%@",path];

//中文编码
NSString *urlPath = [pathWithPhoneNum stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
//URL
NSURL *phoneURL = [NSURL URLWithString:urlPath];
//请求对象
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:phoneURL];
//请求方式
[request setHTTPMethod:@"GET"];
//网络配置
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
//网络会话
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];

//任务
NSURLSessionDataTask *sessionTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    
    //回到主线程更新UI -> 撤销遮罩
    dispatch_async(dispatch_get_main_queue(), ^
    {
        
        NSString*str=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        DLog(@"%@",str);
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];

        NSDictionary *ipDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

        NSString*country = ipDic[@"data"][@"country"];
        DLog(@"国家:%@",country);
        if (country) {
            
            if ([country isEqualToString:@"美国"])
            {
                NSString *currentTime = @"";
                NSDate *date = [[NSDate alloc] init];
                NSDateFormatter *df=[[NSDateFormatter alloc]init];
                [df setDateFormat:@"yyyyMMdd"];
                currentTime = [df stringFromDate:date];
                NSLog(@"----%d", [currentTime intValue]);
                [USER setObject:currentTime forKey:@"AmericaTime"];
            }
        }
        
    });
    
}];
//开始任务
[sessionTask resume];

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容