Search code examples
iosobjective-cmapkitmkoverlay

iOS 9 MKTileOverlay not working


My app works fine on iOS 8, but when I tried to build it on Xcode 7, the map doesn't show. I tried it on simulator and real device.

Below is some code.

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSString *url = [[NSUserDefaults standardUserDefaults]
                            stringForKey:@"tileOverlayURL"];
    MKTileOverlay *overlay = [[MKTileOverlay alloc] initWithURLTemplate:url];
    overlay.canReplaceMapContent = YES;
    [self.mapView addOverlay:overlay level:MKOverlayLevelAboveLabels];
}

#pragma mark - MKMapViewDelegate

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView
            rendererForOverlay:(id <MKOverlay>)overlay
{
    if ([overlay isKindOfClass:[MKTileOverlay class]]) {
        return [[MKTileOverlayRenderer alloc] initWithTileOverlay:overlay];
    }
    
    return nil;
}


Solution

  • I had the same problem. IOS 9 by default does not allow connecting to non secured URLs. The map servers I was using were non secured, so IOS 9 prevented the NSURLRequest that my code invoked in MkTileOverlay from talking to the map servers.

    You can tell IOS 9 to allow access to these non secured servers. You must add an NSAppTransportSecurity section to your IOS project's info.plist file and specify an NSExceptionDomain for each non secure map server that you talk to. Here is an example showing 2 of the map servers that I use.

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSExceptionDomains</key>
        <dict>
            <key>nationalmap.gov</key>
            <dict>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSIncludesSubdomains</key>
                <true/>
            </dict>
            <key>opencyclemap.org</key>
            <dict>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSIncludesSubdomains</key>
                <true/>
            </dict>
        </dict>
    </dict>