Solved

getting current time from server on iphone

Posted on 2011-09-15
7
662 Views
Last Modified: 2012-05-12
Dear All,

am trying to write application for iphone this application shows current time and date but not from the iphone i need to get it from server for example microsoft windows server.

Best regards
0
Comment
Question by:deniro1974
  • 4
  • 3
7 Comments
 
LVL 13

Accepted Solution

by:
SpeedyApocalypse earned 500 total points
ID: 36545836
On your server, you will need to have a .php file (or something similar that can output data based on your current server time).  From there, you can make a connection to the URL through one of the following:

+[NSData dataWithContentsOfURL:]
-NSURLConnection
-ASIHTTPRequest

All three of these options will retrieve what you have from your server and download them as data onto your iOS application.

Here is an example:

PHP CODE:
<?php 
die(date(DATE_RFC822));
?>

Open in new window


OBJC CODE:
NSString *time_and_date = [[NSString alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.example.com"] encoding:NSUTF8StringEncoding]];
[time_and_date release];

Open in new window

0
 

Author Comment

by:deniro1974
ID: 36555534
can i use this in timer like every 1 sec for example it will not put traffic on the network or it will not make the Iphone or application became slow or something
0
 
LVL 13

Expert Comment

by:SpeedyApocalypse
ID: 36555539
You are downloading the data synchronously, so it will lag the main thread.

You can just transform the data you download into an NSDate object and increment that each second, hence increasing the server time without redownloading.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:deniro1974
ID: 36555573
how we can increment it as i understand i can get the time one time in the application lunch  ans start to increment it every seconds but how i can do this
0
 
LVL 13

Expert Comment

by:SpeedyApocalypse
ID: 36555579
Look into the NSDateFormatter and NSDate class reference.  If you have code issues, post your code and I can assist with it.
0
 

Author Comment

by:deniro1974
ID: 36555688
now i tried the code you give to me and when the application start its crash any idea?
0
 

Author Comment

by:deniro1974
ID: 36555896
thanks very much its working now but i had to use the below code

 
NSData *dataurl =	[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://example.com"]] ;
	
	
					 NSString *ahmad = [[NSString alloc] initWithData:dataurl encoding:NSUTF8StringEncoding];	

Open in new window

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

A lot of new and distinct gadgets are making their appearance every other day. The latest gadget that has wooed the attention of all gadget lovers and non gadget lovers alike is the Smartwatch. This tiny gadget is capable of offering live access to …
iCloud Drive was introduced after iOS 8 was launched last year. This drive is Apple’s online storage device that lets users sync their files and access them from all their Apple devices.   There is a lot of data that is not automatically backed up…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now