iOS Question LocManager_LocationError The operation couldn’t be completed. (kCLErrorDomain error 1.)

Alex_197

Well-Known Member
Licensed User
Longtime User
Hi all.

I created a class based on Erel Location example. It works fine but today I got an error
LocManager_LocationError The operation couldn’t be completed. (kCLErrorDomain error 1.)

Permission dialog appeared and permission was granted. I'm using iPhone 7 with iOS 16.

Despite this error Lat / Long are correct and it only happens once.

I don't quite undertand what domain this error is talking about?
Here is a code of my class

B4X:
Sub Class_Globals
    Public LocManager As LocationManager
    Public strHeading As String,blnEnable As Boolean
    Public strAltitude As String,strBearing As String,strLL As String
    Public strSpeed  As String,strTime As String,strAccuracy As String
    Public Latitude As Double,Longitude As Double
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
 
'    If LocManager.IsAuthorized=False Then
        LocManager.Initialize("LocManager")
'    End If
 
End Sub


public Sub LocManager_AuthorizationStatusChanged (Status As Int)
 
    Try
    
        If LocManager.AuthorizationStatus = LocManager.AUTHORIZATION_DENIED Then
            Log("AUTHORIZATION_DENIED")
            blnEnable=False
        Else if LocManager.AuthorizationStatus = LocManager.AUTHORIZATION_RESTRICTED Then
            Log("AUTHORIZATION_RESTRICTED")
            blnEnable=False
        Else   
            blnEnable=True
        End If
    
        Main.IsGPSOk=blnEnable
    
        StartLocationUpdates
 
    Catch
        Log("LocManager_AuthorizationStatusChanged " & LastException.description)
        Main.modFun.ShowError("clsGPS_LocManager_AuthorizationStatusChanged " & LastException.description)
    End Try
 
End Sub


public Sub StartLocationUpdates
 
    Try
    
        'if the user allowed us to use the location service or if we never asked the user before then we call LocationManager.Start.
        If LocManager.IsAuthorized Or LocManager.AuthorizationStatus = LocManager.AUTHORIZATION_NOT_DETERMINED Then
            LocManager.Start(0)
        
        End If
        LocManager.StartHeading
 
    Catch
        Log("StartLocationUpdates " & LastException.description)
        Main.modFun.ShowError("clsGPS_StartLocationUpdates " & LastException.description)
    End Try
 
End Sub


Public Sub LocManager_HeadingChanged (MagneticHeading As Double, TrueHeading As Double)
 
    Try
    
        If TrueHeading >= 0 Then
            strHeading = NumberFormat(TrueHeading, 1, 0) & Chr(176) & " (true north)"
        Else
            strHeading = NumberFormat(MagneticHeading, 1, 0) & Chr(176) & " (magnetic)"
        End If
 
    Catch
        Log("LocManager_HeadingChanged " & LastException.description)
        Main.modFun.ShowError("clsGPS_LocManager_HeadingChanged " & LastException.description)
    End Try
End Sub

public Sub LocManager_AllowCalibration As Boolean
    Return True
End Sub

public Sub LocManager_LocationError
    Log("Error: " & LastException.description)
    Main.modFun.ShowError("LocManager_LocationError " & LastException.description )
End Sub

public  Sub LocManager_LocationChanged (Location1 As Location)
 
    Try
    
    
        If Location1.VerticalAccuracy >= 0 Then
            strAltitude =    NumberFormat(Location1.Altitude, 1, 1) & "m"
        Else
            strAltitude = "N/A"
        End If
        strBearing = ValueOrNA (Location1.Bearing, 1, Chr(176))
        If Location1.Accuracy >= 0 Then
            strLL = NumberFormat(Location1.Latitude, 2, 4) & " / " & NumberFormat(Location1.Longitude, 2, 4)
            Latitude=NumberFormat(Location1.Latitude, 2, 4)
            Longitude=NumberFormat(Location1.Longitude, 2, 4)
        Else
            strLL = "N/A"
            Latitude=0
            Longitude=0
        End If
        strSpeed = ValueOrNA(Location1.Speed, 1, "m/s")
        strTime = DateTime.Time(Location1.Time)
        strAccuracy = ValueOrNA(Location1.Accuracy, 2, "m")
 
    Catch
        Log("LocManager_LocationChanged " & LastException.description)
        Main.modFun.ShowError("clsGPS_LocManager_LocationChanged " & LastException.description)
    End Try

End Sub

Private Sub ValueOrNA(value As Double, NumberOfFractions As Int, unit As String) As String
 
    Try
        If value < 0 Then
            Return "N/A"
        Else
            Return NumberFormat(value, 1, NumberOfFractions) & unit
        End If
 
    Catch
        Log("ValueOrNA " & LastException.description)
        Main.modFun.ShowError("clsGPS_ValueOrNA " & LastException.description)
        Return "Error"
    End Try
 
End Sub




public Sub CheckGPS As Boolean
    Try

        Return Main.IsGPSOk
    
    Catch
        Log("CheckGPS " & LastException.Description)
        Main.modFun.ShowError("SimpleCamera_CheckGPS " & LastException.description)
        Return False
    End Try
End Sub

Thanks
 
Last edited:

Alex_197

Well-Known Member
Licensed User
Longtime User
That's exactly like in your example https://www.b4x.com/android/forum/threads/location-gps.46148/

B4X:
Private Sub LocManager_AuthorizationStatusChanged (Status As Int)
    lblEnable.Visible = (LocManager.AuthorizationStatus = LocManager.AUTHORIZATION_DENIED _
        Or LocManager.AuthorizationStatus = LocManager.AUTHORIZATION_RESTRICTED)
    StartLocationUpdates
End Sub

Private Sub StartLocationUpdates
    'if the user allowed us to use the location service or if we never asked the user before then we call LocationManager.Start.
    If LocManager.IsAuthorized Or LocManager.AuthorizationStatus = LocManager.AUTHORIZATION_NOT_DETERMINED Then
        LocManager.Start(0)
    End If
    LocManager.StartHeading
End Sub
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
You are correct. It is fine as there is another check in StartLocationUpdates.

I guess that this is the expected behavior. It fails to get the location because you don't have permission yet and then it shows the dialog.
I'm also getting this error even if the permission granted. If I on the screen where I need a GPS but want to check my emails so I put the application into background mode and after in about 10 seconds I'm getting this error - I see it in the log. Here is a small project attached.

Please see my log

B4X:
Application_Start
Application_Active
We are active
LocManager.AuthorizationStatus=    4
AUTHORIZATION_OK
StartLocationUpdates LocManager.AuthorizationStatus=    4
LocManager.IsAuthorized=    true
btnStartGPS_Click
StartLocationUpdates LocManager.AuthorizationStatus=    4
LocManager.IsAuthorized=    true
Application_Inactive
We are Inactive
Application_Background
We are in background
LocManager_LocationError LocManager.AuthorizationStatus=    4
LocManager.IsAuthorized=    true
Error: The operation couldn’t be completed. (kCLErrorDomain error 1.)
Application_Foreground
Application_Foreground
Counter: 0
Application_Active
We are active
btnStartGPS_Click
StartLocationUpdates LocManager.AuthorizationStatus=    4
LocManager.IsAuthorized=    true
 

Attachments

  • GPS.zip
    6.4 KB · Views: 44
Upvote 0
Top