地理围栏生成随机通知

时间:2016-02-21 03:39:37

标签: swift cllocationmanager uilocalnotification geofencing

我正在构建一个利用地理围栏来触发本地通知的应用。我有一个允许用户更改地理围栏B的位置和半径的视图。此地理围栏仅在homeNotification设置为true时触发。当用户输入地理围栏A时,homeNotification仅设置为true。此部分工作正常。当用户更改地理围栏B的位置时,我的问题就开始了。地理围栏B的初始位置仍会触发通知,但不会振动或产生噪音。它只显示通知。即使我已设置notifyOnExit = false,通知也会在进入和退出时触发。当我将它们打印到控制台时,该区域也不存在于CLMonitoredRegions中。代码如下。

for region in self.locationManager.monitoredRegions {
            if let circularRegion = region as? CLCircularRegion {
                if circularRegion.identifier == "Home" {
                    self.homeCoordinateOld = circularRegion.center
                    self.homeRadiusOld = circularRegion.radius
                    self.homeIdentifierOld = circularRegion.identifier
                }
            }
        }
        let regionOld = CLCircularRegion(center: self.homeCoordinateOld, radius: self.homeRadiusOld, identifier: self.homeIdentifierOld)
        print("\(regionOld)")
        self.locationManager.stopMonitoringForRegion(regionOld)
        let region = CLCircularRegion(center: homeCoordinate!, radius: 76, identifier: "Home")
        region.notifyOnEntry = true
        region.notifyOnExit = false
        self.locationManager.startMonitoringForRegion(region)

以下是我创建地理围栏B的完整代码。使用相同的视图控制器来更改位置。

func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {

    let home = view.annotation
    let homeCoordinate = home?.coordinate
    let homeLat = home?.coordinate.latitude
    let homeLon = home?.coordinate.longitude
    let homeAddress = searchedStoreNames["\(homeCoordinate!)"]
    let ac = UIAlertController(title: "Confirm", message: "Would you like to add \(homeAddress!) as your home location?", preferredStyle: .Alert)
    ac.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: handleCancel))
    ac.addAction(UIAlertAction(title: "Add Home", style: .Default, handler: {(UIAlertAction) in

        if !CLLocationManager.isMonitoringAvailableForClass(CLCircularRegion) {
            showSimpleAlertWithTitle("Error", message: "Geofencing is not supported on this device!", viewController: self)
            return
        }
        if CLLocationManager.authorizationStatus() != .AuthorizedAlways {
            showSimpleAlertWithTitle("Warning", message: "Your geotification is saved but will only be activated once you grant permission to access the device location. Do this under settings.", viewController: self)
        }

        for region in self.locationManager.monitoredRegions {
            if let circularRegion = region as? CLCircularRegion {
                if circularRegion.identifier == "Home" {
                    self.homeCoordinateOld = circularRegion.center
                    self.homeRadiusOld = circularRegion.radius
                    self.homeIdentifierOld = circularRegion.identifier
                }
            }
        }
        let regionOld = CLCircularRegion(center: self.homeCoordinateOld, radius: self.homeRadiusOld, identifier: self.homeIdentifierOld)
        print("\(regionOld)")
        self.locationManager.stopMonitoringForRegion(regionOld)
        let region = CLCircularRegion(center: homeCoordinate!, radius: 76, identifier: "Home")
        region.notifyOnEntry = true
        region.notifyOnExit = false
        self.locationManager.startMonitoringForRegion(region)


        homeLocation["coordinates"] = "\(home!.coordinate)"
        homeLocation["address"] = "\(homeAddress!)"
        homeLocation["latitude"] = "\(homeLat!)"
        homeLocation["longitude"] = "\(homeLon!)"
        //savedStores.append(["name":"Home", "coordinates": "\(home!.coordinate)", "address": "\(homeAddress!)", "identifier": "\(identifier)"])

        NSUserDefaults.standardUserDefaults().setObject(homeLocation, forKey: "homeLocation")
        homeNotifications = true
        NSUserDefaults.standardUserDefaults().setBool(true, forKey: "homeNotifications")
        self.dismissViewControllerAnimated(true, completion: nil)
        self.searchText.text = ""
        self.mapView.removeAnnotations(self.mapAnnotations)
        self.navigationController?.popViewControllerAnimated(true)
        print("Monitored Regions \(self.locationManager.monitoredRegions)")

    }))
    presentViewController(ac, animated: true, completion: nil)
}

这是地理围栏A

@IBAction func saveTabbed(sender: AnyObject) {

    let ac = UIAlertController(title: "Confirm", message: "Would you like to add \(storeNameHolder) to favorites? This will create a reminder.", preferredStyle: .Alert)
    ac.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: handleCancel))
    ac.addAction(UIAlertAction(title: "Add Store", style: .Default, handler: {(UIAlertAction) in

        if !CLLocationManager.isMonitoringAvailableForClass(CLCircularRegion) {
            showSimpleAlertWithTitle("Error", message: "Geofencing is not supported on this device!", viewController: self)
            return
        }
          if CLLocationManager.authorizationStatus() != .AuthorizedAlways {
        showSimpleAlertWithTitle("Warning", message: "Your geotification is saved but will only be activated once you grant Bag-It! permission to access the device location. Do this under settings.", viewController: self)
        } 

        #if FREE

            if savedStores.count == 5 {
                showSimpleAlertWithTitle("Favorites Filled", message: "You can only have 3 favorite stores in Bag-It! Free. To save more stores, download Bag-It! from the App Store.", viewController: self)
                return
            }
        #else
            if savedStores.count == 19 {
                showSimpleAlertWithTitle("Favorites Filled", message: "You can only have 19 favorite stores at one time.", viewController: self)
                return
            }
        #endif
        let newRadiusInMeters = Double(self.newRadius) / 3.2808
        let region = CLCircularRegion(center: self.storeCoordinates, radius: newRadiusInMeters, identifier: "\(self.storeIdentifierHolder)")
        print("\(region)")
        region.notifyOnEntry = true
        region.notifyOnExit = false
        self.locationManager.startMonitoringForRegion(region)
        savedStores.append(["name":self.storeNameHolder, "coordinates": "\(self.storeLat), \(self.storeLon)", "address": "\(self.storeAddressHolder)", "identifier": "\(self.storeIdentifierHolder)", "longitude": "\(self.storeLon)", "latitude": "\(self.storeLat)"])

        NSUserDefaults.standardUserDefaults().setObject(savedStores, forKey: "savedStores")
        storeNotifications = true
        NSUserDefaults.standardUserDefaults().setBool(true, forKey: "storeNotifications")
        print("\(self.locationManager.monitoredRegions)")
        self.navigationController?.popViewControllerAnimated(true)

    }))
    presentViewController(ac, animated: true, completion: nil)
}

0 个答案:

没有答案