简体   繁体   English

iOS模拟器自定义位置问题

[英]iOS Simulator Custom Location issues

I am having issues with the iOS simulator and specifically the custom location setting for the iPhone. 我在iOS模拟器,特别​​是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. 相反,如果我在模拟器中设置“调试”>“位置”>“无”,然后在xCo​​de本身的“产品”>“方案”>“编辑方案”中更改位置,则不会有问题。 However every time I change the location this way I have to first set the location to none in the simulator. 但是, 每次以这种方式更改位置时,都必须先在模拟器中将位置设置为none。 Is it a problem with my code, or just a quirk of the simulator that I wouldn't find with a real iPhone? 是我的代码有问题,还是仅仅是我在真正的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()
        }
    }
}

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. 您应该选择通过方案 XCode中的调试菜单来模拟位置,但不能同时通过两者进行模拟。 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. 您确实需要处理现实世界中GPS硬件的实际特性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM