从MonoTouch访问iCloud NSUbiquitousKeyValueStoreDidChangeExternallyNotification详细信息

时间:2012-02-07 04:07:46

标签: ios5 xamarin.ios icloud

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

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

有一个样本@ 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"));
});

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

1 个答案:

答案 0 :(得分: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...
});
相关问题