Solved

getting current time from server on iphone

Posted on 2011-09-15
7
663 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

CocoaPods is the best way to manage library dependencies in iOS and OS X projects. By using cocoa pods there is no need of downloading the code from github and copying to your project. There are plenty of open source libraries now available with C…
Set up iPhone and iPad email signatures to always send in high-quality HTML with this step-by step guide.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

919 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

13 Experts available now in Live!

Get 1:1 Help Now