2012-02-07 4 views
3

Под MonoTouch, я хотел бы получить доступ к NSUbiquitousKeyValueStoreDidChangeExternallyNotification (iOS5/ICloud ключ-значение въездной обновление) данные уведомления:Доступ к ICloud NSUbiquitousKeyValueStoreDidChangeExternallyNotification детали из MonoTouch

  • NSUbiquitousKeyValueStoreChangeReasonKey -> NSNumber
  • NSUbiquitousKeyValueStoreChangedKeysKey -> NSArray из NSString

Есть образец @http://docs.xamarin.com/@api/deki/files/321/=iCloud.zip, но код для доступа выше комментируется и является buggy (как он пытается преобразовать разум в целое, является неправильным). Я здесь:

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")); 
}); 

Я понял, как получить разум как целое. Но как преобразовать NSArray из NSStrings в простую строку [] ?? Мне никогда не приходилось работать с обертками типа Objective-C раньше, извините.

ответ

5

Надеюсь, что это помогает ~ использует IntPtr, возвращенный из метода NSArray.ValueAt(), чтобы обновить 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... 
}); 
+0

Thanks @CraigD. И если это поможет кому-то, вот значения перечислений, которые Вы послали: общественное перечисление NSUbiquitousKeyValueStoreChangeReason { ServerChange = 0, InitialSyncChange = 1, QuotaViolationChange = 2 } – t9mike