Xamarin.Forms for iOS using SceneDelegate.cs - xamarin.forms

In iOS 13, UIScene is used. Codes in AppDelegate.cs must be moved into SceneDelegate.cs to support multiple windows of the same app in Split View.
For Xamarin.Forms, AppDelegate.cs uses LoadApplication (new App()) to launch an instance of App.cs in the Xamarin.Forms. LoadApplication is found in Xamarin.Forms.Platform.iOS.FormsApplicationDelegate.
What is the equivalent in SceneDelegate.cs to launch an instance of App.cs in the Xamarin.Forms?

From the app lifecycle of xamrin forms :
iOS – Main method > AppDelegate > App > ContentPage .
Android – MainActivity > App > ContentPage
We will see that Main method invoke App class before , if need a instance of App from iOS , generally will try as follow :
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App(app)); // pass app to Forms
return base.FinishedLaunching(app, options);
}
However ,Forms can not using UIKit (error screenshot).
In xamarin forms , there is DependencyService to load navtive method . Therefore, suggest that using DependecyService to call app from iOS native AppDelegate.cs .
About using SceneDelegate.cs in Xamarin Forms , there is no SceneDelegate.cs file in iOS solution now . I will check that whether be possible in Xamarin Forms .
==================================Update==============================
If want to deal with a universal link in AppDelegate.cs , you need to do something in continueUserActivity method as follow :
public override bool ContinueUserActivity(UIApplication application, NSUserActivity userActivity, UIApplicationRestorationHandler completionHandler)
{
//return base.ContinueUserActivity(application, userActivity, completionHandler);
if(userActivity.ActivityType == NSUserActivityType.BrowsingWeb)
{
NSUrl url = userActivity.WebPageUrl;
// other code
}
return true;
}
==================================Update===============================
Finally , found that it is possible to add SceneDelegate to a Xamarin Forms project. A new Xamarin Forms project does not come with the necessary SceneDelegate.cs or .storyboard files, so these need to be added. After adding these files, the info.plist needs to be updated with the UIApplicationSceneManifest key, which will contain more needed keys.
The additions to info.plist are shown here: https://learn.microsoft.com/en-us/xamarin/ios/platform/ios13/multi-window-ipad#project-configuration (just UIApplicationSceneManifest and everything under)
The two things to note are that:
The sample has issues with navigation working properly when having multiple windows of the app running.
This is not an official sample, as Xamarin.Forms does not currently offer official support for using mutiple Scenes with an iOS application.
The unofficial Xamarin.Forms sample is here:https://www.dropbox.com/s/sdxq5me7vcdmuf9/XamFormsiOSMultiWindow.rar?dl=0

It seems this issue has been resolved in MAUI. Just inherit from MauiUISceneDelegate for your SceneDelegate class and the framework will take car of the rest:
[Register("SceneDelegate")]
public class SceneDelegate : MauiUISceneDelegate
{
}
And then in your info.plist file:
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
<true/>
<key>UISceneConfigurations</key>
<dict>
<key>UIWindowSceneSessionRoleApplication</key>
<array>
<dict>
<key>UISceneConfigurationName</key>
<string>__MAUI_DEFAULT_SCENE_CONFIGURATION__</string>
<key>UISceneDelegateClassName</key>
<string>SceneDelegate</string>
</dict>
</array>
</dict>
</dict>
I wish there was an equally clean solution for Xamarin.Forms as we have not migrated our app to MAUI yet.
Hope this helps!
*Update:
Junior Jiang's solution did work for me, but I had to make a few changes to get it to work:
Uncomment the following lines in SceneDelegate.cs:
//var ad = new AppDelegate();
//ad.GetUI();
...
//Window.RootViewController = ad.Window.RootViewController;
Then add the following line after the uncommented code in SceneDelegate.cs
Window.MakeKeyAndVisible();
I made a few additional changes that are not specifically required, but probably a bit better in terms of resource usage, such as rather resolving appDeleggate as follows:
var appDelegate = (AppDelegate)UIApplication.SharedApplication.Delegate;
And instead returning the created Xamarin.Forms app in GetUI method (in AppDelegate) to be used in SceneDelegate, instead of creating another one in SceneDelegate.
One limitation that I did notice is that I could no longer see iOS alert messages, so I used a custom syncfusion popup instead.

