带有自签名 SSL 证书的 iOS MKTileOverlay

Posted

技术标签:

【中文标题】带有自签名 SSL 证书的 iOS MKTileOverlay【英文标题】:iOS MKTileOverlay with self signed SSL certificate 【发布时间】:2015-08-20 20:22:02 【问题描述】:

我为地图操作自己的 tileserver。该服务器可通过带有自签名证书的 HTTPS 访问。有没有机会使用MKTileOverlay

static NSString * const template = @"https://tile.myserverwithselfsignedcertificate.org/z/x/y.png";

MKTileOverlay *overlay = [[MKTileOverlay alloc] initWithURLTemplate:template];
overlay.canReplaceMapContent = YES;

[self.mapView addOverlay:overlay
                   level:MKOverlayLevelAboveLabels];

带有自签名证书。不幸的是,我在 XCode 日志窗口中收到证书无效的错误消息。

对于直接 NSURLConnection 请求,我可以使用所描述的解决方案,例如这里:http://www.cocoanetics.com/2010/12/nsurlconnection-with-self-signed-certificates/

但这不适用于我自定义的 MKTileOverlay 类。

有人知道这是否可行吗?

2015 年 8 月 21 日编辑

我相信我必须将 MKTileOverlay 覆盖为这样的:

- (void)loadTileAtPath:(MKTileOverlayPath)path result:(void (^)(NSData *data, NSError *error))result

    NSURLRequest *request = [NSURLRequest requestWithURL:[self URLForTilePath:path]
                                                 cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20];
    connectionApi = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];



- (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data

    [myData appendData:data];


- (void)connectionDidFinishLoading:(NSURLConnection *)conn

    // myData includes now the required tile,
    // but how to pass it back to the result
    // block of the loadTileAtPath method???

有人知道如何解决这个问题吗?

【问题讨论】:

如果您使用的是 HTTPS,则 URL 将以 https:// 开头。 你是对的。我只是从另一个来源复制并粘贴了这段代码行。当然我用 HTTPS 试过了。 如果 MKTileOverlay 内部使用了 NSURLRequest,你可以试试this answer提供的解决方案 这与使用 HTTP 没有区别,Apple 可能会拒绝您的二进制文件... 【参考方案1】:

我能够解决它:

- (void)loadTileAtPath:(MKTileOverlayPath)path result:(void (^)(NSData *data, NSError *error))result

        NSURLRequest *request = [NSURLRequest requestWithURL:[self URLForTilePath:path]
                                                 cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20];
        if (!tileDict)
            tileDict = [[NSMutableDictionary alloc] initWithCapacity:100];

        NSURLConnection *connectionApi = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
        NSURL *myURL = [[connectionApi currentRequest] URL];

        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        __block id tileNotification;
        tileNotification = [center addObserverForName:[NSString stringWithFormat:@"receivedTileFromInternet-%@", myURL]
                                                  object:nil
                                                   queue:nil
                                              usingBlock:^(NSNotification *notification)
                               
                                   NSURL *myURL = [notification.userInfo objectForKey:@"tileUrl"];

                                   if ([tileDict objectForKey:myURL])
                                   
                                       [[NSNotificationCenter defaultCenter] removeObserver:tileNotification];
                                       NSData *data = [tileDict objectForKey:myURL];

                                       result(data, nil);
                                   
                                ];
    



- (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data

    NSURL *myURL = [[conn currentRequest] URL];

    if (![tileDict objectForKey:myURL])
    
        NSMutableData *singleTile = [[NSMutableData alloc] initWithData:data];
        [tileDict setObject:singleTile forKey:myURL];
    
    else
    
        [[tileDict objectForKey:myURL] appendData:data];
    


- (void)connectionDidFinishLoading:(NSURLConnection *)conn

    NSURL *myURL = [[conn currentRequest] URL];
    if (![tileDict objectForKey:myURL])
    
        NSLog(@"Tile leer???");
    
    else
    
        NSDictionary* userInfo = [NSDictionary dictionaryWithObjectsAndKeys:myURL, @"tileUrl", nil];
        NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
        [nc postNotificationName:[NSString stringWithFormat:@"receivedTileFromInternet-%@", myURL] object:self userInfo:userInfo];
    

【讨论】:

以上是关于带有自签名 SSL 证书的 iOS MKTileOverlay的主要内容,如果未能解决你的问题,请参考以下文章

带有 Python SSL 套接字的自签名证书

带有自签名 ssl 证书的 prometheus 的 Https

iOS开发HTTPS实现之信任SSL证书和自签名证书

iOS开发HTTPS实现之信任SSL证书和自签名证书

Socket.io + SSL + 自签名 CA 证书在连接时出错

带有自签名证书的 IIS 7.5 SSL 错误“ssl_error_rx_record_too_long”