iOS MKTileOverlay with self signed SSL certificate

113 Views Asked by At

I operate my own tileserver for maps. This server is accessible via HTTPS with an self signed certificate. Is there a chance to use 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.

For direct NSURLConnection requests I can use the solution as decribed e.g. here: http://www.cocoanetics.com/2010/12/nsurlconnection-with-self-signed-certificates/

But this does not work for my customized MKTileOverlay class.

Has anyone an idea if this is possible?

EDIT 21st August 2015

I believe I have to override the MKTileOverlay to something like this:

- (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?

1

There are 1 best solutions below

0
On

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