Solved

getting current time from server on iphone

Posted on 2011-09-15
7
666 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 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Suggested Solutions

This article describes in detail how to set up the iPad in the Enterprise using iPCU aka iPhone Configuration Utility.  This could also be used for the iPhone although I have not detailed out any differences. Preparation as an iPad Administrator:…
In this article we will discuss some EI Capitan Mail app issues and provide some manual process to resolve them.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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