以字符串形式从PHP获取时间不起作用

以字符串形式从PHP获取时间不起作用,php,objective-c,nsurlconnection,Php,Objective C,Nsurlconnection,因为我不想使用Xcode的NSDATE函数,所以我尝试用一个php脚本覆盖它……我在这里找到了解决方案,但它不起作用或者我无法让它起作用 代码如下:  - (NSDate *) CurrentDate { if ([self hasInternetConnectivity]) // this tests Internet connectivity based off Apple's Reachability sample code   {  NSURL * s

因为我不想使用Xcode的NSDATE函数,所以我尝试用一个php脚本覆盖它……我在这里找到了解决方案,但它不起作用或者我无法让它起作用

代码如下:

 - (NSDate *) CurrentDate
 {
     if ([self hasInternetConnectivity]) // this tests Internet connectivity based off Apple's Reachability sample code
     { 
         NSURL * scriptUrl = [NSURL URLWithString: @"http://<yoursite>.com/<the 2 line php script>.php"];
         NSData * data = [NSData dataWithContentsOfURL: scriptUrl];

         if (data! = nil) {
              NSString * tempString = [NSString stringWithUTF8String: [data bytes]];
              NSDate * currDate = [NSDate dateWithTimeIntervalSince1970: [tempString doubleValue]];
              NSLog (@"String returned from the site is:% @ and date is:% @", tempString, [currDate description]);
              return currDate;
         } else {
              NSLog (@ "nsdata download failed");
              return [NSDate date];
         }
    } else {
        NSLog (@ "InternetConnectivity failed");
        return [NSDate date];
    }
}
-(NSDate*)当前日期
{
if([self hasInternetConnectivity])//此测试基于苹果的可访问性示例代码进行互联网连接测试
    { 
NSURL*脚本URL=[NSURL URLWithString:@”http://.com/.php"];
NSData*data=[NSData datawithcontentsofull:scriptUrl];
如果(数据!=nil){
NSString*tempString=[NSString stringWithUTF8String:[数据字节]];
NSDate*currDate=[NSDate DATE WithTimeIntervalences1970:[tempString doubleValue]];
NSLog(@“从站点返回的字符串为:%@且日期为:%@”,tempString,[currDate description]);
返回日期;
}否则{
NSLog(@“nsdata下载失败”);
返回[日期];
}
}否则{
NSLog(@“Internet连接失败”);
返回[日期];
}
}
无论我做什么,它都会返回错误,因为hasInternetConnectivity中不存在@interface等

我刚刚将代码粘贴到NAME.m文件中,但它不想工作

我试图在文本字段中获取请求的日期(php时间应该返回20:10:40)

它只是一个字符串…当有人按下按钮,它应该得到服务器时间,并把它放在文本字段


我做错了什么?

我就是这么做的,它工作正常,有点泄漏,但不要担心它太小了:

    NSURL * scriptUrl = [NSURL URLWithString: @"http://www.YOURWEBSITE.com/TIME.php"];
    NSData * data = [NSData dataWithContentsOfURL: scriptUrl];
    serverTime = [[NSMutableString alloc] initWithData:data encoding:NSASCIIStringEncoding];
 NSLog(@"servertime is: %@",servertime);
这是phpfile的一个简单示例:

<?php
$servertime = date("H:i:s");
$string = (string) $servertime;
echo $string;
?>


您的
@界面中似乎不存在
hasInternetConnectivity
…我得到的信息是:“ViewController”没有可见的@interface声明选择器“hasInternetConnectivity”复制hasInternetConnectivity的实现,您从中获得了上面的代码这是唯一给出的“代码”。。。没什么了。“它泄漏了一点,但不要担心…”,不,你应该担心它,当你编写代码时,不应该有泄漏,任何泄漏都是不好的。如果代码中存在漏洞,则编码不好。解决代码中的漏洞并不难。@Popeye也许如果你知道的更好,你可以回答这个问题如果你的答案对你有用,那么很好,我只是说,如果它有漏洞,那么就解决它。解决它不是一个大问题,但添加像上面那样的注释只会得到这样的注释。如果您使用的是
ARC
,则没有泄漏,但我假设您不是因为注释而泄漏的,所以只需添加您的版本,而不是注释。若这对你们提高分数有效,这将是你们应该接受的答案之一。我只是想提出建设性的建议,并不想冒犯别人。有一个好的分数将有助于在未来的问题。