DEV Community

ArshTechPro
ArshTechPro

Posted on

Mocking GPS Location in iOS: A Simple Guide

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:

  1. Run your app on simulator or device
  2. Go to Debug → Simulate Location
  3. 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>
Enter fullscreen mode Exit fullscreen mode

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:

  1. Product → Scheme → Edit Scheme
  2. Click RunOptions tab
  3. Check Allow Location Simulation
  4. 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>
Enter fullscreen mode Exit fullscreen mode

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)")
}
Enter fullscreen mode Exit fullscreen mode

Tip 2: Request Location Explicitly

If location updates aren't coming through, request them explicitly:

locationManager.requestLocation()
Enter fullscreen mode Exit fullscreen mode

Or restart updates:

locationManager.stopUpdatingLocation()
locationManager.startUpdatingLocation()
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Or for background location:

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>We need your location to track your activities</string>
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

Set default location:

Product → Scheme → Edit Scheme → Run → Options → Allow Location Simulation
Enter fullscreen mode Exit fullscreen mode

Stop simulation:

Debug → Simulate Location → Don't Simulate Location
Enter fullscreen mode Exit fullscreen mode

Create GPX file:

File → New → File → GPX File
Enter fullscreen mode Exit fullscreen mode

Summary

Mocking GPS in iOS is straightforward:

  1. Create a GPX file with your test coordinates
  2. Set it as default in your scheme settings
  3. Run your app - location is automatically mocked
  4. 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)

Collapse
 
arshtechpro profile image
ArshTechPro

Run your app on simulator or device
Go to Debug → Simulate Location
Choose a preset location (Apple Campus, London, Tokyo, etc.)