Is there a clock in iOS that can be used that cannot be changed by the user

后端 未结 6 1087
长发绾君心
长发绾君心 2020-11-30 02:52

I am trying to design a system where real-time events happen and I want to synchronise them to a clock. [NSDate date] would normally be ok but the user could c

6条回答
  •  温柔的废话
    2020-11-30 03:15

    I am using this in our app

    + (NSDate *) getAWSDate
    {
        NSDate *today = nil; 
       NSString *dateString = nil;
    
        NSString *awsURL = @"http://s3.amazonaws.com";
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:awsURL]];  
        [request setHTTPMethod:@"HEAD"];  
    
        NSHTTPURLResponse *response;  
        [NSURLConnection sendSynchronousRequest:request returningResponse:&response error: NULL];  
    
        if ([response respondsToSelector:@selector(allHeaderFields)]) {  
            dateString = [[response allHeaderFields] objectForKey:@"Date"];
            dateString = [dateString stringByReplacingOccurrencesOfString:@"GMT" withString:@"+0000"];
            NSDateFormatter *df = [[NSDateFormatter alloc] init];  
        df.dateFormat = @"EEE, dd MMM yyyy HH:mm:ss z";
        df.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];  
        df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];  
        today = [df dateFromString:dateString];
        [df release];    
        }           
        return today;
    }
    

提交回复
热议问题