TimWhiting
TimWhiting

Reputation: 2497

iOS Simulator Custom Location issues

I am having issues with the iOS simulator and specifically the custom location setting for the iPhone. When I run the app the first time the simulator is opened it finds the location of the user without issues, however if I then change the custom location, and run the app again it gives the same location as the first time, despite having changed the custom location. If instead I set the Debug> Location > none in the simulator, and change the location in Product > Schemes > Edit Schemes in xCode itself, I have no issues. However every time I change the location this way I have to first set the location to none in the simulator. Is it a problem with my code, or just a quirk of the simulator that I wouldn't find with a real iPhone?

import UIKit
import CoreLocation
import MapKit
var userLocationCity : String!
var userLocationDate : String!
var safeUsername : String!
class TinderViewController: UIViewController, CLLocationManagerDelegate    {

override func viewDidLoad() {
    super.viewDidLoad()

    PFGeoPoint.geoPointForCurrentLocationInBackground { (geopoint: PFGeoPoint!, error: NSError!) -> Void in

        if error == nil {
            println(geopoint)

            var longitude :CLLocationDegrees = geopoint.longitude
            var latitude :CLLocationDegrees = geopoint.latitude

            var location = CLLocation(latitude: latitude, longitude: longitude) //changed!!!
            println(location)
            var formatter: NSDateFormatter = NSDateFormatter()
            formatter.dateFormat = "dd-MM-yyyy"
            let stringDate: String = formatter.stringFromDate(NSDate())
            userLocationDate = stringDate

            println(userLocationDate)

            CLGeocoder().reverseGeocodeLocation(location, completionHandler: {(placemarks, error) -> Void in

                if error != nil {
                    println("Reverse geocoder failed with error" + error.localizedDescription)
                    return
                }

                if placemarks.count > 0 {
                    println(userLocationCity)
                    let pm = placemarks[0] as CLPlacemark
                    println(pm.locality)

                    println(userLocationCity)
                    userLocationCity = pm.locality
                    println(userLocationCity)
                    user["location"] = userLocationCity
                    user.save()

                    let string1 = PFUser.currentUser().objectId
                    let string2 = "ID_"
                    safeUsername = string2 + string1

                    var locate = PFObject(className: safeUsername)
                    locate.setObject(userLocationCity, forKey: "location")
                    locate.setObject(userLocationDate, forKey: "date")
                    locate.saveInBackgroundWithBlock {
                        (success: Bool!, error: NSError!) -> Void in

                        if success == true {
                            println("Score created with ID: \(locate.objectId)")
                        } else {
                            println(error)
                        }                       
                    }
                }
                else {
                    println("Problem with the data received from geocoder")
                }
            })

           // user["location"] = geopoint
           // user.save()
        }
    }
}

Upvotes: 1

Views: 1210

Answers (1)

cmyr
cmyr

Reputation: 2275

Yes, it sounds like the issue is that you are using two different methods to simulate location. You should choose either to simulate location via schemes or via the debug menu in XCode, but not through both. It sounds like you're doing both, and the setting in the debug menu is overriding the setting in your scheme.

I would strongly advise you, however, to test any location based code on an actual device. Most of the problems that you will find with location services will not appear on the simulator; you really need to deal with the actual peculiarities of real-world GPS hardware.

Upvotes: 1

Related Questions