startMonitoringForRegion and iOS5: is accuracy better compared to iOS4? - gps

Can this method use background GPS for this operation ?
I remember that on iOS4, the method was not very accurate (based on cell location)...
Any detailled information available ?

Short answer : No, but....
Long answer : Region monitoring works on cell+wifi analyzing by default , but by specifying a bigger radius , your app gets a callback (even if it's not suspended at the moment).
From my experience , you can use region monitoring just to cross a specific boundaries , and after entering a region you can "boost" location accuracy by using startUpdatingLocation + setting desired accuracy.
note: you should enter your code in application:didfinishLaunchingWithOptions as well , for the case where your app is not running, and of course stop updating location when crossing a boundary/recognizing an event that should return your app to previous "region monitoring" state, to conserve battery power.

Related

Can I use only GPS to plot my current position instead of the cellular network

I am working on a scheduling and routing app that uses Google Maps to create my route to the appointment location. Upon arriving at the appointment location, the app sets up a geo-fence around that location. The app is designed to send an eta text to the next appointment when I leave out of the geo-fence. Since I am likely to drive through the geo-fence due to either missing the address or looking for a parking spot, I have a timer set for 3 minutes. This requires that I stay inside of the geo-fence for 3 minutes. Once the 3 minutes elapses, then the app is set to send my eta text to the next appointment once I leave out of the fence. My location on the map appears to be determined by the triangulation of the cell towers. Here's the problem, I'm testing in a rural area with poor cell signals and limited towers. This reduces the accuracy of my actual location as determined by the triangulation of the cell towers. It is causing the icon that represents my location to bounce in and out of the geo-fence without completing the 3 minute time requirement. This is most likely due to the poor cell connection and network. Is there a way for me use only the GPS feature of the phone/Google Maps and disable the cell network from trying to position me? This doesn't happen in urban areas where there are more towers and a strong network.

How to detect network disconnects (on an RTCPeerConnection) as soon as possible or the resulting frozen video?

I am using RTCPeerconnections to submit video and audio in a web-RTC-based video-messenger. I am able to detect network disconnects after approximately 7 seconds - yet, this is 7 seconds in which the user is staring at a frozen video and starts to randomly click buttons in the app. I would like to improve the user experience by shortening this timespan - e.g. by informing the user about a network issue if the video freezes for more than 1 second.
Status Quo: I am currently detecting respective situations by listening to the onconnectionstatechange event of the RTCPeerConnection. Yet, the event is only fired approximately 7 seconds after the disconnect. I determined the ~7 seconds by connecting two machines via normal WiFi, using a hardware switch on one of the laptops to switch off the wireless (such switches exist on some older Lenovo models / guarantee an immediate disconnect) and wait for the other machine to detect the event.
Consideration: The root cause being the interruption of the underlying network connection, it would be ideal to detect the changed network status as early as possible (even if its just transport delays). This said, the disturbance faced by the user ultimately stems from the video that instantly freezes when interrupting the network. If there was no way to detect the connection issue earlier, it could be an option to detect the frozen video instead. Is any of these two things possible (ideally event-driven, so that I don't need to poll things every second)?
Here's a very simple code snippet describing my current disconnect detection:
myRTCPeerConnection.onconnectionstatechange = (event: Event) => {
let newCS = myRTCPeerConnection.connectionState;
if (newCS == "disconnected" || newCS == "failed" || newCS == "closed") {
//do something on disconnect - e.g. show messages to user and start reconnect
}
}
(ice)connectionstatechange is the right event in general.
If you want more granularity you'll need to poll getStats and looks for stats like framesReceived. But there is no guaranteed frame rate sent from the other side (e.g. in screensharing you go below 1/s).
While the actual ICE statistics like requestsSent seem more useful they happen much less frequently, only once per second and you can loose a packet or it comes late.
In general this is a question of how reliable the detection of the network failure is. If it is too aggressive you end up with a poor UX showing a warning too often.
You might not end up that is significantly better than at the cost of introducing complexity that you need to maintain.
Thanks Philipp for your insights - this pointed me into the right direction.
I'm now looking into using getStats to identify any freezes. At first sight, polling the framesPerSecond value seems most promising to me. The good thing: it reacts instantly upon disconnect - and - it still works when the underlying video stream is paused (i'm allowing the user to pause video submission / implemented it by setting all video tracks to enabled = false). I.e. even if the video tracks are disabled on the sending side, the receiving side still continues to receive the agreed frames per second.
As the usage of the getStats function appears weak on documentation at the time of this being written / there's rarely a simple examples for its usage, please find my code extract below:
peerRTCPC
.getReceivers()
.forEach(
(
receiver: RTCRtpReceiver,
index: number,
array: RTCRtpReceiver[]
) => {
if (receiver.track.kind == "video") {
receiver.getStats().then((myStatsReport: RTCStatsReport) => {
myStatsReport.forEach(
(statValue: any, key: string, parent: RTCStatsReport) => {
if (statValue.type == "inbound-rtp") {
console.log(
"The PC stats returned the framesPerSecond value " +
statValue["framesPerSecond"] +
" while the full inbound-rtp stats reflect as " +
JSON.stringify(statValue)
);
}
}
);
});
}
}
);
Note that upon disconnect, the framesPerSecond do not necessarily go to zero, even though the webRTCInternals screen suggests the same. I am seeing undefined when a disconnect happens.
Runtime impact of polling this at high frequency / across larger numbers of connections probably needs to be looked at more closely. Yet, this seems like a good step into the right direction unless doing it way to frequently.

Ranging an iBeacon latency

I have been playing around with the new iBeacons in iOS 7. I have one device setup as a beacon, and the other device ranging to detect when I am near, far, immediate, etc. I'd like to know very quickly when I cross between these ranges. Is there any way to adjust the latency? I find that I have to move my device around very slowly or I will not know when I cross these thresholds.
No, you would not be able to adjust the beacon latency. As Apple says in Region Monitoring Guide:
To prevent spurious notifications, iOS does not deliver region
notifications until certain threshold conditions are met.
Specifically, the user’s location must cross the region boundary and
move away from that boundary by a minimum distance and remain at that
minimum distance for at least 20 seconds before the notifications are
reported.
Apple does not define what the latency is, it seems it's not fast enough for your application.
You can have a tradeoff - to implement beacon ranging yourself using Core Bluetooth and listen to the CBPeripheral advertisement events while scanning and range using RSSI:
centralManager:didDiscoverPeripheral:advertisementData:RSSI:
If you are using a custom beacon, such as the RadiusNetworks VirtualiBeacon VM image you can adjust the frequency of the advertisements. The flip side your app must run in the foreground opposed to CoreLocation delivering beacon events even when your app is not running.

NSDate: Get precise time independent of device clock? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I locally detect iPhone clock advancement by a user between app runs?
Is there a way to determine the actual time and date in iOS (not the time of the device)
Is there a clock in iOS that can be used that cannot be changed by the user
Brief
I am working with an auto-renewable subscription-based app. When the app receives the latest receipt from Apple, it stores the expires_date_ms key in NSUserDefaults. Thirty days after that date, the app checks with Apple to see if the subscription is still active. The app can be considered an offline app, but it must connect to the internet once every 30 days in order to check the subscription status. This time comparison will be used to tell the user he/she must connect.
Problem
I am using the code below to compare the current time with the expires_date_ms:
NSTimeInterval expDateMS = [[productInfo objectForKey:#"expires_date_ms"] doubleValue];
NSTimeInterval currentDateMS = ([[NSDate date] timeIntervalSince1970] * 1000);
if (currentDateMS > expDateMS)
subExpired = YES;
This is fine and works well, but from what I can tell there's a loophole that can be exploited - if the user sets the device's clock back a hour/month/decade, the time comparison will become unreliable because [NSDate date] uses the device's current time (please correct me if I'm wrong).
Question
Is there any way of retrieving a device-independent time in milliseconds? One that can be accurately and reliably measured with no regards to the device clock?
While Kevin and H2CO3 are completely correct, there are other solutions for the purposes of checking a subscription (which I would hope does not need millisecond accuracy....)
First watch UIApplicationSignificantTimeChangeNotification so that you get notifications of when the time changes suddenly. This will even be delivered to you if you were suspended (though I don't believe you will receive it if you were terminated). This gets called when there is a carrier time update, and I believe it is called when there is manual time update (check). It also is called at local midnight and at DST changes. The point is that it's called pretty often when the time suddenly changes.
Keep track of what time it was when you go into the background. Keep track of what time it is when you come back into the foreground. If time moves radically backwards (more than a day or two), kindly suggest that you would like access to the network to check things. Whenever you check-in with your server, it should tell you what time it thinks it is. You can use that to synchronize the system.
You can similarly keep track of your actual runtime. If it gets wildly out of sync with apparent runtime, then again, request access to the network to sync things up.
I'm certain that attackers would be able to sneak 35 days or whatever out of this system rather than 30, but anyone willing to work that hard will just crack your software and take the check out entirely. The focus here is the uncommitted attacker who is just messing with their clock. And that you can catch pretty well.
You should test this carefully, and be very hesitant to accuse the user of anything. Just connecting to your server should always be enough to get a legitimate user working again.
You need to connect to/retrieve information from a reliable, official time server and use that time data in your app. For example, here's a world time server with an easy-to use API
Here are three options I can think of:
clock_gettime(CLOCK_MONOTONIC) gets the current system uptime. This is relatively unreliable because if the user reboots, this is reset. You could save the last value used and at launch use the last saved value as an offset, but the problem with this is that the time that the device was shut off for won't be calculated.
mach_absolute_time() counts the number of CPU ticks since the last reboot. It can be fetched easily through CACurrentMediaTime. Note that this can be reset simply by rebooting the device, so if changing the time is very important, I'm not so sure if you would go this way.
Network Time Protocol (NTP) is a networking protocol for synchronizing the clocks of computer systems. In practice, all NTP is is querying a time server. An iOS library for NTP can be found here.
So the first two methods do not require connectivity, while the third does. However, the third method is the only foolproof one.
There is no such thing as a non-mutable device clock that persists across reboots. The only way to get a trustworthy time is to contact a remote server that you trust and ask what its time is.

Need a delay to wait for GPS

Using the iPhone and objective C, is there a way to stall or perform a timing loop to allow for the GPS to catch up and return a valid set of coordinates?
Currently, the application runs too quickly and the GPS cannot supply the coordinates fast enough...
Since you said you're on iPhone, you're using CLLocationManager. Just set a delegate on the manager and wait for the locationManager:didUpdateToLocation:fromLocation: message to know when the GPS data is ready.
Assuming your GPS polling is running in a different thread to the User Interface, you can call the static NSThread functions sleepForTimeInterval or sleepUntilDate from the thread that is waiting for the GPS data.
If your mobile application is using GPS, your application should be prepared for location updates, even if your application doesn't track movements..
A common case would be where the user put your application in background and activate it later on a completely different location.
On iOS, create an implementation of CLLocationManagerDelegate like Anomie wrote. And use the timestamp of the update to evaluate the freshness of the location.
Don't sleep & poll like other people suggested.
Either block to wait for data or don't update anything if no data received. There is of course usleep(), but without showing code and specifically how your loop is executed and by what mechanism (threaded or not) we can only answer in general terms.