Skip to content
This repository was archived by the owner on Apr 29, 2024. It is now read-only.

Commit 82f7bdd

Browse files
committed
[IntroToiCloud]Adding sample for docs purposes
1 parent 9bb7c9d commit 82f7bdd

18 files changed

+915
-0
lines changed

IntroductionToiCloud/Metadata.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<SampleMetadata>
3+
<ID>DCB9360C-EE13-428A-A89E-FBE74EDC1A11</ID>
4+
<IsFullApplication>false</IsFullApplication>
5+
<Level>Intermediate</Level>
6+
<Tags>Platform Features, Cloud, Data</Tags>
7+
<SupportedPlatforms>iOS</SupportedPlatforms>
8+
<LicenseRequirement>Indie</LicenseRequirement>
9+
<Gallery>false</Gallery>
10+
<Brief>This sample shows how to use iCloud Documents.</Brief>
11+
</SampleMetadata>

IntroductionToiCloud/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Introduction to iCloud
2+
======================
3+
4+
DocPicker accompanies to the [Introduction to iCloud](http://developer.xamarin.com/guides/ios/platform_features/introduction_to_icloud/) guide in the developer portal.
5+
6+
## Setting up sample
7+
8+
To run this sample you need to have an app ID with iCloud
9+
entitlements enabled and provisioning profile linked to it.
10+
11+
Follow the steps in the [Introduction to iCloud](http://developer.xamarin.com/guides/ios/platform_features/introduction_to_icloud/#Preparing_for_iCloud_development) guide to set up your provisioning profile.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Foundation;
2+
using UIKit;
3+
4+
namespace Cloud {
5+
[Register ("AppDelegate")]
6+
public class AppDelegate : UIApplicationDelegate {
7+
UIWindow window;
8+
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
9+
{
10+
var v = new KeyValueViewController();
11+
12+
window = new UIWindow (UIScreen.MainScreen.Bounds);
13+
window.BackgroundColor = UIColor.White;
14+
window.Bounds = UIScreen.MainScreen.Bounds;
15+
window.AddSubview(v.View);
16+
window.MakeKeyAndVisible ();
17+
return true;
18+
}
19+
}
20+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>com.apple.developer.ubiquity-container-identifiers</key>
6+
<array>
7+
<string>$(TeamIdentifierPrefix)com.xamarin.samples.icloud</string>
8+
</array>
9+
<key>com.apple.developer.ubiquity-kvstore-identifier</key>
10+
<string>$(TeamIdentifierPrefix)com.xamarin.samples.icloud</string>
11+
<key>keychain-access-groups</key>
12+
<array>
13+
<string>$(AppIdentifierPrefix)com.xamarin.samples.icloud</string>
14+
</array>
15+
</dict>
16+
</plist>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDisplayName</key>
6+
<string>iCloud</string>
7+
<key>CFBundleDocumentTypes</key>
8+
<array>
9+
<dict>
10+
<key>CFBundleTypeExtensions</key>
11+
<array>
12+
<string>monkeydoc</string>
13+
</array>
14+
<key>CFBundleTypeName</key>
15+
<string>monkeydoc</string>
16+
<key>LSItemContentTypes</key>
17+
<array>
18+
<string>com.xamarin.samples.icloud.monkeydoc</string>
19+
</array>
20+
</dict>
21+
</array>
22+
<key>CFBundleIdentifier</key>
23+
<string>com.xamarin.samples.icloud</string>
24+
<key>CFBundleVersion</key>
25+
<string>1.0</string>
26+
<key>MinimumOSVersion</key>
27+
<string>5.1.1</string>
28+
<key>UIDeviceFamily</key>
29+
<array>
30+
<integer>1</integer>
31+
</array>
32+
<key>UIFileSharingEnabled</key>
33+
<false/>
34+
<key>UISupportedInterfaceOrientations</key>
35+
<array>
36+
<string>UIInterfaceOrientationPortrait</string>
37+
<string>UIInterfaceOrientationLandscapeLeft</string>
38+
<string>UIInterfaceOrientationLandscapeRight</string>
39+
</array>
40+
</dict>
41+
</plist>
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using UIKit;
2+
3+
namespace Cloud
4+
{
5+
/// <summary>Kick everything off</summary>
6+
public class Application
7+
{
8+
static void Main (string[] args)
9+
{
10+
UIApplication.Main (args, null, "AppDelegate");
11+
}
12+
}
13+
}
14+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace iCloudSample
4+
{
5+
public enum NSUbiquitousKeyValueStoreChangeReason
6+
{
7+
ServerChange = 0
8+
, InitialSyncChange = 1
9+
, QuotaViolationChange = 2
10+
}
11+
}
12+

0 commit comments

Comments
 (0)