在使用Socket.IO时禁用应用传输安全



我正在尝试连接到Socket。IO服务器。我正在使用swift库

https://github.com/socketio/socket.io-client-swift

下面是我的示例代码

NSString *url = [NSString stringWithFormat:@"myIP:443];

    self.socket = [[SocketIOClient alloc] initWithSocketURL:url opts:nil];
    [socket on:@"connect" callback:^(NSArray* data, SocketAckEmitter* ack) {
        NSLog(@"socket connected");
    }];
    [socket on:@"currentAmount" callback:^(NSArray* data, SocketAckEmitter* ack) {
        double cur = [[data objectAtIndex:0] floatValue];
        [socket emitWithAck:@"canUpdate" withItems:@[@(cur)]](0, ^(NSArray* data) {
            [socket emit:@"update" withItems:@[@{@"amount": @(cur + 2.50)}]];
        });
        [ack with:@[@"Got your currentAmount, ", @"dude"]];
    }];
    [socket connect];

这是我在我的plist中使用的

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSExceptionDomains</key>
        <dict>
            <key>xx.xx.xxx.xxx</key>
            <dict>
                <key>NSIncludesSubdomains</key>
                <true/>
                <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSTemporaryExceptionMinimumTLSVersion</key>
                <string>TLSv1.1</string>
            </dict>
        </dict>
    </dict>

我仍然得到

App Transport Security已经阻止了一个明文HTTP (http://))资源负载,因为它是不安全的。临时异常可以通过应用的Info配置。plist文件。

然后它可能会尝试连接到API与另一个地址,你没有在info.plist文件中提供。
作为临时的事情,您可以这样做,以消除传输安全错误:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

最新更新