1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using CoreGraphics ;
4
+ using System . Linq ;
5
+ using UIKit ;
6
+ using Foundation ;
7
+
8
+ namespace Cloud
9
+ {
10
+ /// <summary>
11
+ /// View containing Buttons and TextView to show off the samples
12
+ /// </summary>
13
+ public class KeyValueViewController : UIViewController
14
+ {
15
+ UIButton saveButton , reloadButton , clearButton ;
16
+ UITextView localText , testText , outputText ;
17
+ UILabel localLabel , testLabel , keyLabel , valueLabel ;
18
+
19
+ string sharedKeyName = "Shared" ;
20
+
21
+ public override void ViewDidLoad ( )
22
+ {
23
+ base . ViewDidLoad ( ) ;
24
+
25
+ #region UI Layout
26
+ // Create the buttons and TextView to run the sample code
27
+ saveButton = UIButton . FromType ( UIButtonType . RoundedRect ) ;
28
+ saveButton . Frame = new CGRect ( 10 , 120 , 90 , 50 ) ;
29
+ saveButton . SetTitle ( "Save" , UIControlState . Normal ) ;
30
+
31
+ reloadButton = UIButton . FromType ( UIButtonType . RoundedRect ) ;
32
+ reloadButton . Frame = new CGRect ( 110 , 120 , 90 , 50 ) ;
33
+ reloadButton . SetTitle ( "Reload" , UIControlState . Normal ) ;
34
+
35
+ clearButton = UIButton . FromType ( UIButtonType . RoundedRect ) ;
36
+ clearButton . Frame = new CGRect ( 210 , 120 , 90 , 50 ) ;
37
+ clearButton . SetTitle ( "Clear" , UIControlState . Normal ) ;
38
+
39
+ outputText = new UITextView ( new CGRect ( 10 , 180 , 300 , 270 ) ) ;
40
+ //outputText.BackgroundColor = UIColor.FromRGB(224,255,255);
41
+ outputText . Editable = false ;
42
+ outputText . ScrollEnabled = true ;
43
+
44
+ localText = new UITextView ( new CGRect ( 130 , 40 , 150 , 30 ) ) ;
45
+ localText . BackgroundColor = UIColor . FromRGB ( 224 , 255 , 255 ) ;
46
+ testText = new UITextView ( new CGRect ( 130 , 80 , 150 , 30 ) ) ;
47
+ testText . BackgroundColor = UIColor . FromRGB ( 224 , 255 , 255 ) ;
48
+
49
+ localLabel = new UILabel ( new CGRect ( 10 , 40 , 100 , 30 ) ) ;
50
+ localLabel . Text = UIDevice . CurrentDevice . Name + ":" ;
51
+ testLabel = new UILabel ( new CGRect ( 10 , 80 , 100 , 30 ) ) ;
52
+ testLabel . Text = "Shared:" ;
53
+ keyLabel = new UILabel ( new CGRect ( 10 , 10 , 200 , 30 ) ) ;
54
+ keyLabel . Text = "KEY" ;
55
+ valueLabel = new UILabel ( new CGRect ( 150 , 10 , 200 , 30 ) ) ;
56
+ valueLabel . Text = "VALUE" ;
57
+
58
+ // Add the controls to the view
59
+ this . Add ( saveButton ) ;
60
+ this . Add ( reloadButton ) ;
61
+ this . Add ( clearButton ) ;
62
+ this . Add ( outputText ) ;
63
+ Add ( testText ) ;
64
+ Add ( localText ) ;
65
+ Add ( localLabel ) ;
66
+ Add ( testLabel ) ;
67
+ Add ( keyLabel ) ;
68
+ Add ( valueLabel ) ;
69
+ #endregion
70
+
71
+ // Wire up the buttons to the SamplCode class methods
72
+ saveButton . TouchUpInside += Save ;
73
+
74
+ reloadButton . TouchUpInside += Reload ;
75
+
76
+ clearButton . TouchUpInside += Clear ;
77
+ }
78
+
79
+ // saves the inputs to iCloud keys
80
+ void Save ( object sender , EventArgs ea ) {
81
+ var store = NSUbiquitousKeyValueStore . DefaultStore ;
82
+ store . SetString ( UIDevice . CurrentDevice . Name , localText . Text ) ;
83
+ store . SetString ( sharedKeyName , testText . Text ) ;
84
+ var synchronized = store . Synchronize ( ) ;
85
+ outputText . Text += String . Format ( "\n --- Local save ({4}) ---\n {0}: {1}\n {2}: {3}"
86
+ , UIDevice . CurrentDevice . Name , localText . Text
87
+ , sharedKeyName , testText . Text
88
+ , synchronized ) ;
89
+
90
+ localText . ResignFirstResponder ( ) ;
91
+ testText . ResignFirstResponder ( ) ;
92
+ }
93
+ // loads the inputs from iCloud keys
94
+ void Reload ( object sender , EventArgs ea ) {
95
+ var store = NSUbiquitousKeyValueStore . DefaultStore ;
96
+ var synchronized = store . Synchronize ( ) ;
97
+
98
+ testText . Text = store . GetString ( sharedKeyName ) ;
99
+ localText . Text = store . GetString ( UIDevice . CurrentDevice . Name ) ;
100
+
101
+ outputText . Text += String . Format ( "\n --- Reload ({4}) ---\n {0}: {1}\n {2}: {3}"
102
+ , UIDevice . CurrentDevice . Name , localText . Text
103
+ , sharedKeyName , testText . Text
104
+ , synchronized ) ;
105
+
106
+ localText . ResignFirstResponder ( ) ;
107
+ testText . ResignFirstResponder ( ) ;
108
+ }
109
+ // clears the iCloud keys
110
+ void Clear ( object sender , EventArgs e ) {
111
+ var store = NSUbiquitousKeyValueStore . DefaultStore ;
112
+ store . Remove ( UIDevice . CurrentDevice . Name ) ;
113
+ store . Remove ( sharedKeyName ) ;
114
+
115
+ var synchronized = store . Synchronize ( ) ;
116
+ outputText . Text += String . Format ( "\n --- Clear ({4}) ---\n {0}: {1}\n {2}: {3}"
117
+ , UIDevice . CurrentDevice . Name , "<cleared>"
118
+ , sharedKeyName , "<cleared>"
119
+ , synchronized ) ;
120
+ localText . ResignFirstResponder ( ) ;
121
+ testText . ResignFirstResponder ( ) ;
122
+ }
123
+
124
+ // notification when Key-Value changes are triggered by server
125
+ NSObject keyValueNotification ;
126
+
127
+ // register for the notification when iCloud keys are changed
128
+ public override void ViewWillAppear ( bool animated )
129
+ {
130
+ base . ViewWillAppear ( animated ) ;
131
+
132
+ Reload ( null , null ) ;
133
+
134
+ keyValueNotification =
135
+ NSNotificationCenter . DefaultCenter . AddObserver (
136
+ NSUbiquitousKeyValueStore . DidChangeExternallyNotification
137
+ , delegate ( NSNotification n )
138
+ {
139
+ Console . WriteLine ( "Cloud notification received" ) ;
140
+ NSDictionary userInfo = n . UserInfo ;
141
+
142
+ NSNumber reasonNumber = ( NSNumber ) userInfo . ObjectForKey ( NSUbiquitousKeyValueStore . ChangeReasonKey ) ;
143
+ nint reason = reasonNumber . NIntValue ;
144
+
145
+ //Console.WriteLine("reason.IntValue: " + ireason);
146
+ //Console.WriteLine("reason (enum): " + (NSUbiquitousKeyValueStoreChangeReason)ireason);
147
+
148
+ NSArray changedKeys = ( NSArray ) userInfo . ObjectForKey ( NSUbiquitousKeyValueStore . ChangedKeysKey ) ;
149
+ var changedKeysList = new List < string > ( ) ;
150
+ for ( uint i = 0 ; i < changedKeys . Count ; i ++ )
151
+ {
152
+ var key = changedKeys . GetItem < NSString > ( i ) ; // resolve key to a string
153
+
154
+ //Console.WriteLine("changedKey IntPtr: " + changedKeys.ValueAt(i));
155
+ //Console.WriteLine("changedKey (value): " + key);
156
+
157
+ changedKeysList . Add ( key ) ;
158
+ }
159
+
160
+ var store = NSUbiquitousKeyValueStore . DefaultStore ;
161
+ store . Synchronize ( ) ;
162
+ // now do something with the list...
163
+ InvokeOnMainThread ( ( ) => {
164
+ outputText . Text += "\n --- Cloud Notification \uE049 \uE049 \uE049 ---" ;
165
+ foreach ( var k in changedKeysList ) {
166
+ outputText . Text += String . Format ( "\n {0}: {1}" , k , store . GetString ( k ) ) ;
167
+ }
168
+ testText . Text = store . GetString ( sharedKeyName ) ;
169
+ localText . Text = store . GetString ( UIDevice . CurrentDevice . Name ) ;
170
+ } ) ;
171
+ } ) ;
172
+ }
173
+
174
+ // remove notification observer
175
+ public override void ViewWillDisappear ( bool animated )
176
+ {
177
+ base . ViewWillDisappear ( animated ) ;
178
+ if ( keyValueNotification != null )
179
+ NSNotificationCenter . DefaultCenter . RemoveObserver ( keyValueNotification ) ;
180
+ }
181
+ }
182
+ }
0 commit comments