sameerkapps / SecureStorage

119 stars 32 forks source link

iOS Not Able to Return Vale #20

Closed dgerding closed 6 years ago

dgerding commented 6 years ago

Hi, I have the entitlements property set to:

<dict>
    <key>keychain-access-groups</key>
    <array>
        <string>fadsdasdad96.com.wordbahn.app.ios</string>
    </array>
</dict>

Which is a real iOS cert/provision etc... The project builds and runs. But I'm not getting correct values back when I try and persist a user Id on startup using the following code:

bool hasCachedAppId = false;
            try
            {
                hasCachedAppId= CrossSecureStorage.Current.HasKey("OwnerId");
            }
            catch (Exception ex)
            {
                var ErrMessage = ex.Message;
            }

            if (!hasCachedAppId)
            {
                if (!CrossConnectivity.Current.IsConnected)
                {
                    throw new NotImplementedException("Add offline page popup here");
                }
                else
                {
                    //call backend GetOwnerIdWithDeviceDetails
                    try
                    {
                        var tempGuid = Guid.NewGuid().ToString();
                        CrossSecureStorage.Current.SetValue("OwnerId", tempGuid);
                        var checkGuid = CrossSecureStorage.Current.GetValue("OwnerId");

                        if (tempGuid==checkGuid)
                        {
                            IsUsingDeviceUserId = true;
                            UserId = tempGuid;
                        }
                        else
                        {
                            throw new Exception("Failed to access read or write device secure storage.");
                        }
                    }
                    catch (Exception e)
                    {
                        TrackException("Secure Storage read or write failed during app init.", e, true);
                    }
                }
            }
            else
            {
                try
                {
                    UserId = CrossSecureStorage.Current.GetValue("OwnerId");
                    IsUsingDeviceUserId = true;
                }
                catch (Exception e)
                {
                    TrackException("Secure Storage read or write failed during app init.", e, true);

                }
            }

Are there any additional steps needed beyond the entitlements property to get iOS secure storage to work? The code works in Android.

Thanks! Dave G

dgerding commented 6 years ago

The line "if (tempGuid==checkGuid)" should return true in iOS but returns false.

camaya commented 6 years ago

I'm experiencing the same issue now. It works correctly on Android but it doesn't on iOS.

ValonK commented 6 years ago

This Issue affects me as well on iPhone X iOS 11.2 Simulator, the value is always null.

iPhone X iOS 11.2 Simulator Xamarin.Forms 2.5.0.280555 SecureStorage 2.0.1

Dodda commented 6 years ago

I am using iPhone Simulator 11.3, still exists the same problem, the get value always return null

sameerkapps commented 6 years ago

Please check the latest package 2.5.0. It sets KeyChain access level at AfterFirstUnlock as default. That should solve the problem. I have also updated the sample App to test in OnStart, OnSleep, and OnResume. If you still face the issue, please send a sample.

sameerkapps commented 6 years ago

Closing with assumption no response => Works :). Please reopen, if required.