
- iOS Tutorial
- iOS - Home
- iOS - Getting Started
- iOS - Environment Setup
- iOS - Objective-C Basics
- iOS - First iPhone Application
- iOS - Actions and Outlets
- iOS - Delegates
- iOS - UI Elements
- iOS - Accelerometer
- iOS - Universal Applications
- iOS - Camera Management
- iOS - Location Handling
- iOS - SQLite Database
- iOS - Sending Email
- iOS - Audio & Video
- iOS - File Handling
- iOS - Accessing Maps
- iOS - In-App Purchase
- iOS - iAd Integration
- iOS - GameKit
- iOS - Storyboards
- iOS - Auto Layouts
- iOS - Twitter & Facebook
- iOS - Memory Management
- iOS - Application Debugging
- iOS Useful Resources
- iOS - Quick Guide
- iOS - Useful Resources
- iOS - Discussion
How to request permission programatically to use location services in iPhone/iOS?
To request location services permission in ios with swift we can use the CLLocationManager.
We’ll do this with help of a sample project. So, create a new project. First, we need to create a locationManager object, so in your view controller.
var locationManager = CLLocationManager()
Now, we, first of all, we need to check if the location services are enabled on the device or not. To check this we’ll use
CLLocationManager.locationServicesEnabled() function, which returns a Boolean value showing whether the location service on the device is active or not.
if CLLocationManager.locationServicesEnabled() { print("permissions allowed") } else { locationManager.requestAlwaysAuthorization() locationManager.requestWhenInUseAuthorization() }
In the example above, if the location services are enabled, then we print “permissions allowed”, otherwise we request two kind of authorizations, alwaysInUse, and WhenInUse authorization.
Now, let’s see another example where we’ll see what kind of permission is granted if the location services are active on a device.
We’ll use the CLLocationManager.authorizationStatus() method, which returns us the kind of authorization given. It is an enum which has 5 possible values.
As per the official Apple documentation, the enum has the following values.
notDetermined, restricted, denied, authorized, authorizedWhenInUse.
Let’s see the other example.
if CLLocationManager.locationServicesEnabled() { switch CLLocationManager.authorizationStatus() { case .authorizedAlways,.authorizedWhenInUse : print("authorized.") case .denied,.restricted,.notDetermined : print("not authorized.") } }
- Related Articles
- How to request Location Permission at runtime on iOS
- How to request Location Permission at runtime on Kotlin?
- How to access RESTFul services from iOS/iPhone?
- How to request Location permission at run time in Android?
- How to check if Location Services are enabled in iOS App?
- How to parse JSON object in iPhone/iOS?
- How to compare two NSDates in iPhone/iOS?
- How to create a WebView in iOS/iPhone?
- How to use Bold & Non-Bold Text In A Single UILabel in iOS/iPhone?
- How to take a screenshot programmatically in iPhone/iOS?
- How to add 1 day to a Date in iOS/iPhone?
- How to add line break for UILabel in iOS/iPhone?
- Redirect Output to location with Permission denied Error?
- How to check if Location Services are enabled in Android App?
- How to get the MAC address of an iOS/iPhone programmatically?
