从MonoTouch访问iCloud NSUbiquitousKeyValueStoreDidChangeExternallyNotification详细信息

t9m*_*ike 3 xamarin.ios ios5 icloud

在MonoTouch下,我想访问NSUbiquitousKeyValueStoreDidChangeExternallyNotification(iOS5/iCloud键值入站更新)通知数据:

  • NSUbiquitousKeyValueStoreChangeReasonKey - > NSNumber
  • NSUbiquitousKeyValueStoreChangedKeysKey - > NSString的NSArray

有一个示例@ http://docs.xamarin.com/@api/deki/files/321/=iCloud.zip,但上面访问的代码已注释掉并且有错误(它如何尝试将原因转换为整数是错的).我在这附近:

NSNotificationCenter.DefaultCenter.AddObserver(new NSString("NSUbiquitousKeyValueStoreDidChangeExternallyNotification"), 
delegate(NSNotification n)  
{
    NSDictionary userInfo = n.UserInfo;
    NSNumber reason = (NSNumber)userInfo.ObjectForKey(
        new NSString("NSUbiquitousKeyValueStoreChangeReasonKey"));
    int ireason = reason.IntValue;
    NSArray changedKeys = (NSArray)userInfo.ObjectForKey(
        new NSString("NSUbiquitousKeyValueStoreChangedKeysKey"));
});
Run Code Online (Sandbox Code Playgroud)

我想出了如何将理由作为整数.但是如何将NSStrings的NSArray转换为简单的字符串[]?我以前从未使用过核心的Objective-C类包装器,抱歉.

Con*_*dev 5

希望这有助于〜使用从NSArray.ValueAt()方法返回的IntPtr来新建一个NSString并访问你所追求的值.

NSNotificationCenter.DefaultCenter.AddObserver (
    NSUbiquitousKeyValueStore.DidChangeExternallyNotification
    , delegate (NSNotification n)
{
    Console.WriteLine("Cloud notification received");
    NSDictionary userInfo = n.UserInfo;

    NSNumber reason = (NSNumber)userInfo.ObjectForKey(NSUbiquitousKeyValueStore.ChangeReasonKey);
    int ireason = reason.IntValue;
    Console.WriteLine("reason.IntValue: " + ireason);

    NSArray changedKeys = (NSArray)userInfo.ObjectForKey (NSUbiquitousKeyValueStore.ChangedKeysKey);
    var changedKeysList = new System.Collections.Generic.List<string> ();
    for (uint i = 0; i < changedKeys.Count; i++)
    {
        var key = new NSString (changedKeys.ValueAt(i));
        Console.WriteLine("changedKey IntPtr: " + changedKeys.ValueAt(i));
        Console.WriteLine("changedKey (value): " + key);

        changedKeysList.Add (key);
    }
    // now do something with the list...
});
Run Code Online (Sandbox Code Playgroud)