从 MonoTouch 访问 iCloud NSUbiquitousKeyValueStoreDidChangeExternallyNotification 详细信息
Posted
技术标签:
【中文标题】从 MonoTouch 访问 iCloud NSUbiquitousKeyValueStoreDidChangeExternallyNotification 详细信息【英文标题】:Accessing iCloud NSUbiquitousKeyValueStoreDidChangeExternallyNotification details from MonoTouch 【发布时间】:2012-02-07 04:07:46 【问题描述】:在 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"));
);
我想出了如何将原因作为整数。但是如何将 NSStrings 的 NSArray 转换为 一个简单的字符串[]??抱歉,我以前从未使用过核心的 Objective-C 类型包装器。
【问题讨论】:
【参考方案1】:希望这会有所帮助 ~ 使用从 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...
);
【讨论】:
感谢@CraigD。如果它可以帮助其他人,这里是您发送的枚举值: public enum NSUbiquitousKeyValueStoreChangeReason ServerChange = 0, InitialSyncChange = 1, QuotaViolationChange = 2以上是关于从 MonoTouch 访问 iCloud NSUbiquitousKeyValueStoreDidChangeExternallyNotification 详细信息的主要内容,如果未能解决你的问题,请参考以下文章