简体   繁体   English

具有自签名SSL证书的iOS MKTileOverlay

[英]iOS MKTileOverlay with self signed SSL certificate

I operate my own tileserver for maps. 我为地图操作自己的tileserver。 This server is accessible via HTTPS with an self signed certificate. 该服务器可通过带有自签名证书的HTTPS访问。 Is there a chance to use MKTileOverlay 有机会使用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];

wiht a self-signed-certificate. 带有自签名证书。 I receive in the XCode log window unfortunately just an error message that the certificate is invalid. 不幸的是,我在XCode日志窗口中收到一条错误消息,指出证书无效。

For direct NSURLConnection requests I can use the solution as decribed eg here: http://www.cocoanetics.com/2010/12/nsurlconnection-with-self-signed-certificates/ 对于直接的NSURLConnection请求,我可以使用此处所述的解决方案,例如: http : //www.cocoanetics.com/2010/12/nsurlconnection-with-self-signed-certificates/

But this does not work for my customized MKTileOverlay class. 但这不适用于我自定义的MKTileOverlay类。

Has anyone an idea if this is possible? 有谁知道这是否可能?

EDIT 21st August 2015 编辑2015年8月21日

I believe I have to override the MKTileOverlay to something like this: 我相信我必须重写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???
}

Has anyone an idea how too solve this? 有谁知道如何解决这个问题?

I was able to solve it so: 我能够解决它:

- (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];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM