FlutterAppDelegate.mm 6.4 KB
Newer Older
C
Chinmay Garde 已提交
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
#include "flutter/shell/platform/darwin/ios/framework/Headers/FlutterAppDelegate.h"
6
#include "flutter/shell/platform/darwin/ios/framework/Headers/FlutterPluginAppLifeCycleDelegate.h"
7
#include "flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h"
8

9
@implementation FlutterAppDelegate {
10
  FlutterPluginAppLifeCycleDelegate* _lifeCycleDelegate;
11
}
C
Chinmay Garde 已提交
12

13 14
- (instancetype)init {
  if (self = [super init]) {
15
    _lifeCycleDelegate = [[FlutterPluginAppLifeCycleDelegate alloc] init];
16 17 18 19 20
  }
  return self;
}

- (void)dealloc {
21
  [_lifeCycleDelegate release];
22 23 24 25 26
  [super dealloc];
}

- (BOOL)application:(UIApplication*)application
    didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
27
  return [_lifeCycleDelegate application:application didFinishLaunchingWithOptions:launchOptions];
28 29
}

30 31 32
// Returns the key window's rootViewController, if it's a FlutterViewController.
// Otherwise, returns nil.
- (FlutterViewController*)rootFlutterViewController {
33
  UIViewController* viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
34 35 36 37 38 39
  if ([viewController isKindOfClass:[FlutterViewController class]]) {
    return (FlutterViewController*)viewController;
  }
  return nil;
}

40
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
41 42 43 44 45 46 47 48
  [super touchesBegan:touches withEvent:event];

  // Pass status bar taps to key window Flutter rootViewController.
  if (self.rootFlutterViewController != nil) {
    [self.rootFlutterViewController handleStatusBarTouches:event];
  }
}

49
- (void)applicationDidEnterBackground:(UIApplication*)application {
50
  [_lifeCycleDelegate applicationDidEnterBackground:application];
51 52
}

53
- (void)applicationWillEnterForeground:(UIApplication*)application {
54
  [_lifeCycleDelegate applicationWillEnterForeground:application];
55 56 57
}

- (void)applicationWillResignActive:(UIApplication*)application {
58
  [_lifeCycleDelegate applicationWillResignActive:application];
59 60 61
}

- (void)applicationDidBecomeActive:(UIApplication*)application {
62
  [_lifeCycleDelegate applicationDidBecomeActive:application];
63 64 65
}

- (void)applicationWillTerminate:(UIApplication*)application {
66
  [_lifeCycleDelegate applicationWillTerminate:application];
67 68 69 70
}

- (void)application:(UIApplication*)application
    didRegisterUserNotificationSettings:(UIUserNotificationSettings*)notificationSettings {
71 72
  [_lifeCycleDelegate application:application
      didRegisterUserNotificationSettings:notificationSettings];
73
}
74

75 76
- (void)application:(UIApplication*)application
    didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
77 78
  [_lifeCycleDelegate application:application
      didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
79 80 81 82 83
}

- (void)application:(UIApplication*)application
    didReceiveRemoteNotification:(NSDictionary*)userInfo
          fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
84 85 86
  [_lifeCycleDelegate application:application
      didReceiveRemoteNotification:userInfo
            fetchCompletionHandler:completionHandler];
87 88
}

89 90 91
- (BOOL)application:(UIApplication*)application
            openURL:(NSURL*)url
            options:(NSDictionary<UIApplicationOpenURLOptionsKey, id>*)options {
92
  return [_lifeCycleDelegate application:application openURL:url options:options];
93 94
}

95
- (BOOL)application:(UIApplication*)application handleOpenURL:(NSURL*)url {
96
  return [_lifeCycleDelegate application:application handleOpenURL:url];
97 98
}

99
- (BOOL)application:(UIApplication*)application
100 101 102
              openURL:(NSURL*)url
    sourceApplication:(NSString*)sourceApplication
           annotation:(id)annotation {
103 104 105 106
  return [_lifeCycleDelegate application:application
                                 openURL:url
                       sourceApplication:sourceApplication
                              annotation:annotation];
107 108
}

109 110
- (void)application:(UIApplication*)application
    performActionForShortcutItem:(UIApplicationShortcutItem*)shortcutItem
111
               completionHandler:(void (^)(BOOL succeeded))completionHandler NS_AVAILABLE_IOS(9_0) {
112 113 114
  [_lifeCycleDelegate application:application
      performActionForShortcutItem:shortcutItem
                 completionHandler:completionHandler];
115 116
}

117 118 119
- (void)application:(UIApplication*)application
    handleEventsForBackgroundURLSession:(nonnull NSString*)identifier
                      completionHandler:(nonnull void (^)())completionHandler {
120 121 122
  [_lifeCycleDelegate application:application
      handleEventsForBackgroundURLSession:identifier
                        completionHandler:completionHandler];
123 124 125 126
}

- (void)application:(UIApplication*)application
    performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
127
  [_lifeCycleDelegate application:application performFetchWithCompletionHandler:completionHandler];
128 129
}

130 131
- (BOOL)application:(UIApplication*)application
    continueUserActivity:(NSUserActivity*)userActivity
132
      restorationHandler:(void (^)(NSArray*))restorationHandler {
133 134 135 136 137
  return [_lifeCycleDelegate application:application
                    continueUserActivity:userActivity
                      restorationHandler:restorationHandler];
}

138
#pragma mark - FlutterPluginRegistry methods. All delegating to the rootViewController
139

140
- (NSObject<FlutterPluginRegistrar>*)registrarForPlugin:(NSString*)pluginKey {
141
  UIViewController* rootViewController = _window.rootViewController;
142 143 144
  if ([rootViewController isKindOfClass:[FlutterViewController class]]) {
    return
        [[(FlutterViewController*)rootViewController pluginRegistry] registrarForPlugin:pluginKey];
145 146
  }
  return nil;
147 148
}

149
- (BOOL)hasPlugin:(NSString*)pluginKey {
150
  UIViewController* rootViewController = _window.rootViewController;
151 152
  if ([rootViewController isKindOfClass:[FlutterViewController class]]) {
    return [[(FlutterViewController*)rootViewController pluginRegistry] hasPlugin:pluginKey];
153
  }
154
  return false;
155 156
}

157
- (NSObject*)valuePublishedByPlugin:(NSString*)pluginKey {
158 159 160 161 162 163
  UIViewController* rootViewController = _window.rootViewController;
  if ([rootViewController isKindOfClass:[FlutterViewController class]]) {
    return [[(FlutterViewController*)rootViewController pluginRegistry]
        valuePublishedByPlugin:pluginKey];
  }
  return nil;
164 165
}

166
#pragma mark - FlutterAppLifeCycleProvider methods
167

168 169
- (void)addApplicationLifeCycleDelegate:(NSObject<FlutterPlugin>*)delegate {
  [_lifeCycleDelegate addDelegate:delegate];
170 171
}

C
Chinmay Garde 已提交
172
@end