Solved

getting current time from server on iphone

Posted on 2011-09-15
7
664 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface   In the first 100 days of the Macintosh release in January 1984, Apple sold 72,000 of the computers. Relatively cheap, easy-to-use personal computer with a graphical operating system. I've never seen this model. I did see this one:   …
There is a security feature on iOS devices that is nearly impenetrable when it has been activated.  This article will provide some possible solutions as well as necessary steps to take to ensure you do not end up with a locked device.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

831 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