简体   繁体   中英

React Native iOS app restart on reboot

I want to create an iOS app using React Native. One of the primary features of the app is that it runs constantly in the background. It also requires using GPS btw (in case that is important).

I have had a number of devs tell me its not possible to do this for iOS, however I have read that it is do-able.

Is this possible to do in the iOS environment? Mainly, if the app is running when the phone is powered off, can you make it open when the device is restarted without the user opening it?

I should say that I am a RN novice and any help is much appreciated.

If your app gets permission to get location while it's not in the foreground, then you will get periodic updates and some time to process it.

For example, if you are giving driving directions.

https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html#//apple_ref/doc/uid/TP40009497-CH2-SW10

If there is no good user benefit for you to get the location in the background constantly, you might be rejected. Apple suggests region monitoring instead

iOS supports the delivery of location events to apps that are suspended or no longer running. The delivery of location events in the background supports apps whose functionality would be impaired without them, so configure your app to receive background events only when doing so provides a tangible benefit to the user. For example, a turn-by-turn navigation app needs to track the user's position at all times and notify the user when it's time to make the next turn. If your app can make do with alternate means, such as region monitoring, it should do so.

I have no idea how RN wraps this behavior, but no matter what it does (or what a plugin might do), the core iOS behavior is how it is described in that URL.

I verified yesterday (at least on iOS 11.2 simulator) that automatically restarting the app (and the location tracking) after phone reboot works.

The key point is that startMonitoringSignificantLocationChanges needs to be on to wake up the app after reboot. For me the difficult part was figuring out when to turn it on, because I couldn't find a reliable way to detect when the phone is being rebooted or run any code at that point. However, based on initial testing it looks like startMonitoringSignificantLocationChanges is independent and doesn't negatively impact the usual location updates, so now I just leave it on all the time and toggle startUpdatingLocation / stopUpdatingLocation on top of it based on Core Motion-detected activity.

Otherwise requirements are the same as for any location tracking on the background, ie handle permissions and don't process too much. Apple documentation explains how to detect that the app was relaunched by a location event.

这是一个react native模块,基本上可以帮助您实现所描述的内容: https : //github.com/transistorsoft/react-native-background-geolocation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM