Testing location-based features in iOS apps can be frustrating if you don't know the right approach. Let me show you how to mock GPS locations properly in Xcode.
The Quick Way: Xcode Debug Menu
The simplest method while your app is running:
- Run your app on simulator or device
- Go to Debug → Simulate Location
- Choose a preset location (Apple Campus, London, Tokyo, etc.)
That's it! Your app will receive the new location.
Note: Sometimes the location change doesn't register immediately. If this happens, just relaunch the simulator and the new location will be active.
The Better Way: Using GPX Files
GPX files give you more control and let you set custom locations. Here's how:
Step 1: Create a GPX File
In Xcode: File → New → File → GPX File
Add your coordinates:
<?xml version="1.0"?>
<gpx version="1.1" creator="Xcode">
<wpt lat="37.7749" lon="-122.4194">
<name>San Francisco</name>
</wpt>
</gpx>
That's a single point location. Simple and clean.
Step 2: Use the GPX File
Run your app, then:
Debug → Simulate Location → [Your GPX File Name]
Step 3: Make It Automatic (Optional)
Tired of selecting the location every time? Set a default:
- Product → Scheme → Edit Scheme
- Click Run → Options tab
- Check Allow Location Simulation
- Select your GPX file from the dropdown
Now your app automatically starts with that location every time you run it.
Common Coordinates for Testing
<!-- San Francisco -->
<wpt lat="37.7749" lon="-122.4194">
<name>San Francisco</name>
</wpt>
<!-- New York -->
<wpt lat="40.7128" lon="-74.0060">
<name>New York</name>
</wpt>
<!-- London -->
<wpt lat="51.5074" lon="-0.1278">
<name>London</name>
</wpt>
<!-- Tokyo -->
<wpt lat="35.6762" lon="139.6503">
<name>Tokyo</name>
</wpt>
Quick Tips
Tip 1: Add Debug Logging
Always log location updates to see what's happening:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
print("📍 Location: \(location.coordinate.latitude), \(location.coordinate.longitude)")
}
Tip 2: Request Location Explicitly
If location updates aren't coming through, request them explicitly:
locationManager.requestLocation()
Or restart updates:
locationManager.stopUpdatingLocation()
locationManager.startUpdatingLocation()
Tip 3: Stop Simulation
To stop mocking and use real location:
Debug → Simulate Location → Don't Simulate Location
Tip 4: Simulator Menu
You can also set location directly in the simulator:
Features → Location → Custom Location
Enter your coordinates manually.
Info.plist Requirements
Don't forget to add location permissions:
<key>NSLocationWhenInUseUsageDescription</key>
<string>We need your location to show nearby places</string>
Or for background location:
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>We need your location to track your activities</string>
Physical Device Testing
Location simulation works on physical devices too, but only when:
- Device is connected to Xcode via cable
- App is running from Xcode
The simulated location stays active until you select Don't Simulate Location or disconnect the device.
Quick Reference
Change location while debugging:
Debug → Simulate Location → [Choose Location]
Set default location:
Product → Scheme → Edit Scheme → Run → Options → Allow Location Simulation
Stop simulation:
Debug → Simulate Location → Don't Simulate Location
Create GPX file:
File → New → File → GPX File
Summary
Mocking GPS in iOS is straightforward:
- Create a GPX file with your test coordinates
- Set it as default in your scheme settings
- Run your app - location is automatically mocked
- Add debug logging to verify updates
That's all you need for reliable location testing!
Questions? Drop them in the comments below. Happy testing! 🚀
Top comments (1)
Run your app on simulator or device
Go to Debug → Simulate Location
Choose a preset location (Apple Campus, London, Tokyo, etc.)