startMonitoringSignificantLocationChanges不起作用

时间:2017-04-13 10:40:50

标签: ios swift3 location core-location background-fetch

我正在创建一个使用设备位置服务的应用程序。这个服务需要在每次位置发生变化时捕获邮件,它应该在所有情况下都保持运行(后台/前景/被杀),所以我使用 startMonitoringSignificantLocationChanges()方法,但是当我把它发送到后台,它工作了很短的时间然后停止,之后它没有捕获任何位置。我开始移动文档建议的距离以获得位置更新(500米)并且没有任何反应。我能做错什么?注意:在模拟器上它可以很好地工作,但是在物理设备上它不起作用。

Info.plist 文件已启用后台设置

<key>UIBackgroundModes</key>
  <array>
    <string>fetch</string>
    <string>location</string>
  </array>

AppDelegate.swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    if (launchOptions != nil) {
        let locationManager = CLLocationManager()
        locationManager.delegate = ViewController()
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.activityType = CLActivityType.other
        locationManager.allowsBackgroundLocationUpdates = true
        locationManager.startUpdatingLocation()
    }

    return true
}

func applicationDidEnterBackground(_ application: UIApplication) {
    let locationManager = CLLocationManager()
    locationManager.delegate = ViewController()
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.activityType = CLActivityType.other
    locationManager.allowsBackgroundLocationUpdates = true
    locationManager.startMonitoringSignificantLocationChanges()
}

ViewController.swift

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {    
    // Capture of new location 
}

2 个答案:

答案 0 :(得分:0)

在plist中添加这些

NSLocationAlwaysUsageDescription

NSLocationWhenInUseUsageDescription

enter image description here

private void add_new_row()
    {
        if (ViewState["ProductsSold"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["ProductsSold"];
            DataRow drCurrentRow;

            //if (dtCurrentTable.Rows.Count > 0)
            //{
                //for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                //{
                    //Creating new row and assigning values  
                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["ddl_choose_item"] = "ddl";
                    drCurrentRow["txt_price"] = "pric";
                    drCurrentRow["txt_discount"] = "dis";
                    drCurrentRow["txt_quantitiy"] = "quan";
                    drCurrentRow["txt_total"] = "tot";
                //}

                //Added New Record to the DataTable  
                dtCurrentTable.Rows.Add(drCurrentRow);
                //storing DataTable to ViewState  
                ViewState["ProductsSold"] = dtCurrentTable;
                add_bill_GridView.DataSource = dtCurrentTable; 
                add_bill_GridView.DataBind();
            //}
        }
    }

答案 1 :(得分:0)

将此代码写入 AppDelegate didFinishLaunchingWithOptions

  //location manager
  locationManager = CLLocationManager()
  locationManager.delegate = self
  locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
  locationManager.distanceFilter = kCLDistanceFilterNone;
  locationManager.requestAlwaysAuthorization()
  locationManager.allowsBackgroundLocationUpdates = true
  locationManager.startUpdatingLocation()

在第一个屏幕 viewDidLoad()(或不在 didEnterBackground 中的任何地方)调用locationManager.startMonitoringSignificantLocationChanges()