嗨,我正在为APNS构建我的Xcode项目,我遇到了将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);
}
}
];
}
此错误写在控制台中 error Error Domain = NSURLErrorDomain Code = -1004“无法连接到服务器。
我在我的设备中运行我的应用程序,我在浏览器中使用处理数据运行链接,服务器保存了它! 你能帮我解释为什么会出现这个错误吗? 提前致谢
答案 0 :(得分:1)
使用计算机的IP地址而不是localhost。 当你使用localhost时,你假设请求和连接是在本地进行的,在你的情况下它是在iPhone和服务器之间(它们都有不同的ip)