我有我已經在泊塢窗容器中的遠程機器上運行(它實際上是一個小盒子的旁邊我的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中是否有內置的內容可以幫助我診斷正在發生的事情?
-1004是'NSURLErrorCannotConnectToHost'。 – Rob
而只是讓我們明白了,你是說你可以'從Mac到這個盒子curl',但iOS設備無法連接所有(雖然它是WiFi和可連接到Mac)? – Rob
不相關的,我建議退休'NSURLConnection'並使用'NSURLSession'來代替。它就像你的'sendAsynchronousRequest'一樣簡單。 'NSURLConnection'已棄用。 – Rob