Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

OnSleep / OnResumed called by System Dialogs on iOS [Bug] #13077

Closed
Axemasta opened this issue Dec 8, 2020 · 3 comments
Closed

OnSleep / OnResumed called by System Dialogs on iOS [Bug] #13077

Axemasta opened this issue Dec 8, 2020 · 3 comments
Labels
s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. s/unverified New report that has yet to be verified t/bug 🐛

Comments

@Axemasta
Copy link

Axemasta commented Dec 8, 2020

Description

When certain iOS system dialogs appear the OnSleep method in the App is called. Events I have noticed include:

  • Permission Requests
  • Authentication Requests (ie Microsoft MSAL login)

This is making it difficult to implement a reliable background service using Xamarin Forms events whilst the app is running in the foreground.

This is the same issue as #8413 which had no reproduction so I have provided one here, although as I mention the scope of this issue is wider than just permission requests. If your app is redirected to a microsoft login for example, the OnSleep method will be called.

I came across this issue when handling the sleep / wake events for the SignalR service in my companys app. The app will connect to our SignalR client when it loads. I decided to disconnect it when the OnSleep method is called because the app is now "offline", I noticed that when logging for the first time the SignalR service was being disconnected leading me to this issue. I would not consider the app to get asleep at this time, the user is currently going through login!

Simulator Screen Shot - iPad Pro (9 7-inch) - 2020-12-08 at 11 33 16

Steps to Reproduce

  1. Add Debug/Console prints to the OnSleep / OnResume methods in App.xaml.cs
  2. Invoke a system dialog (for example a permission request via Xamarin Essentials)

Expected Behavior

OnSleep is not called because the app is in the foreground

Actual Behavior

OnSleep is called, and OnResume is called when the system dialog has completed.

Basic Information

  • Version with issue: Latest Stable (4.8.0.1687)
  • Last known good version: N/A
  • Platform Target Frameworks:
    • iOS: Latest (14.2)
  • NuGet Packages:
    • Xamarin.Forms 4.8.0.1687
    • Xamarin.Essentials 1.5.3.2

Environment

Visual Studio Professional 2019 for Mac
Version 8.8.3 (build 16)
Installation UUID: 77491835-31ec-4571-8177-452b14b3f766
	GTK+ 2.24.23 (Raleigh theme)
	Xamarin.Mac 6.18.0.23 (d16-6 / 088c73638)

	Package version: 612000107

Mono Framework MDK
Runtime:
	Mono 6.12.0.107 (2020-02/a22ed3f094e) (64-bit)
	Package version: 612000107

Roslyn (Language Service)
3.8.0-5.20519.18+4c195c3ac1974edcefa76774d7a59a2350ec55fa

NuGet
Version: 5.8.0.6860

.NET Core SDK
SDK: /usr/local/share/dotnet/sdk/5.0.100/Sdks
SDK Versions:
	5.0.100
	3.1.404
	3.1.403
	3.1.402
MSBuild SDKs: /Applications/Visual Studio.app/Contents/Resources/lib/monodevelop/bin/MSBuild/Current/bin/Sdks

.NET Core Runtime
Runtime: /usr/local/share/dotnet/dotnet
Runtime Versions:
	5.0.0
	3.1.10
	3.1.9
	3.1.8
	2.1.23
	2.1.22

.NET Core 3.1 SDK
SDK: 3.1.404

Xamarin.Profiler
Version: 1.6.15.68
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

Updater
Version: 11

Apple Developer Tools
Xcode 12.2 (17535)
Build 12B45b

Xamarin.Mac
Version: 7.0.0.15 (Visual Studio Professional)
Hash: 87a1b18d8
Branch: d16-8
Build date: 2020-11-16 21:39:40-0500

Xamarin.iOS
Version: 14.6.0.15 (Visual Studio Professional)
Hash: 87a1b18d8
Branch: d16-8
Build date: 2020-11-16 21:39:41-0500

Xamarin Designer
Version: 16.8.0.507
Hash: e87b24884
Branch: remotes/origin/d16-8
Build date: 2020-10-29 00:31:38 UTC

Xamarin.Android
Version: 11.1.0.17 (Visual Studio Professional)
Commit: xamarin-android/d16-8/c0e2b8e
Android SDK: /Users/alex.duffell/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		5.0 (API level 21)
		7.1 (API level 25)
		8.0 (API level 26)
		8.1 (API level 27)

SDK Tools Version: 26.1.1
SDK Platform Tools Version: 30.0.5
SDK Build Tools Version: 29.0.2

