【问题标题】:iOS Simulator Custom Location issuesiOS 模拟器自定义位置问题
【发布时间】:2014-12-16 11:30:55
【问题描述】:

我在使用 iOS 模拟器时遇到问题,尤其是 iPhone 的自定义位置设置。当我第一次打开模拟器时运行该应用程序时,它会毫无问题地找到用户的位置,但是如果我随后更改自定义位置并再次运行该应用程序,它会给出与第一次相同的位置,尽管已经更改了自定义位置。相反,如果我在模拟器中设置 Debug> Location > none,并在 xCode 本身中更改 Product > Schemes > Edit Schemes 中的位置,我没有问题。但是每次我以这种方式更改位置时,我必须首先在模拟器中将位置设置为无。是我的代码有问题,还是只是我在真 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()
        }
    }
}

【问题讨论】:

    标签: ios swift location ios-simulator


    【解决方案1】:

    是的,听起来问题在于您使用两种不同的方法来模拟位置。您应该选择通过方案通过 XCode 中的调试菜单来模拟位置,但不能同时通过两者。听起来你两者都做,调试菜单中的设置覆盖了你的方案中的设置。

    强烈建议您在实际设备上测试任何基于位置的代码。您在定位服务中发现的大多数问题都不会出现在模拟器上;您确实需要处理现实世界 GPS 硬件的实际特性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-19
      • 2013-11-10
      • 2020-07-27
      • 1970-01-01
      • 1970-01-01
      • 2015-01-01
      • 1970-01-01
      相关资源
      最近更新 更多