@@ -22,17 +22,19 @@ + (NSApplication*)sharedApplication {
2222 // +sharedApplication initializes the global NSApp, so if we're delivering
2323 // something other than a FlutterApplication, warn the developer once.
2424#ifndef FLUTTER_RELEASE
25- static bool notified = false ;
26- if (!notified && ![NSApp isKindOfClass: [FlutterApplication class ]]) {
27- NSLog (@" NSApp should be of type %s , not %s . "
28- " Some application lifecycle requests (e.g. ServicesBinding.exitApplication) "
29- " and notifications will be unavailable.\n "
30- " Modify the application's NSPrincipleClass to be %s "
31- " in the Info.plist to fix this." ,
32- [[self className ] UTF8String ], [[NSApp className ] UTF8String ],
33- [[self className ] UTF8String ]);
34- notified = true ;
35- }
25+ static dispatch_once_t onceToken = 0 ;
26+ dispatch_once (&onceToken, ^{
27+ if (![app respondsToSelector: @selector (terminateApplication: )]) {
28+ NSLog (@" NSApp should be of type %s , not %s .\n "
29+ " System requests for the application to exit will not be sent to "
30+ " the Flutter framework, so the framework will be unable to cancel "
31+ " those requests.\n "
32+ " Modify the application's NSPrincipleClass to be %s in the "
33+ " Info.plist to fix this." ,
34+ [[self className ] UTF8String ], [[NSApp className ] UTF8String ],
35+ [[self className ] UTF8String ]);
36+ }
37+ });
3638#endif // !FLUTTER_RELEASE
3739 return app;
3840}
@@ -62,16 +64,17 @@ + (NSApplication*)sharedApplication {
6264// it if it is OK to terminate. When that method channel call returns with a
6365// result, the application either terminates or continues running.
6466- (void )terminate : (id )sender {
65- FlutterEngineTerminationHandler* terminationHandler =
66- [static_cast<FlutterAppDelegate*>([NSApp delegate ]) terminationHandler ];
67- if (terminationHandler) {
68- [terminationHandler requestApplicationTermination: self
69- exitType: kFlutterAppExitTypeCancelable
70- result: nil ];
71- } else {
67+ FlutterAppDelegate* delegate = [self delegate ];
68+ if (!delegate || ![delegate respondsToSelector: @selector (terminationHandler )] ||
69+ [delegate terminationHandler ] == nil ) {
7270 // If there's no termination handler, then just terminate.
7371 [super terminate: sender];
7472 }
73+ FlutterEngineTerminationHandler* terminationHandler =
74+ [static_cast<FlutterAppDelegate*>([self delegate ]) terminationHandler ];
75+ [terminationHandler requestApplicationTermination: sender
76+ exitType: kFlutterAppExitTypeCancelable
77+ result: nil ];
7578 // Return, don't exit. The application delegate is responsible for exiting on
7679 // its own by calling |-terminateApplication|.
7780}
0 commit comments