How can I clear MKMapView's cache of map tiles

2019-01-25 12:26发布

I am working on an MKMapView based iPhone / iPad mapping app that overlays my own basic base map to provide for some limited functionality even when users are offline and can not reach Google's map tile server.

This functionality is working - but I am having a hard time testing it after each new build because I can't find a way to flush / reset the iOS map tile cache. The iOS cache even survives a power-cycle and reboot of the iOS device. Anywhere I have zoomed into in the past renders with the tiles in memory from the prior test session.

Is there a way to force iOS to flush its map cache? Right now the only alternative I can think of is to reflash the OS every time I need to do a test.

Thanks!

BTW: This is almost a duplicate of this question, but in that case the issue wasn't testing with an empty cache, but rather freeing up memory. The accepted answer given there focused on the RAM issue, not the same problem that I am having here.

3条回答
爷、活的狠高调
2楼-- · 2019-01-25 13:06

Try setting NSURLConnection cache size to zero before creating any instance of your MKMapView

    NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
[sharedCache release];

This might make your connections stop storing cached data as the cache remaining size will always be insufficient

查看更多
来,给爷笑一个
3楼-- · 2019-01-25 13:09

We have not found a way to clear the MKMapView Cache from within an App, but you can "Reset All Content and Data" on Settings\General\Reset. While this is not ideal it is much faster than "reflashing the OS" in fact it only takes a few minutes. After the reset you also have set up the device and reload the app, but this also takes another few minutes.

查看更多
我想做一个坏孩纸
4楼-- · 2019-01-25 13:11

I stumbled across this question, and it's certainly an interesting problem. Clearly, the MKMapView is saving the tiles to disk somewhere (because it survives a reboot). I wonder if a potential solution would be to 1) locate where cached tiles are being saved, and then 2) programatically delete that cache when your app starts. This would require a jailbroken device to give you file access outside your application sandbox. Obviously unsuitable for distribution, but for testing would presumably be acceptable.

That said, when I had this problem I ended up just implementing my own tiled map view and running a reachability test to determine whether to display the google map or my offline mode.

查看更多
登录 后发表回答