首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有自签名SSL证书的iOS MKTileOverlay

具有自签名SSL证书的iOS MKTileOverlay
EN

Stack Overflow用户
提问于 2015-08-21 04:22:02
回答 1查看 86关注 0票数 0

我有自己的地图分割器。可以使用自签名证书通过HTTPS访问此服务器。有机会使用MKTileOverlay吗?

代码语言:javascript
复制
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重写为下面这样的内容:

代码语言:javascript
复制
- (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???
}

有没有人知道怎么解决这个问题?

EN

回答 1

Stack Overflow用户

发布于 2015-08-23 03:32:38

我可以这样解决这个问题:

代码语言:javascript
复制
- (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];
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32127652

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档