I am developing an app in which I have to check programmatically if gps is enabled or not for locations in phone? How do I find this?
if (!LocationInfo.isLocationOn()) {
try {
LocationInfo.setLocationOn();
} catch (ControlledAccessException cae) {
// You don't have the rights to enable GPS programatically.
// You might want to prompt the user for it.
}
}
You might also want to have a look at the following functions:
LocationInfo.isLocationSourceAvailable(int mode) -
Determines if a specific location source is available to provide location information.
LocationInfo.isLocationSourceSupported(int mode) -
Determines if a specific location source is currently supported on the device.
gl
All new BB have on device GPS. You just need to make sure it is set correctly.
First make sure you set GPS Data Source == Device GPS in the Options->Advanced Options app.
Related
Hi I am looking into an app which is written in Xamarin iOS. The feature I am trying to implement is
GPS must be enabled when app is running, if gps is not enabled prompt the user to enable. If the answer is yes enable the gps and continue otherwise exit app.
I am really new Xamarin and mobile development in general. After research i have found this link which shows the lifecycle of Xamarin iOS app.
Xamarin Lifecycle
The question I have is
1) Will I be able to show an alert to user from App Delegate to enable GPS when app returns from background or launched in the overrides (See link)
2) If a dialog is not possible from app delegate will it be better to create a new screen and show the dialog to enable gps instead of adding gps check in all screens.
3) Is this the correct way of checking if GPS is enabled
e.g., when app returns from background
public override void WillEnterForeground(UIApplication application)
{
Console.WriteLine("App will enter foreground");
if(CLLocationManager.Status == CLAuthorizationStatus.Denied)
{
}
}
Welcome #Abe you are on the right path!
Yes, you will be. You just have to place it in the overrides as you mentioned.
You can place the check in the AppDelegate, but it may not be the best thing to do. You might want to just place it in the Appear override of your page.
Yes using AppDelegate lifecycle overrides, is one way of checking for GPS if you need the it when your app is in the background.
Tips:
If you are using Xamarin Native, inside the ViewController, you could instead use the ViewDidLoad override to perform the check for GPS
You could instead just also just look into the Xamarin Essentials examples for a simplified way of dealing with GPS
I am new to Xamarin iOS and mobile dev in general.
I have a application which requires location services, on my view controller I have a button which takes the user to location settings for the app, however, if the main device location is off the user will not be able to do anything with the app level location setting.
I am using this code on my button click event to take the user to the settings page.
if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
{
NSString settingsString = UIApplication.OpenSettingsUrlString;
NSUrl url = new NSUrl(settingsString);
UIApplication.SharedApplication.OpenUrl(url);
}
I would like to know if there is a way to check if device level Location services are off and take the user to that settings page instead of app-level location settings and vice versa.
Also how to take users to Location settings screen if device level location services is disabled. I tried a few combinations but I am unsure what the NSUrl will be.
To check the Device level location permission:
bool deviceLevel = CLLocationManager.LocationServicesEnabled;
Document here: determining_the_availability_of_location_services
To check the app level location permission:
public void CheckAuthorization(CLLocationManager manager, CLAuthorizationStatus status)
{
switch (status)
{
case CLAuthorizationStatus.Authorized | CLAuthorizationStatus.AuthorizedAlways | CLAuthorizationStatus.AuthorizedWhenInUse:
Console.WriteLine("Access");
break;
case CLAuthorizationStatus.Denied | CLAuthorizationStatus.NotDetermined | CLAuthorizationStatus.Restricted:
Console.WriteLine("No Access");
break;
default:
Console.WriteLine("No Access");
break;
}
}
Document here: clauthorizationstatus
Update:
Have a look at answers in there two threads: how-to-programmatically-open-settings-privacy-location-services-in-ios-11 and how-to-open-location-services-screen-from-setting-screen
There says
Avoid use of "prefs:root" or "App-Prefs:root" in you app, otherwise
App will be rejected from App Store. Just open Setting page.
You can not open the device location permission directly, it is not allow through App Store rules.
Just use UIApplication.OpenSettingsUrlString; to open the setting page.
Welcome to mobile and Xamarin! Yes there are several Nuget Packages you can add that will help you do this. One that's gaining popularity is Xamarin Essentials.
As they show in the documentation, just try to get the location, it will handle permissions by itself, and if you face PermissionException, then you can open the settings as you are! Happy Coding
You can check if the user has disabled the Location services at the settings level then check the app level:
if(!CLLocationManager.LocationServicesEnabled)
{
Console.WriteLine("Location Services are off globally go to settings");
// This may get your app rejected using the strings below
if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
{
UIApplication.SharedApplication.OpenUrl(new NSUrl("App-Prefs:root=General"));
}
else
{
UIApplication.SharedApplication.OpenUrl(new NSUrl("prefs:root=General"));
}
}
else if (CLLocationManager.Status == CLAuthorizationStatus.Denied ||
CLLocationManager.Status == CLAuthorizationStatus.NotDetermined ||
CLLocationManager.Status == CLAuthorizationStatus.Restricted)
{
Console.WriteLine("Location Services are off just for your app, got to app settings");
UIApplication.SharedApplication.OpenUrl(new NSUrl(UIApplication.OpenSettingsUrlString));
}
In terms of opening to the system settings or the app settings, UIApplication.OpenSettingsUrlString will go to the app settings as per the docs:
UIApplicationOpenSettingsURLString
Used to create a URL that you can pass to the openURL: method. When you open the URL built from this string, the system launches the Settings app and displays the app’s custom settings, if it has any.
You can use the string:
prefs:root=General
or for iOS 10 and above
App-Prefs:root=General
But Apple may reject your app, tbh I think it's not worth trying to go to the settings just for this reason but up to you.
I want to develop an app that detecting the user's moving way (walking, cycling, driving etc...) and send a specific UILocalNotification for each activity type.
My question is: is it possible to detect it on the background (when the app is completely closed) without draining the device's battery? What will be the best way to do it?
Thank you!
There is coprocessor m7(+) in iPhones upper 5s.
It gives you possibility to get device motion.
Just
import CoreMotion
in your file.
Create a CMMotionActivityManager object:
let motionActivityManager = CMMotionActivityManager()
Check if it`s available on your device:
motionActivityManager.isActivityAvailable()
Use this method:
motionActivityManager.startActivityUpdates(to: OperationQueue.main) { (activity) in
if (activity?.automotive)! {
print("User using car")
}
if (activity?.cycling)! {
print("User is cycling")
}
if (activity?.running)! {
print("User is running")
}
if (activity?.walking)! {
print("User is walking")
}
if (activity?.stationary)! {
print("User is standing")
}
if (activity?.unknown)! {
print("Unknown activity")
}
}
It would return you types of user activity.
Regarding the user activity which can be handled in background tasks are the below once which does not mention about (walking, cycling,driving etc...)
Implementing Long-Running Background Tasks
For tasks that require more execution time to implement, you must request specific permissions to run them in the background without their being suspended. In iOS, only specific app types are allowed to run in the background:
Apps that play audible content to the user while in the background,
such as a music player app
Apps that record audio content while in the background.
Apps that keep users informed of their location at all times, such as
a navigation app Apps that support Voice over Internet Protocol
(VoIP)
Apps that need to download and process new content regularly
Apps that receive regular updates from external accessories
Yes it´s possible to do that!
If your iOS app must keep monitoring location even while it’s in the
background, use the standard location service and specify the location
value of the UIBackgroundModes key to continue running in the
background and receiving location updates. (In this situation, you
should also make sure the location manager’s
pausesLocationUpdatesAutomatically property is set to YES to help
conserve power.) Examples of apps that might need this type of
location updating are fitness or turn-by-turn navigation apps.
Read more here.
Instead of system default below popup for core location permissions, how can I use my default pop up for my app?
And when does this popup come? Is it coming on particular delegate Method?
Is there any way to disable that?
You can not replace that UIAlertView but cutomize the text to hopefully explain better why your app needs access to the user's location.
See https://stackoverflow.com/questions/12562152/replacement-for-purpose-property-of-cllocationmanager
This popup comes when you access this locationServicesEnabled property of CLLocationManager.
[CLLocationManager locationServicesEnabled];
You can handle this in your app. But anyway, you cann't handle it first time according to apple's doc(see locationServicesEnabled topic.
locationServicesEnabled
Returns a Boolean value indicating whether location services are enabled on the device.
+ (BOOL)locationServicesEnabled
Return Value
YES if location services are enabled; NO if they are not.
Discussion
The user can enable or disable location services from the Settings application by toggling the Location Services switch in General.
You should check the return value of this method before starting
location updates to determine whether the user has location services
enabled for the current device. Location services prompts users the
first time they attempt to use location-related information in an app
but does not prompt for subsequent attempts. If the user denies the
use of location services and you attempt to start location updates
anyway, the location manager reports an error to its delegate.
After disallow, try to attempt,
if ([CLLocationManager locationServicesEnabled)
{
//Do your work
}
else
{
if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorized)
{ //Show Alert view... }
}
If we try to access the user's location, iOS will tell the user that our application wants to use their location.
If I do this
[locationManager startUpdatingLocation];
An alert will show.
However, that only happens once.
It looks like some variable or default must have been set up once that display pops out.
How do I reset those default so that next time my app wants to use location users will be asked again?
Google map can displays that again and again.
It's Apple that asks them for permission, not you
Translation: You don't have any control over that part of the process. The little popup:
is only shown by Apple when you first ask for it - so the user always feels in control. After they accept for the first time, Apple assumes they are OK with your app getting their location information from their device, and won't show it again*.
*Unless they specifically go into Settings and disable Location Services for you app.
It's only showed on the first time and there's nothing you can do to change that. What you can do is ask your users to allow it on settings.
You can check if your app has permissions by checking:
[CLLocationManager locationServicesEnabled]
From the docs:
The user can enable or disable location services from the Settings application by toggling the Location Services switch in General.
You should check the return value of this method before starting location updates to determine whether the user has location services enabled for the current device. If this method returns NO and you start location updates anyway, the Core Location framework prompts the user to confirm whether location services should be reenabled.