基于MapBox SDK的首个iOS应用定位异常求助
Hey there, I see you're running into a common snag when getting started with Mapbox's iOS SDK. The Ghana/Togo border is Mapbox's default fallback location when map tiles fail to load, and the -999 error (which maps to NSURLErrorCancelled) tells us the tile requests are being blocked or cancelled. Let's walk through targeted fixes step by step:
1. Double-Check Your Access Token
- First, confirm the
MGLMapboxAccessTokenin yourInfo.plistis an exact match for the token you generated in the Mapbox dashboard—no extra spaces, typos, or line breaks allowed. - Head to your Mapbox account and verify the token is a public access token (the default type when you create one) and hasn't been restricted by IP or other filters (new tokens usually don't have restrictions, but it's worth double-checking).
2. Fix App Transport Security (ATS) Settings
iOS 9+ enforces ATS by default, which can interfere with Mapbox's tile requests even though they use HTTPS. Here's how to configure it properly:
- Open your
Info.plistand add this configuration (either via the property list editor or raw XML):
<key>NSAppTransportSecurity</key> <dict> <key>NSExceptionDomains</key> <dict> <key>mapbox.com</key> <dict> <key>NSIncludesSubdomains</key> <true/> <key>NSExceptionRequiresForwardSecrecy</key> <false/> </dict> </dict> </dict>
- For quick testing, you can temporarily set
NSAllowsArbitraryLoadstoYESunderNSAppTransportSecurity, but avoid this in production apps.
3. Clear Simulator Cache & Reset the App
The WAL file recovery log in your console suggests corrupted cache data might be causing issues:
- Delete your app from the simulator (long-press the app icon → tap the "X" to delete).
- For a thorough reset, go to
Simulator→Reset Content and Settings...to wipe all simulator data, then rebuild and run your app.
4. Verify CocoaPods Installation & SDK Version
You're using Mapbox-iOS-SDK ~> 3.7, an older 2017-era version that might have compatibility issues with Xcode 9.2:
- Run
pod deintegratein your project directory, delete thePodfile.lockfile, then runpod installagain to ensure a clean installation. - Try upgrading to the latest patch version compatible with Xcode 9.2 (like
~> 3.7.1) to rule out version-specific bugs.
5. Confirm MGLMapView IBOutlet Connection
While your code looks correct, it's worth ruling out a broken connection:
- In Storyboard, right-click your
ViewControllerand check the Connections panel to make sureviewMapBoxis properly linked to theMGLMapViewin your layout. - Add a debug print in
viewDidLoad:print("Map view:", viewMapBox)—if it printsnil, you'll need to re-establish the IBOutlet connection.
Why This Happens
The -999 error means Mapbox's tile requests are being cancelled, usually due to network restrictions (ATS), an invalid access token, or corrupted cache. The fallback to the Ghana/Togo border is Mapbox's way of indicating it couldn't load the requested map tiles.
内容的提问来源于stack exchange,提问作者jingo_man

