DependencyService not working (iOS 10.2, XF v2.3.3.180)

JohannDirryJohannDirry USMember ✭✭
edited January 2017 in Xamarin.Forms

Hi,
I have an issue that the DependencyService is not working in iOS, while the same works perfectly in Andorid and UWP.

Example code:

using System.IO;
using Foundation;

[assembly: Xamarin.Forms.Dependency(typeof(FontHelper))]

namespace Controls.iOS
{
    public sealed class FontHelper : IFontHelper
    {
        public string GetFontPath(string fontName)
        {
            var fileName = Path.GetFileNameWithoutExtension(fontName);
            var extension = Path.GetExtension(fontName);
            return NSBundle.MainBundle.PathForResource(fileName, extension);
        }
    }
}

When calling var fontHelper = DependencyService.Get<IFontHelper>(); I only get a null result.

Registering the service manually in AppDelegate.OnActivated(UIApplication uiApplication) did not help either. I tried this variants:

DependencyService.Register<IFontHelper, FontHelper>();
DependencyService.Register<FontHelper>();

The same problem applies to ViewRenderer, which are declared as follows:

[assembly: Xamarin.Forms.ExportRenderer(typeof(TestView), typeof(TestViewRenderer))]

namespace Controls.iOS
{
    [Preserve(AllMembers = true, Conditional = false)]
    public sealed class TestViewRenderer : ViewRenderer<TestView, UIView>
    {
        [Preserve]
        public TestViewRenderer()
        {
        }
    }
}
  • According to the documentation this can happen in UWP when using Native Compilation, but shouldn't happen in iOS.
  • The setting Linker behavior is set to Don't link.
  • A Xamarin.Forms.Forms.Init(string[] assemblies) method seems to be missing. However, the Application Output (NSLog) confirms that all necessarily assemblies got loaded.
  • A imperatively method for registering ViewRenderer and EntryRenderer seems to be missing. There is only the declarative method using the ExportRendererAttribute.
  • When resolving the renderer, I get the Xamarin-renderer for the base class. So the resolving appears to work, while the registering of custom types seems to be the problem.

I am wondering if this is a problem with the DependencyService or an mistake in the documentation.

Posts

  • JohnHardmanJohnHardman GBUniversity mod

    @JohannDirry

    Have you tried explicitly calling the following from your iOS startup code?

    DependencyService.Register<FontHelper>();
    
  • JohannDirryJohannDirry USMember ✭✭

    yes. I tried both variants already. not working.

  • JohannDirryJohannDirry USMember ✭✭

    I guess the issue might be somehow related to the Forms.Init() and Registrar.RegisterAll(Type[] attrTypes) methods. Don't know yet how to put in a debugger in there...

  • JohannDirryJohannDirry USMember ✭✭

    after initializing with DependencyService.Register<IFontHelper, FontHelper>, the key IFontHelper does exist in DependencyService.DependencyImplementations, but its value is null.

  • JohannDirryJohannDirry USMember ✭✭

    Downgrading to XF 2.3.3.175 and XF 2.3.3.168 did not help either.

  • JohannDirryJohannDirry USMember ✭✭

    Fixed it by giving the full Namespace in the typeof() declaration.

  • SyedRazaSyedRaza USMember

    Hi,
    Can you please explain this that how you resolve this issue, as I am also facing the same issue.

  • TrevorBarstow.5779TrevorBarstow.5779 USMember
    edited January 2018

    @JohannDirry said:
    Fixed it by giving the full Namespace in the typeof() declaration.

    I had a similar problem, again iOS only. JohannDirry solution did not work.
    I found that I needed some kind of reference to the assembly with the dependency class (say FontHelper) in.
    I put this in the AppDelegate .FinishLaunching override of the app e.g.

        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            global::Xamarin.Forms.Forms.Init();
            LoadApplication(new App());
    
                    var x = typeof(FontHelper);
            :
    

    My guess is that if the iOS linker does not find any usage of the library containing the dependency class in the app then it does not include the library in the executable and hence cannot be found by the dependency service.

Sign In or Register to comment.