Hi everybody in this short tutorial based post I will share with you one of the tips I have regarding the usage and saving approach of iOS device-token inside of Xamarin.iOS app.
I had a scenario where I wanted to keep the device token in the NSData format without converting it to String... and generally I did not wanted to do the conversion to String and to convert it back to NSData.
For my case, I was looking at the solution to cache (store/persist/save) the device-token for later usage in NSData type. Since I am implementing some feature which and the SDK requires the device token in NSData format in one method.
How to convert device-token from NSData type to String data type is for another post, which I will try to cover in the future, but you can find a lot of resources in the community already.
- Keeping it in String data type was not a option.
- I could not use Xamarin.Essentials to store it, since I did wanted to convert it to string and back to NSData
The solution is:
- Usage of NSUserDefaults on iOS
I made simple and static class with one field and full property, and it looks like this:
In order to use this within the RegisteredForRemoteNotifications, since that is the best place and time where we want to save it for later usage.
... and yeah, that is pretty much it....
Later you can use it like any other property, since it has getter and setter logic for retrive and saving the value for device token on iOS.
The problem is solved, device token is saved, we did not converted it to another type and we have it in "an original form".
This blog post and this sample is for Xamarin.iOS, but similar approach can be used for other developing platforms on iOS as well.
Hope this super short blog post was helpful for you!
Wishing you lots of luck with coding! 👋