maui icon indicating copy to clipboard operation
maui copied to clipboard

iOS AppDelegate - Override for PerformFetch is not available

Open AndreasReitberger opened this issue 3 years ago • 4 comments
trafficstars

Description

I'm currently migrating my Xamarin.Forms app to .NET MAUI. Now I stuck on the BackgroundFetch on iOS.

In Xamarin I was able to override the PerformFetch method

public override async void PerformFetch(UIApplication application, Action<UIBackgroundFetchResult> completionHandler)
{
    //Console.WriteLine("App is performing background fetch.");
    try
    {
        await service?.RefreshAsync(application, completionHandler);
    }
    catch(Exception exc)
    {
        EventManager.LogError(exc, forceReport: true);
    }
}

In the .NET MAUI AppDelegate, this method is not available for now.

image

I searched the docs, but the only thing I found was for Xamarin.Forms. https://docs.microsoft.com/en-us/dotnet/api/uikit.uiapplicationdelegate.performfetch?view=xamarin-ios-sdk-12

Steps to Reproduce

  1. Try to override the PerformFetch method on iOS (AppDelegate)

Version with bug

6.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

iOS

Affected platform versions

Any

Did you find any workaround?

No response

Relevant log output

No response

AndreasReitberger avatar Jun 12 '22 07:06 AndreasReitberger

From what I can tell this maybe wasn't ported. Opened a PR with what I think is the fix, but needs some feedback as I'm not really sure. Also, if this is the case, this will be added to .NET 7 at the earliest.

I think you can still implement it yourself by adding something like that method in my PR with the attribute above it. It doesn't need to be virtual then :)

jfversluis avatar Jun 13 '22 11:06 jfversluis

Perfect, thank you @jfversluis

AndreasReitberger avatar Jun 15 '22 04:06 AndreasReitberger

I believe this might be a candidate to be in the ConfigureLifecycleEvents as well? Basically,

events.AddiOS(ios => ios.PerformFetch((delegate) => RunMyBackgroundFetchCode());

edgiardina avatar Aug 14 '22 03:08 edgiardina

@jfversluis any chance that the attached PR will be merged soon? Thank you!

AndreasReitberger avatar Nov 14 '22 07:11 AndreasReitberger

Any updates? Or any workarounds?

g0dpain avatar Nov 28 '22 11:11 g0dpain

@g0dpain Can you try something like this? Notice the Export tag and the lack of override. I'm able to call PerformFetch from an iOS 6 15.4 app. Works on Asp.net 7 and iOS 16 as well.

Added this to FinishedLaunching.

UIApplication.SharedApplication.SetMinimumBackgroundFetchInterval(UIApplication.BackgroundFetchIntervalMinimum);
    [Export("application:performFetchWithCompletionHandler:")]
    public void PerformFetch(UIApplication application, Action<UIBackgroundFetchResult> completionHandler)
    {
        // Perform task

        // Inform system of fetch results
        completionHandler(UIBackgroundFetchResult.NewData);
    }

faceoffers28 avatar Nov 28 '22 16:11 faceoffers28

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

ghost avatar Feb 04 '23 02:02 ghost

It is a large amount of work for a workaround, but I've been able to somewhat work around this by using Shiny.net's background jobs

You have to use the latest alpha packages for shiny, and the documentation is lacking; however, if you use their .NET template builder , you can get something that works in both platforms. It is kind of a philosophy change, once you start using 1 or 2 things from Shiny, you start consuming all their things. I've now converted my Json Config reader, background jobs, and local notifications to Shiny's implementation.

edgiardina avatar Feb 09 '23 14:02 edgiardina