app delegate:
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
// Get current device token
var DeviceToken = deviceToken.Description;
if (!string.IsNullOrWhiteSpace(DeviceToken))
{
DeviceToken = DeviceToken.Trim('<').Trim('>');
}
// Get previous device token
var oldDeviceToken = NSUserDefaults.StandardUserDefaults.StringForKey("token");
// Has the token changed?
if (string.IsNullOrEmpty(oldDeviceToken) || !oldDeviceToken.Equals(DeviceToken))
{
//TODO: Put your own logic here to notify your server that the device token has changed/been created!
}
// Save new device token
//NSUserDefaults.StandardUserDefaults.SetString(DeviceToken, "token");
IOSUserPreferences userPreference = new IOSUserPreferences();
userPreference.SetString("token", DeviceToken);
}
my PCL interface:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace neoFly_Montana
{
public interface IUserPreferences
{
void SetString(string key, string value);
string GetString(string key);
}
}
when I call the interface in pcl:
string token = App.UserPreferences.GetString("token");
App.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Forms;
using Xamarin.Forms.Maps;
namespace neoFly_Montana
{
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new NavigationPage(new Views.Splash2()) { BarTextColor = Color.FromHex("#f98e1e") };
}
protected override void OnStart()
{
// Handle when your app starts
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
public static IUserPreferences UserPreferences { get; private set; }
public static void Init(IUserPreferences userPreferencesImpl)
{
App.UserPreferences = userPreferencesImpl;
}
}
}
on iphone project:
using Foundation;
using System;
using System.Collections.Generic;
using System.Text;
namespace neoFly_Montana.iOS
{
class IOSUserPreferences : IUserPreferences
{
public void SetString(string key, string value)
{
NSUserDefaults.StandardUserDefaults.SetString(value, key);
}
public string GetString(string key)
{
return NSUserDefaults.StandardUserDefaults.StringForKey(key);
}
}
}
on Android it works fine, saves and gets the data...
as a test, I tried to save a data and get it on app delegate and it worked fine, I could see it accesing the IOSUserPreferences script, the problens occurs when I try to get it on pcl
when I try to get the data that is saved, only in ios, it's null and app crashes...I noticed that it doesn't access the get string method on iphone project... Does someone know what is happening ?