开发者

Best way to synchronize iPhone time with the server

I have a client server model where server will send some data which has date parameters and iPhone will need to render it. Server 开发者_如何学Cand iPhone will resides in different timezones. So I need to synchronize times in both. Let say user has done some activity at "07/18/2011/04 /45/EDT" (server time). From the iPhone end I need to convert this date string into a NSDate object.

What is the best way to achieve this.

Thank you


Sorry for the late response, just looked for "NSDate Sync to Server" on google and got here. I got stuck with the same issue and written this nice little category that helps.

It can return an NSDate object synced to any web server with only a single HTTP Request (per-session).

https://github.com/freak4pc/NSDate-ServerDate

If you have any questions feel free to let me know.


If you want time to be the same on server and client, synchronize both with NTP (network time protocol). There is an obj-c ntp client available at http://code.google.com/p/ios-ntp/source/browse/trunk/ntpA.xcodeproj/project.pbxproj?spec=svn25&r=25

As suggested above, aways store / send times as unix timestamps (seconds since the epoch UTC) and convert to timezones as necessary.


You can definitely achieve this in following way:

1) When you fetch data from server first time, then pass the date value as "" (blank). So that, server will identify that, user require all data.

2) In above service, please add "Date" tag in server web-service. So, when you get other data, you can also get the server date. So, store this server date in local.

3) Next time, when you get other data then pass this stored date (which will be in server format and timezone), so that server will give you only those data which are newly added or modified after this data.

4) This way you can handle the date problem. And also there will be no issue with local time or time zone.

Please review above point and let me know in case of any query of difficulty.

I have implement this in most of my application and it's working fine.

For UTC Date, you can use below function:

-(NSString *)getUTCFormateDate
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    [dateFormatter setTimeZone:timeZone];
    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
    //NSLog(@"UTC Date = %@",dateString);

    return dateString;
}

I hope, it will be helpful to you.

Thanks.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