简体   繁体   中英

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

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>

I had the same problem with loading tiles in custom overlay. My problem was that I was using "http" requests instead of "https". Requests with "http" are considered to be insecure on iOS9 and are blocked by default. That could be possible issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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