Related

Can't get Prism 8 with DryIoc RegisterServices and Platform-Specific support to work

Trying to use Prism 8, ContainerLocator, IHttpClientFactory and Platform-Specific Service registration with DryIoc Extensions (not Magician) in a Xamarin Forms application
I have these nuget packages installed
Prism.DryIoc.Extensions
Prism.Forms
My main Xamarin Application inherits from PrismApplicationBase.
In platform code (eg Android) I have IPlatformInitializer implented by MainActivity and for platform-specific services I use:
LoadApplication(new App(this));
as documented here:
https://prismlibrary.com/docs/dependency-injection/platform-specific-services.html
This line causes the IContainerExtension to be resolved in the shared code
protected override IContainerExtension CreateContainerExtension()
...but I can't find a way to successfully return a valid IContainerExtension implementation.
I've attempted:
https://prismlibrary.com/docs/dependency-injection/container-locator.html
var createContainerExtension = () => new DryIocContainerExtension();
ContainerLocator.SetContainerExtension(createContainerExtension);
But the code given here can't even compile - the DryIocContainerExtension class created is defined INTERNAL and isn't available to my application code.
If I use...
PrismContainerExtension.Init();
or
return ContainerLocator.Current;
...as worked previously (eg Prism 7.2) I get a runtime error as described here:
https://githubmemory.com/repo/dansiegel/Prism.Container.Extensions/issues/180
ValueFactory attempted to access the Value property of this instance.
Please can someone advise what I'm doing wrong, or do I have to get my company to pay for Magician to resolve my issue?

Send data from Share Extension to Xamatin.Forms app

I work on Share Extension. And it only has a sample project (following link) and a general extension documentation.
Basic project:
Extension documentation:
When I click share with my app on iphone (for example on photos), I want to send this file to my app and upload it remote server. But I couldn't find anyway to send object from share extension. I looked up and probably I need to use "NSUserDefaults" and I tried to use it that:
I did necessary settings (creating group id, app id etc) on my apple developer account.
I did necessary settings on my project Entitlements.plist
I try to send on ShareViewController - DidSelectPost method in the my project's share extension as below:
var default = new NSUserDefaults ("group.com.myapp.AppName", NSUserDefaultsType.SuiteName);
default.SetString ("Test content", "TestKey");
default.Synchronize ();
4)I try to receive string data on my main project's AppDelegate as below:
var default = new NSUserDefaults ("group.com.myapp.AppName", NSUserDefaultsType.SuiteName);
var key = default.StringForKey ("TestKey");
string value = key;
But I couldn't do it. And I saw someone did this with OpenUrl function. I tried this and this didn't work.
I started mobile programming with Xamarin forms and I don't know enough native iOS programming (I only learn with Xamarin). I think share extension has insufficient documentation.

What's the proper way of setting up MvvmCross 6.0 Xamarin.Forms UWP application code?