Build Information: 
Mono: be2226b
Java.Interop: xamarin/java.interop/d16-8@79d9533
ProGuard: Guardsquare/proguard/proguard6.2.2@ebe9000
SQLite: xamarin/sqlite/3.32.1@1a3276b
Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-8@2fb1cbc

Microsoft OpenJDK for Mobile
Java SDK: /Users/alex.duffell/Library/Developer/Xamarin/jdk/microsoft_dist_openjdk_1.8.0.25
1.8.0-25
Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

Android SDK Manager
Version: 16.8.0.32
Hash: 01a7774
Branch: remotes/origin/d16-8
Build date: 2020-10-13 23:32:30 UTC

Android Device Manager
Version: 16.8.0.45
Hash: fc0af5f
Branch: remotes/origin/d16-8
Build date: 2020-10-13 23:32:54 UTC

Build Information
Release ID: 808030016
Git revision: cc6375d2fdcc1b42532e551e24ce650eaca1ae45
Build date: 2020-11-25 13:16:20-05
Build branch: release-8.8
Xamarin extensions: cc6375d2fdcc1b42532e551e24ce650eaca1ae45

Operating System
Mac OS X 10.15.7
Darwin 19.6.0 Darwin Kernel Version 19.6.0
    Mon Aug 31 22:12:52 PDT 2020
    root:xnu-6153.141.2~1/RELEASE_X86_64 x86_64

Enabled user installed extensions
DeepClean 1.2.5
FileNesting 0.1.2
LightSwitch 💡 1.0
Code Coverage 1.0
MFractor 4.4.3

Reproduction Link

onsleepexample

I have created a reproduction repo that requests photos permission. You will need to delete the app and reinstall to get the permission dialog to show, this was easier than putting something in like Microsoft MSAL which will trigger this issue every-time.
OnSleepRepro.zip

Workaround

The workaround is ignoring the xamarin forms events and implementing your own service that is aware of the native events.

@Axemasta Axemasta added s/unverified New report that has yet to be verified t/bug 🐛 labels Dec 8, 2020
@PureWeen
Copy link
Contributor

This is a tricky one @Axemasta hartez explains the behavior in a comment here #8413 (comment)

Those application events are just tied to the native equivalents but what's definitely missing here is some better granularity. This is something that we're looking into for MAUI

But for this case it's not really a bug.

@PureWeen PureWeen added the s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. label Dec 10, 2020
@Axemasta
Copy link
Author

Hi @PureWeen yes I saw hartez's comment and am aware its difficult. I really wanted to open up the conversation about lifecycle. Hartez mentioned that the team would help suggest a workaround if these events were causing issues.

In my example my app immediately spins up a SignalR connection which I am managing in the App lifecycle methods:

protected override void OnStart()
{
    base.OnStart();

    Connect();
}

protected override void OnResume()
{
    base.OnResume();

    Connect();
}

protected override void OnSleep()
{
    base.OnSleep();

    Disconnect();
}

private async void Connect()
{
    try
    {
        await _sessionManager.StartSession();
    }
    catch (Exception ex)
    {
        _loggingManager.LogError("An exception occured connecting to starting app session", "Connect", ex);
    }
}

private void Disconnect()
{
    _sessionManager.EndSession();
}

Starting the Signalling service immediately is a requirement as it connects to our systems and allows remote users to view the device screen. The first thing my users do is authenticate with M365 / Gsuite and both logins use webview popups to handle the login. When the login starts the OnSleep method is firing because iOS has called WillResignActiviation meaning my app will now disconnect from signalling causing an unexpected state because the user is using the app and it's turning things off. My app actually handles this disconnect fairly gracefully and things like screensharing won't break because thats using a p2p connection however it creates an annoying scenario where I have to handle something that I would prefer be done by the platform.

For the time being i've commented out the OnSleep / OnResume events and I have created a separate class to raise these events from the AppDelegate (Android It does exactly the same as forms). I'm considering DidEnterBackground the sleep state for the app. Is there anything else you would suggest doing? I would think for MAUI there would be an extra method:
OnStart, OnSleep, OnResume, OnPause where OnPause would be the equivalent of OnSleep currently and OnSleep would be when the app has been hard closed by the user.

@Redth
Copy link
Member

Redth commented Jan 21, 2021

We are reviewing the cross platform lifecycle API's in Maui: dotnet/maui#30

This should help with implementing patterns like you are attempting. Your best bet for now is to continue on with your custom events in the AppDelegate.

@Redth Redth closed this as completed Jan 21, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. s/unverified New report that has yet to be verified t/bug 🐛
Projects
None yet
Development

No branches or pull requests

3 participants