美文网首页iOS开发
iOS获取北京时间

iOS获取北京时间

作者: Jesscia_Liu | 来源:发表于2017-04-10 16:05 被阅读648次
    • 参照 http://www.jianshu.com/p/ffc758b8eb0e 并对其做了更新

    • [需求] 如果当前系统时间和北京时间相差大于1小时则提示用户去设置时间

    • 使用最新NSURLSession

    • 在application: didFinishLaunchingWithOptions:中调用

    • 注意 弹框需要切回主线程,否则程序会卡在启动页

    - (void)checkSystemTimeInterval{
        NSString *urlString = @"https://m.baidu.com";
        urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setURL:[NSURL URLWithString: urlString]];
        [request setCachePolicy:NSURLRequestReloadIgnoringCacheData];
        [request setTimeoutInterval: 60];
        [request setHTTPShouldHandleCookies:FALSE];
        [request setHTTPMethod:@"GET"];
        
        NSURLSession *session = [NSURLSession sharedSession];
        NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            NSHTTPURLResponse *httpResponse=(NSHTTPURLResponse *)response;
            if ([response respondsToSelector:@selector(allHeaderFields)]) {
                NSDictionary *dic=[httpResponse allHeaderFields];
                //NSLog(@"dic:%@",dic);
                NSString *date = [dic objectForKey:@"Date"];
                date = [date substringFromIndex:5];
                date = [date substringToIndex:[date length]-4];
                NSDateFormatter *dMatter = [[NSDateFormatter alloc] init];
                dMatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
                [dMatter setDateFormat:@"dd MMM yyyy HH:mm:ss"];
                NSDate *netDate = [[dMatter dateFromString:date] dateByAddingTimeInterval:60*60*8];
                
                NSTimeZone *zone = [NSTimeZone systemTimeZone];
                NSInteger interval = [zone secondsFromGMTForDate: netDate];
                NSDate *netLocalDate = [netDate  dateByAddingTimeInterval:interval];
                NSLog(@"netLocalDate=%@",netLocalDate);
                
                NSDate *localDate = [NSDate mylocalDate];
                int timeout =  [localDate timeIntervalSinceDate:netLocalDate];
                NSLog(@"timeout=%zd,localDate=%@,netDate=%@",timeout,localDate,netLocalDate);
                
                if (abs(timeout)>60*60) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        UIViewController *currentVc = [(UINavigationController *)[[UIApplication sharedApplication].windows firstObject].rootViewController visibleViewController];
                        UIView *currentView = currentVc.view;
                        //获取到当前显示View,可弹框提示
                        dispatch_async(dispatch_get_main_queue(), ^{  
                            //对UI操作要切回主线程
                        });
                    });
                }
            }
        }];
        [dataTask resume];
    }
    
    • 跳转去设置系统时间的code(iOS8.0+)

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=General&path=DATE_AND_TIME"]];
    
    • NSDate扩展
    + (NSDate *)mylocalDate
    {
        NSDate *date = [NSDate date];
        
        NSTimeZone *zone = [NSTimeZone systemTimeZone];
        
        NSInteger interval = [zone secondsFromGMTForDate: date];
        
        NSDate *localeDate = [date  dateByAddingTimeInterval: interval];
        
        return localeDate;
    }
    

    相关文章

      网友评论

        本文标题:iOS获取北京时间

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