Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by subjectZero · Nov 18, 2016 at 09:14 AM · ioslocationdevice

[iOS] Asking for location services does not present alert request to user

When using asking for location services(unity 5.4.2p2) does not actually happen on ios device (works on unity 5.3.6). The user is never presented with the location service permission alert

To reproduce.

  1. new project

  2. create a script called Gps

       using UnityEngine;
         using System.Collections;
         
         public class Gps : MonoBehaviour {
         
         // Use this for initialization
         IEnumerator Start () {
     
             // First, check if user has location service enabled
             if (!Input.location.isEnabledByUser)
             {
                 Debug.Log ("should ask for location");
                 // remind user to enable GPS
                 // As far as I know, there is no way to forward user to GPS setting menu in Unity
             }
     
             // Start service before querying location
             Input.location.Start();
             Debug.Log ("should start");
     
     
             // Wait until service initializes
             int maxWait = 20;
             while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
             {
                 yield return new WaitForSeconds(1);
                 maxWait--;
             }
     
             // Service didn't initialize in 20 seconds
             if (maxWait < 1) {
                 print("Timed out");
             }
     
             // Connection has failed
             if (Input.location.status == LocationServiceStatus.Failed) {
                 print("Unable to determine device location");
             }
     
             // Access granted and location value could be retrieved
             else
                 print("Location: " + Input.location.lastData.latitude + " " + Input.location.lastData.longitude + " " + Input.location.lastData.altitude + " " + Input.location.lastData.horizontalAccuracy + " " + Input.location.lastData.timestamp);
     
             // Stop service if there is no need to query location updates continuously
             Input.location.Stop();
         }
     }
    
    
  3. attached to camera

  4. build for ios

  5. play and test on device

as you can see if (!Input.location.isEnabledByUser) does nothing.

I have made a bug report case number 850631

thank you for your time

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image audionomio · Nov 18, 2016 at 09:01 PM 0
Share

Same problem here...

avatar image subjectZero audionomio · Nov 21, 2016 at 06:54 AM 0
Share

@audionomio I haven't received any feedback from unity at all. I really hope they get to it, because it is essential for our game...

3 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by _Paulius · Nov 21, 2016 at 01:44 PM

Starting from iOS 10 Apple requires to set the 'NSLocationWhenInUseUsageDescription' field in App's info.plist file when location services are used (same for Camera & Microphone). You can set it in iOS Player Settings 'Location Usage Description'.

You should get warnings both when building the project in Unity & in Xcode if location services are used, but the description is not set.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image subjectZero · Nov 21, 2016 at 03:00 PM 0
Share

Thanks so much!

avatar image audionomio · Nov 21, 2016 at 05:09 PM 0
Share

Thanks, that did it!

avatar image NeatWolf · Mar 23, 2017 at 11:22 AM 0
Share

Thanks!

But, what should I put in the player settings? The real text to be used?

What if I need to localize it? Is there a way to do it?

avatar image
0

Answer by agentargyle · May 19, 2017 at 01:57 PM

I would also like to point out a few things that may be helpful to others who run into this issue:

  1. The location permissions request in IOS does not seem to get invoked until it is called for in the code. It happens at runtime so if you're code is not trying to access the location right away, it will not pop up the permissions dialog.

  2. If you are relying on the Unity Docs here: https://docs.unity3d.com/ScriptReference/LocationService.Start.html then you should probably comment out the first yield break; statement as this may prevent your code from requesting the location data.

    // First, check if user has location service enabled if (!Input.location.isEnabledByUser)
    yield break; // try Commenting this out and add some curly braces above while you're at it...

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image stephenmutua · Mar 08, 2018 at 08:17 AM 0
Share

did not work for me on android.

avatar image
0

Answer by highpockets · Dec 09, 2020 at 01:01 PM

Ok I was trying to invoke the location permission popup in iOS and none of the above answers indicate a way to do so (though you still need to set the Privacy - Location When In Use Usage Description string in plist or in Player Settings in Unity). My problem is that I am using Mapbox and the first time a user opens the app, the permission popup appears while Mapbox is initializing the map, this had the unfortunate side effect of the map not being able to initialize. The same issue occurred on iOS and Android. So I had to get permission before Mapbox started to initialize the location services/map.. Easy enough on Android, but awkward on iOS. Anyhow, I just put an extra scene which loads before the Mapbox scene and I run the code below.


Both iOS and Android need to be treated differently. In the Unity docs, Input.location.isEnabledByUser is supposed to return wether the user has already accepted on both Android and iOS, but I find it is not dependable because iOS returns true no matter. So for iOS I just start the location services early if it is the users first time opening the app which prompts the user to allow location services and Mapbox then initializes without issues. For Android, There was a helpful method for requesting permission as shown below.


Hopefully this is of help to others. Cheers


 #if UNITY_ANDROID
 using UnityEngine.Android;
 #endif
 
 public class NewGameSessionController : MonoBehaviour
 {
     private void Start()
     {
             
 #if UNITY_ANDROID
         if (!Permission.HasUserAuthorizedPermission( Permission.FineLocation ) )
             Permission.RequestUserPermission( Permission.FineLocation );
             
 #elif UNITY_IOS
         if( !PlayerPrefs.HasKey( "HasPlayed") )
             Input.location.Start();
 #endif 
     }
 }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

77 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Duplication of the scenes for different resolutions ? 1 Answer

Identifying iphone resolution isn't working for all devices 1 Answer

Mobile aspect ratio and scaling - Use 16:10 or 16:9 base for full screen background ? 0 Answers

How to save Score in ios device?? 1 Answer

Check if IOS device is charging 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges