?
Solved

getting current time from server on iphone

Posted on 2011-09-15
7
Medium Priority
?
668 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 13

Accepted Solution

by:
SpeedyApocalypse earned 2000 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
Simple, centralized multimedia control

Watch and learn to see how ATEN provided an easy and effective way for three jointly-owned pubs to control the 60 televisions located across their three venues utilizing the ATEN Control System, Modular Matrix Switch and HDBaseT extenders.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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:   …
Information security is a multi-billion dollar industry. Just as lucrative is the black market industry which trades stolen identities, credit card numbers and software exploits all over the world. Nothing is hack-proof. The best one can do is make …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

770 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