Hello, all:
This might sound like a Xamarin.iOS question at first, but it's actually about Xamarin Forms, so bear with me.
I am interested in adding iCloud functionality to a Xamarin Forms app I'm writing. I am new to iCloud, and have seen the Microsoft site's Xamarin documentation on incorporating iCloud to a Xamarin Forms app. But as a way of learning to trust the whole approach, I did the following first:
1) On a MacBook, with Xcode, I created a very basic native iOS Swift app that just calls iCloud and creates a simple text file, nothing else. This works just fine - I can see documents created in my iCloud account.
2) On the same MacBook, but using Visual Studio for Mac, I created the same very basic native iOS app that calls iCloud, but using C#. Again, this second effort works just fine - I can see text file documents created in my iCloud account.
I am now at a point where I'm wanting to replicate the iCloud functionality from the Xamarin.iOS project (in #2 above) into a regular Xamarin Forms project, where the code should fit into the Xamarin.iOS folder. I am now running on a Windows 10 machine with Visual Studio. I had hoped I could simply copy the code from the Xamarin.iOS standalone project (#2 above) into the full Xamarin project, the Xamarin.iOS folder.
No dice. For one thing, the standalone Xamarin.iOS project has a clearly defined view controller file of its own. The Xamarin.iOS folder inside the Xamarin Forms project doesn't have any such thing - the AppDelegate.cs file calls global::Xamarin.Forms.Forms.Init() followed by LoadApplication(new App()). If there's a ViewDidLoad in the mix, I can't find it.
Since Xamarin Forms abstracts the whole process of creating the UIViewController object under the covers, I can't find any reference to the ViewDidLoad event anywhere. This appears to be an issue because in the Xamarin.iOS standalone project, when the following code block executes in ViewDidLoad there:
ThreadPool.QueueUserWorkItem(_ => { containerUrl = NSFileManager.DefaultManager.GetUrlForUbiquityContainer(null).Append("Documents", true); Console.WriteLine(">>> containerUrl = " + containerUrl); });
...the NSFileManager.DefaultManager.GetUrlForUbiquityContainer method returns a value as I expect. In the Xamarin Forms project, where I call the same code via a DependencyService call, as this:
DependencyService.Get<ICloudHelper>().iCloudHelperInit();
...the result of the GetUrlForUbiquityContainer method is NULL. My method iCloudHelperInit uses that SAME ThreadPool code block above, but the result is nothing, and my app crashes due to a NullReferenceException.
When I stepped through the code in my Xamarin Forms version of this app, it appears the DefaultManager's folder referred to a path on my iPhone device rather than a path to iCloud.
So I suppose I have several questions here:
A.) Am I doing something wrong in Xamarin Forms that I'm not seeing a value returned from GetUrlForUbiquityContainer? Or is this a bug? Or is this a bug for Debug only? I saw a post elsewhere on another site saying that GetUrlForUbiquityContainer returns nulls running in debug mode. Again, I consistently see a non-null value returned and normal processing if it's strictly a Xamarin.iOS project, no other platforms.
B.) Is there a way to access/override the ViewDidLoad event or, for that matter, any of the other events in a UIViewController? Or is that a no-no with all the stuff that Xamarin Forms does under the covers to handle generating an iOS screen that looks native?
C.) What's the best way to get a non-null value from a call to GetUrlForUbiquityContainer?
D.) Should I consider using a .NET Standard Library to accomplish calls to iCloud from any of the three folders in my Xamarin Forms project - Xamarin.Android, Xamarin.iOS and Xamarin.UWP?
Thanks, folks!