Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Request state javascript function (iOS) #126

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/ios/GeofencePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ func log(message: String){
NSLog("%@ - %@", TAG, message)
}

func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}

@available(iOS 8.0, *)
@objc(HWPGeofencePlugin) class GeofencePlugin : CDVPlugin {
lazy var geoNotificationManager = GeoNotificationManager()
Expand Down Expand Up @@ -98,6 +107,25 @@ func log(message: String){
}
}

func requestState(command: CDVInvokedUrlCommand) {
dispatch_async(dispatch_get_global_queue(priority, 0)) {
for id in command.arguments {
let region = self.geoNotificationManager.getMonitoredRegion(id as! String)
if (region != nil) {
log("RequestingStateForRegion \(id)")
self.geoNotificationManager.locationManager.requestStateForRegion(region!)
}
else {
log("RequestingStateForRegion, region not found \(id)")
}
}
dispatch_async(dispatch_get_main_queue()) {
let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK)
self.commandDelegate!.sendPluginResult(pluginResult, callbackId: command.callbackId)
}
}
}

func remove(command: CDVInvokedUrlCommand) {
dispatch_async(dispatch_get_global_queue(priority, 0)) {
for id in command.arguments {
Expand Down Expand Up @@ -184,10 +212,12 @@ class GeofenceFaker {
dispatch_async(dispatch_get_main_queue()) {
if let region = self.geoNotificationManager.getMonitoredRegion(id) {
log("FAKER Trigger didEnterRegion")
/*
self.geoNotificationManager.locationManager(
self.geoNotificationManager.locationManager,
didEnterRegion: region
)
*/
}
}
}
Expand Down Expand Up @@ -332,6 +362,7 @@ class GeoNotificationManager : NSObject, CLLocationManagerDelegate {
log("deferred fail error: \(error)")
}

/*
func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion) {
log("Entering region \(region.identifier)")
handleTransition(region, transitionType: 1)
Expand All @@ -341,17 +372,35 @@ class GeoNotificationManager : NSObject, CLLocationManagerDelegate {
log("Exiting region \(region.identifier)")
handleTransition(region, transitionType: 2)
}
*/

func locationManager(manager: CLLocationManager, didStartMonitoringForRegion region: CLRegion) {
let lat = (region as! CLCircularRegion).center.latitude
let lng = (region as! CLCircularRegion).center.longitude
let radius = (region as! CLCircularRegion).radius

log("Starting monitoring for region \(region) lat \(lat) lng \(lng) of radius \(radius)")
delay(0.2) {
log("Requesting State for region \(region) lat \(lat) lng \(lng)")
manager.requestStateForRegion(region)
}
}

func locationManager(manager: CLLocationManager, didDetermineState state: CLRegionState, forRegion region: CLRegion) {
log("State for region " + region.identifier)

if state == CLRegionState.Inside {
log("In \(region.identifier)")
handleTransition(region, transitionType: 1)
}
else if state == CLRegionState.Outside {
log("Not in \(region.identifier)")
handleTransition(region, transitionType: 2)
}
else {
log("Not determined")
}

}

func locationManager(manager: CLLocationManager, monitoringDidFailForRegion region: CLRegion?, withError error: NSError) {
Expand Down
9 changes: 9 additions & 0 deletions www/geofence.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ module.exports = {
getWatched: function (success, error) {
return execPromise(success, error, "GeofencePlugin", "getWatched", []);
},
/**
* Requesting state of geofences with given ids
*
* @name requestState
* @param {String} Geofence id
*/
requestState: function(ids,success,error) {
return execPromise(success, error, "GeofencePlugin", "requestState", ids);
},
/**
* Called when app is opened via Notification bar
*
Expand Down