I have my UWP Application inherited from Base class, which inherits from MvxApplication<Setup, CoreApp>:
public sealed partial class App : WindowsApplication
{
public App()
{
InitializeComponent();
}
}
public class WindowsApplication : MvxApplication<Setup, CoreApp>
{
}
public class Setup : MvxWindowsSetup<CoreApp>
{
public override IEnumerable<Assembly> GetViewAssemblies()
{
// need to do this as otherwise I receive the message that corresponding view to view model is not found
var assemblies = base.GetViewAssemblies().ToList();
assemblies.Add(typeof(Forms.App).Assembly);
return assemblies;
}
}
However, when launching it, receiving the following error message:
The type MvxContentPagePresentationAttribute is not configured in the
presenter dictionary
As I understand, all that is not proper way to launch Xamarin.Forms MvvmCross application, as UWP App and Setup should be inherited from something like MvxFormsApplication and MvxFormsWindowsSetup<CoreApp, Forms.App> respectively (to have Xamarin.Forms app properly initialized).
But:
MvxFormsApplication is not generic and doesn't provide ability of passing Forms-generic setup.
even if I inherit the App from MvxFormsApplication and use this.RegisterSetupType<MvxFormsWindowsSetupInheritor>();, Visual Studio compiler never allows me to compile the project because of some weird error message (something like The name “WindowsApplication” does not exist in the namespace “…”) (this might be some issue of Visual Studio, but I have VS 15.7 version, which expects the code to work (again, MvvmCross declares they support UWP and XF)).
So, from my understanding, if there is Xamarin.Forms app, there must be also some way of passing actually Xamarin.Forms App class to the UWP App class initialization.
MvvmCross, again, stands for UWP and Xamarin.Forms support, but I can't see any clear example of the way to setup such type of application.
MvvmCross documentation as always is quite "modest". There are some instructions about setting up MvvmCross UWP app as well as setting up MvvmCross XF iOS/Android, but the only word about MvvmCross XF UWP is:
You are now free to place your custom renderers in a different
assembly. All you have to do to make it work is to add your assembly
to the Setup.ViewAssemblies collection.
(in official website docs)
(which is still sounds weird, as iOS and Android versions don't need that additional code, which makes me think that such (current) documentation isn't quite actualized)
and
UWP, WPF
Extend App from MvxApplication. ( App : MvxApplication { } )
from MvvmCross.Forms package readme.txt file, when all other platforms, again, expect inheritance for the app classes from MvxForms*-based ones.
MvvmCross guys, any thoughts on that?
When I set up a new Xamarin.Forms project, I always follow the Playground sample in the MvvmCross GitHub as this example evolves along with the API and is always up-to-date, as it is part of the MvvmCross solution, so any commits need to preserve its functionality. So if you want to see how everything should look in a minimal UWP + Xamarin.Forms project see the Playground.Forms.UI and Playground.Forms.Uwp projects in the linked folder.

Xamarin Forms IEventAggregator in iOS class crash the application

I'm using Xamarin.Forms and Prism 6.3.0
I have a class in the iOS project where I want to use the IEventAggregator. When I'm passing the IEventAggregator as a parameter to the constructor of the class, the application crashes.
A clarification, I'm trying to publish from the iOS class, not to subscribe to an event published by Xamarin.Forms.
How can I use the IEventAggregator inside a class in iOS project of Xamarin.Forms?
I don't know if there is a more elegant solution, but there is one that is working. You'll have to add the dependency to your container manually with the correct injection parameters.
In your platform specific project (iOS in this case, but it holds for Android, too) change the following line
this.LoadApplication(new App());
to
var app = new App();
this.LoadApplication();
Given your App is a PrismApplication you can now access the IUnityContainer and register your type manually
var app = new App();
app.Container.RegisterType(typeof(IServiceService),
typeof(MyServiceService),
string.Empty,
new ContainerControlledLifetimeManager(),
new InjectionConstructor(new ResolvedParameter(typeof(IEventAggregator))));
this.LoadApplication(app);
Now Unity should be able to resolve MyServiceService correctly and your app should not crash anymore. Maybe you have to remove
[assembly: Xamarin.Forms.Dependency(typeof(MyServiceService)]
but I have not tried if it crashed if it's not removed.
The answer to the question is here
In short, in the class in the iOS project add the using statement using Microsoft.Practices.Unity; and in the constractor of the class add
var ea = ((App)Xamarin.Forms.Application.Current).Container.Resolve<IEventAggregator>();
in order to get the EventAggregator.
Then you can use it to publish a message i.e.
ea.GetEvent<SomeEvent>().Publish(somePayload);

Xamarin Forms MediaManager Source property

I am new to Xamarin and Xamarin Forms. I am using the Sample form MediaManager https://github.com/martijn00/XamarinMediaManager. It works with the sample url, but I can't get it to work with a local resource for either iOS or Android.
I've only tested this in iOS so far but place the file in your Resources directory.
Then use this code to play the file.
iOS
await CrossMediaManager.Current.Play("myfile.mp3", MediaFileType.Audio, ResourceAvailability.Local);
Android:
Place file in Resource/raw folder.
Do not add file extension to name.
await CrossMediaManager.Current.Play("android.resource://[PackageName]/raw/myfile", MediaFileType.Audio, ResourceAvailability.Local);

Resources