Preface In the previous article, I mentioned that I am currently working on a social APP based on LBS positioning. One of its main functions is to be able to locate the location of each member in the social circle in real time. Uploading the location in real time in the background is a very important technical point. Next, let me talk about my practical experience in this regard. need Let's first look at the specific requirements for implementing this function. Since we are a real-time positioning social app, we need to do the following:
After analyzing the requirements, we will start to introduce how to implement Prepare First, do some preparation In the Capabilities section of the target, turn on Background Modes and check Location updates. Then add a key of NSLocationAlawaysUsageDescription in plist and type anything in value
Code Positioning must deal with CLLocationManager, so we first define a subclass of CLLocationManager and define three variables according to the requirements.
Here are some explanations of these parameters:
Next is the initialization function
The default value here can be adjusted according to needs Then the processing logic after the location update is actually very simple.
The adjustDistanceFilter function is the core of the entire code and will dynamically adjust the distanceFilter parameter according to the current speed to meet our needs.
It should be noted here that the distanceFilter parameter cannot be set all the time because each time it is set, the didUpdateLocations callback will be triggered immediately after the next second (the system's standard shortest update interval is 1 second, that is, the update frequency is 1hz). Therefore, the distanceFilter will be reset only when the change exceeds 10%. #p# Next, in order to be able to be awakened correctly when being killed, we have to do the last step and add the following code to AppDelegate's didFinishLaunchingWithOptions
This is because when the killed APP is awakened by the system in the background, launchOptions will contain the UIApplicationLaunchOptionsLocationKey** field for identification. At this time, we can restart the positioning function. At this point, the positioning function that meets our needs is completed. For this purpose, I wrote a demo to verify (use the simulator and select Debug->Location->Freeway Drive) The result is as follows Next we will discuss several related issues discuss Why not use a timer to control the positioning interval? There are many tutorials on the Internet that use NSTimer to achieve this, but this is not very good. Although the positioning interval is fixed, the problem of power consumption cannot be solved. The background will continue to update the positioning regardless of whether the current location is updated. Of course, if your usage scenario is to upload at regular intervals, you can use a timer to handle it. What are the problems with using distanceFilter? Since distanceFilter = currentSpeed * minInteval, the interval time will fluctuate due to the change of speed, but this fluctuation is within the acceptable range. If the speed increases or decreases, the next update time will be shortened or lengthened accordingly. However, because we are in a real life environment, the speed change cannot be so fast, so this error is acceptable. In addition, we correct the speed of distanceFilter, so the interval will generally remain within our range. Why not use allowDeferredLocationUpdatesUntilTraveled:timeout:allowDeferredLocationUpdatesUntilTraveled is a new API introduced in iOS 6. From the name, we can know that the function of this function is to delay the location update until the movement is xx meters or the time exceeds xx seconds. So doesn't this function just meet all our requirements? But I never expected that this is not the case. This function is not easy to use Next is the time to complain ლ(⁰⊖⁰ლ) Why is this function not easy to use? First of all, this function has many requirements. Let's see what conditions must be met for this function to work.
Regarding the description of Low Power State in iOS, I only found some definitions in the documents on Apple's official website.
As far as I know, this "Low Power State" can only be triggered when the screen is black (not just locked). Any operation on the screen with any power (even push notifications) will cause the APP to exit this state. At the same time, it cannot be entered when the APP is charging. I tried to use this API on a real device and a simulator, but the app still locates at 1HZ (because kCLDistanceFilterNone is set)
The conclusion is... this thing can't even be debugged, so I don't have that much time to go out and test it... Besides, the method I mentioned above can basically meet the needs... So I have given up on continuing to study this API because even if I use this thing, it's just icing on the cake. If any of you know how to use this correctly, please leave a message to tell me. Thank you very much! |
<<: The worst code collection ever
>>: Zhou Hongyi: 360 is too conservative in making smart hardware
The market is very good, but you need to be cauti...
After launching the new Desire 8 series for the pr...
"There's some gold foil on the chicken n...
Norovirus can cause acute infectious diarrhea, an...
Lu Zhanka's 30-day short video oral broadcast...
There is no doubt that the topic of mini programs...
Recently, the author went to Shenzhen to attend a...
lead We have talked about autonomous driving, sma...
I have talked a lot about the process of QQ's ...
“I mourn the brevity of my life and envy the infi...
Several days have passed since the finale of Nirv...
Those who understand this tool will naturally und...
“Once you fall into the rabbit hole of conspiracy...
Source code introduction: High imitation of Baidu...
At present, almost all major countries in the wor...