2012-06-13 29 views
1

嗨,我建我的Xcode项目APNS,我在发送deviceToken到本地服务器(XAMPP)面临的一个问题,这里是代码:错误将数据发送到服务器

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{ 
NSLog(@"devicetoken= %@",deviceToken); 
NSString *string =[NSString stringWithFormat:@"%@",deviceToken]; 
string=[string substringWithRange:NSMakeRange(1, string.length-2)]; 
string=[string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

    NSString *url=[NSString stringWithFormat:@"http://localhost/addtoken.php?devictoken=%@",string]; 
NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:url]]; 
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *urlResponse, NSData *data, NSError *error) { 
    if (error){ 
     NSLog(@"error %@",error); 
    } 
} 
]; 

}

此错误是写在控制台 错误错误域= NSURLErrorDomain代码= -1004“无法连接到服务器。

我跑我的应用程序在我的设备,运行L在浏览器中用墨水处理数据,服务器将其保存! 你能帮我解答为什么会出现这个错误吗? 在此先感谢

回答

1

使用您的计算机的IP地址,而不是本地主机。 当你使用本地主机时,你认为请求和连接是在本地进行的,你的情况是在iPhone和服务器之间(它们都有不同的ip)

+0

你的意思是http://127.0.1 /? 我试过了,没有回答:( – Norah

+0

它woooooorks 太感谢你了,我被困在这个错误两周前.. 你让我很快乐 – Norah

+0

@Norah allohaidan当你使用localhost,你认为该请求及其连接在本地制作,在你的情况下它是在iPhone和服务器之间(它们都有不同的IP) – self