2017-10-04 62 views
0

我有我已经在泊坞窗容器中的远程机器上运行(它实际上是一个小盒子的旁边我的Mac,但无论如何...)一SpringBoot应用。我还有一个实例作为SpringBoot应用程序在我的Mac上本地使用bootRun运行。这些实例没有以任何方式连接。我有一个小型的iOS测试应用程序,用于接受推送通知,使REST调用通过此SB应用程序公开的端点。我有一个Xcode安装应用程序的物理iPhone。NSURLConnection的无法连接到远程IP

如果我指向用我的Mac的IP地址,我的本地运行bootRun实例的iOS应用REST调用,呼叫工作正常,寄存器,一切都很好。但是,如果我将iOS REST调用指向运行Docker实例的远程计算机,则调用将失败。但是,如果我从终端执行curl命令,并使用相同的URL和有效内容,则它可以正常工作。所以没有防火墙问题或类似的东西。报告的错误是:

2017-10-04 14:16:18.907472+0100 IosNotificationApp[563:28094] TIC TCP Conn Failed [1:0x1c416a080]: 1:61 Err(61) 
2017-10-04 14:16:18.907716+0100 IosNotificationApp[563:28094] Task <7111CDD1-CB81-410E-B928-AE46E6964184>.<0> HTTP load failed (error code: -1004 [1:61]) 
2017-10-04 14:16:18.908426+0100 IosNotificationApp[563:28093] NSURLConnection finished with error - code -1004 
2017-10-04 14:16:18.918332+0100 IosNotificationApp[563:28048] Received response: (null), data: (null) 
2017-10-04 14:16:18.918822+0100 IosNotificationApp[563:28048] Failed to register.: Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo={NSUnderlyingError=0x1c0252330 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 "Could not connect to the server." UserInfo={NSErrorFailingURLStringKey=http://111.222.3.4:1234/ios-notification-server/register, NSErrorFailingURLKey=http://111.222.3.4:1234/ios-notification-server/register, _kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1, NSLocalizedDescription=Could not connect to the server.}}, NSErrorFailingURLStringKey=http://111.222.3.4:1234/ios-notification-server/register, NSErrorFailingURLKey=http://111.222.3.4:1234/ios-notification-server/register, _kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=61, NSLocalizedDescription=Could not connect to the server.} 

这里是我的didRegisterForRemoteNotificationsWithDeviceToken功能:远程服务器的身份验证失败或任何在

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken { 

    NSString *newToken = [deviceToken description]; 
    newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]]; 
    newToken = [newToken stringByReplacingOccurrencesOfString:@" " withString:@""]; 
    NSLog(@"Device token is: %@", newToken); 

    NSString *urlString = [NSString stringWithFormat:@"http://111.222.3.4:1234/ios-notification-server/register"]; 
    NSString *body =[NSString stringWithFormat:@"{\"foo\":\"bar\",\"deviceToken\":\"%@\"}", newToken]; 

    NSURL *url = [NSURL URLWithString:urlString]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 

    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
    [request setValue:@"Bearer custom.token" forHTTPHeaderField:@"Authorization"]; 
    [request setHTTPMethod:@"POST"]; 
    [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]]; 

    NSURLSession *session = [NSURLSession sharedSession]; 
    [[session dataTaskWithURL:[NSURL URLWithString:urlString] 
      completionHandler:^(NSData *data, 
           NSURLResponse *response, 
           NSError *connectionError) { 

       NSLog(@"Received response: %@, data: %@", response, data); 

       if (data.length > 0 && connectionError == nil) { 
        NSLog(@"Successfully registered"); 
       } else { 
        NSLog(@"Failed to register.: %@", connectionError); 
       } 
      }] resume]; 
} 

没有注册,它只是似乎没有被发出的呼叫所有。有没有人知道为什么Xcode会在调用不是本机分配的IP时注意到这一点? Xcode中是否有内置的内容可以帮助我诊断正在发生的事情?

+0

-1004是'NSURLErrorCannotConnectToHost'。 – Rob

+0

而只是让我们明白了,你是说你可以'从Mac到这个盒子curl',但iOS设备无法连接所有(虽然它是WiFi和可连接到Mac)? – Rob

+0

不相关的,我建议退休'NSURLConnection'并使用'NSURLSession'来代替。它就像你的'sendAsynchronousRequest'一样简单。 'NSURLConnection'已弃用。 – Rob

回答

0

对于物理设备,去System Preferences -> Sharing。点击Internet Sharing,启用To computers using: iPhone USB

为iPad,我想你应该启用iPad USB