iOS 模拟器自定义位置问题

标签 ios swift location ios-simulator

我在使用 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()
        }
    }
}

最佳答案

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

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

关于iOS 模拟器自定义位置问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27503629/

相关文章:

java - 如何确定地理点(纬度,经度对)是否位于连接两个地理点的线上

javascript - Angular 位置重定向仍然运行当前 Controller ,如何?

ios - 来自 google api 的所有国家和美国各州

ios - Swift:如何防止两个表格单元格同时被滑动?

ios - 在异步调用中索引到数组时出现问题,Swift

ios - swift 处理位置权限

ios - 如何删除空 UITextView 上的空白文本容器

ios - 如何从 iOS 开始显示自定义菜单项?

android - 应用程序关闭时从 BLE 设备向手机推送通知或 Intent

swift - CAKeyframeAnimation doesNotRecognizeSelector