NSURLConnection无法连接到远程IP

时间:2017-10-04 13:46:26

标签: objective-c xcode apple-push-notifications

我有一个SpringBoot应用程序,我在远程计算机上的docker容器中运行(它实际上是我的Mac旁边的一个小盒子,但无论如何......)。我还在Mac上本地使用bootRun作为SpringBoot应用程序运行实例。这些实例没有任何关联。我有一个小的iOS测试应用程序,用于接受推送通知,该通知使REST呼叫到此SB应用程序公开的端点。我有一个物理iPhone连接,Xcode将应用程序安装到。

如果我使用我的Mac的IP地址在我本地运行的bootRun实例上指向iOS应用程序REST调用,则调用正常并且注册,一切都很棒。但是,如果我在运行Docker实例的远程机器上指向iOS调用,则调用将失败。但是,如果我从终端执行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内置了什么可以帮助我诊断正在发生的事情吗?

1 个答案:

答案 0 :(得分:1)

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

对于iPad,我假设你启用iPad USB