FlutterAppDelegate.mm 6.6 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
  [super dealloc];
}

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

30 31
- (BOOL)application:(UIApplication*)application
    didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
32
  return [_lifeCycleDelegate application:application didFinishLaunchingWithOptions:launchOptions];
33 34
}

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

45
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
46 47 48 49 50 51 52 53
  [super touchesBegan:touches withEvent:event];

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

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

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

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

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

- (void)applicationWillTerminate:(UIApplication*)application {
71
  [_lifeCycleDelegate applicationWillTerminate:application];
72 73 74 75
}

- (void)application:(UIApplication*)application
    didRegisterUserNotificationSettings:(UIUserNotificationSettings*)notificationSettings {
76 77
  [_lifeCycleDelegate application:application
      didRegisterUserNotificationSettings:notificationSettings];
78
}
79

80 81
- (void)application:(UIApplication*)application
    didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
82 83
  [_lifeCycleDelegate application:application
      didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
84 85 86 87 88
}

- (void)application:(UIApplication*)application
    didReceiveRemoteNotification:(NSDictionary*)userInfo
          fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
89 90 91
  [_lifeCycleDelegate application:application
      didReceiveRemoteNotification:userInfo
            fetchCompletionHandler:completionHandler];
92 93
}

94 95 96
- (BOOL)application:(UIApplication*)application
            openURL:(NSURL*)url
            options:(NSDictionary<UIApplicationOpenURLOptionsKey, id>*)options {
97
  return [_lifeCycleDelegate application:application openURL:url options:options];
98 99
}

100
- (BOOL)application:(UIApplication*)application handleOpenURL:(NSURL*)url {
101
  return [_lifeCycleDelegate application:application handleOpenURL:url];
102 103
}

104
- (BOOL)application:(UIApplication*)application
105 106 107
              openURL:(NSURL*)url
    sourceApplication:(NSString*)sourceApplication
           annotation:(id)annotation {
108 109 110 111
  return [_lifeCycleDelegate application:application
                                 openURL:url
                       sourceApplication:sourceApplication
                              annotation:annotation];
112 113
}

114 115
- (void)application:(UIApplication*)application
    performActionForShortcutItem:(UIApplicationShortcutItem*)shortcutItem
116
               completionHandler:(void (^)(BOOL succeeded))completionHandler NS_AVAILABLE_IOS(9_0) {
117 118 119
  [_lifeCycleDelegate application:application
      performActionForShortcutItem:shortcutItem
                 completionHandler:completionHandler];
120 121
}

122 123 124
- (void)application:(UIApplication*)application
    handleEventsForBackgroundURLSession:(nonnull NSString*)identifier
                      completionHandler:(nonnull void (^)())completionHandler {
125 126 127
  [_lifeCycleDelegate application:application
      handleEventsForBackgroundURLSession:identifier
                        completionHandler:completionHandler];
128 129 130 131
}

- (void)application:(UIApplication*)application
    performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
132
  [_lifeCycleDelegate application:application performFetchWithCompletionHandler:completionHandler];
133 134
}

135 136
- (BOOL)application:(UIApplication*)application
    continueUserActivity:(NSUserActivity*)userActivity
137
      restorationHandler:(void (^)(NSArray*))restorationHandler {
138 139 140 141 142
  return [_lifeCycleDelegate application:application
                    continueUserActivity:userActivity
                      restorationHandler:restorationHandler];
}

143
#pragma mark - FlutterPluginRegistry methods. All delegating to the rootViewController
144

145
- (NSObject<FlutterPluginRegistrar>*)registrarForPlugin:(NSString*)pluginKey {
146
  UIViewController* rootViewController = _window.rootViewController;
147 148 149
  if ([rootViewController isKindOfClass:[FlutterViewController class]]) {
    return
        [[(FlutterViewController*)rootViewController pluginRegistry] registrarForPlugin:pluginKey];
150 151
  }
  return nil;
152 153
}

154
- (BOOL)hasPlugin:(NSString*)pluginKey {
155
  UIViewController* rootViewController = _window.rootViewController;
156 157
  if ([rootViewController isKindOfClass:[FlutterViewController class]]) {
    return [[(FlutterViewController*)rootViewController pluginRegistry] hasPlugin:pluginKey];
158
  }
159
  return false;
160 161
}

162
- (NSObject*)valuePublishedByPlugin:(NSString*)pluginKey {
163 164 165 166 167 168
  UIViewController* rootViewController = _window.rootViewController;
  if ([rootViewController isKindOfClass:[FlutterViewController class]]) {
    return [[(FlutterViewController*)rootViewController pluginRegistry]
        valuePublishedByPlugin:pluginKey];
  }
  return nil;
169 170
}

171
#pragma mark - FlutterAppLifeCycleProvider methods
172

173 174
- (void)addApplicationLifeCycleDelegate:(NSObject<FlutterPlugin>*)delegate {
  [_lifeCycleDelegate addDelegate:delegate];
175 176
}

C
Chinmay Garde 已提交
177
@end