美文网首页
2018-05-23

2018-05-23

作者: iOS乐乐 | 来源:发表于2018-05-23 08:43 被阅读0次

    //异步
    -(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];
    

    }

    相关文章

      网友评论

          本文标题:2018-05-23

          本文链接:https://www.haomeiwen.com/subject/ssvljftx.html