|
| 1 | +// Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#import "FLTFirebaseCorePlugin.h" |
| 6 | +#import "UserAgent.h" |
| 7 | + |
| 8 | +#import <Firebase/Firebase.h> |
| 9 | + |
| 10 | +static NSDictionary *getDictionaryFromFIROptions(FIROptions *options) { |
| 11 | + if (!options) { |
| 12 | + return nil; |
| 13 | + } |
| 14 | + return @{ |
| 15 | + @"googleAppID" : options.googleAppID ?: [NSNull null], |
| 16 | + @"bundleID" : options.bundleID ?: [NSNull null], |
| 17 | + @"GCMSenderID" : options.GCMSenderID ?: [NSNull null], |
| 18 | + @"APIKey" : options.APIKey ?: [NSNull null], |
| 19 | + @"clientID" : options.clientID ?: [NSNull null], |
| 20 | + @"trackingID" : options.trackingID ?: [NSNull null], |
| 21 | + @"projectID" : options.projectID ?: [NSNull null], |
| 22 | + @"androidClientID" : options.androidClientID ?: [NSNull null], |
| 23 | + @"databaseUrl" : options.databaseURL ?: [NSNull null], |
| 24 | + @"storageBucket" : options.storageBucket ?: [NSNull null], |
| 25 | + @"deepLinkURLScheme" : options.deepLinkURLScheme ?: [NSNull null], |
| 26 | + }; |
| 27 | +} |
| 28 | + |
| 29 | +static NSDictionary *getDictionaryFromFIRApp(FIRApp *app) { |
| 30 | + if (!app) { |
| 31 | + return nil; |
| 32 | + } |
| 33 | + return @{@"name" : app.name, @"options" : getDictionaryFromFIROptions(app.options)}; |
| 34 | +} |
| 35 | + |
| 36 | +@implementation FLTFirebaseCorePlugin |
| 37 | ++ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar { |
| 38 | + FlutterMethodChannel *channel = |
| 39 | + [FlutterMethodChannel methodChannelWithName:@"plugins.flutter.io/firebase_core" |
| 40 | + binaryMessenger:[registrar messenger]]; |
| 41 | + FLTFirebaseCorePlugin *instance = [[FLTFirebaseCorePlugin alloc] init]; |
| 42 | + [registrar addMethodCallDelegate:instance channel:channel]; |
| 43 | + |
| 44 | + SEL sel = NSSelectorFromString(@"registerLibrary:withVersion:"); |
| 45 | + if ([FIRApp respondsToSelector:sel]) { |
| 46 | + [FIRApp performSelector:sel withObject:LIBRARY_NAME withObject:LIBRARY_VERSION]; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result { |
| 51 | + if ([@"FirebaseApp#configure" isEqualToString:call.method]) { |
| 52 | + NSString *name = call.arguments[@"name"]; |
| 53 | + NSDictionary *optionsDictionary = call.arguments[@"options"]; |
| 54 | + FIROptions *options = |
| 55 | + [[FIROptions alloc] initWithGoogleAppID:optionsDictionary[@"googleAppID"] |
| 56 | + GCMSenderID:optionsDictionary[@"GCMSenderID"]]; |
| 57 | + if (![optionsDictionary[@"bundleID"] isEqual:[NSNull null]]) |
| 58 | + options.bundleID = optionsDictionary[@"bundleID"]; |
| 59 | + if (![optionsDictionary[@"APIKey"] isEqual:[NSNull null]]) |
| 60 | + options.APIKey = optionsDictionary[@"APIKey"]; |
| 61 | + if (![optionsDictionary[@"clientID"] isEqual:[NSNull null]]) |
| 62 | + options.clientID = optionsDictionary[@"clientID"]; |
| 63 | + if (![optionsDictionary[@"trackingID"] isEqual:[NSNull null]]) |
| 64 | + options.trackingID = optionsDictionary[@"trackingID"]; |
| 65 | + if (![optionsDictionary[@"projectID"] isEqual:[NSNull null]]) |
| 66 | + options.projectID = optionsDictionary[@"projectID"]; |
| 67 | + if (![optionsDictionary[@"androidClientID"] isEqual:[NSNull null]]) |
| 68 | + options.androidClientID = optionsDictionary[@"androidClientID"]; |
| 69 | + if (![optionsDictionary[@"databaseURL"] isEqual:[NSNull null]]) |
| 70 | + options.databaseURL = optionsDictionary[@"databaseURL"]; |
| 71 | + if (![optionsDictionary[@"storageBucket"] isEqual:[NSNull null]]) |
| 72 | + options.storageBucket = optionsDictionary[@"storageBucket"]; |
| 73 | + if (![optionsDictionary[@"deepLinkURLScheme"] isEqual:[NSNull null]]) |
| 74 | + options.deepLinkURLScheme = optionsDictionary[@"deepLinkURLScheme"]; |
| 75 | + [FIRApp configureWithName:name options:options]; |
| 76 | + result(nil); |
| 77 | + } else if ([@"FirebaseApp#allApps" isEqualToString:call.method]) { |
| 78 | + NSDictionary<NSString *, FIRApp *> *allApps = [FIRApp allApps]; |
| 79 | + NSMutableArray *appsList = [NSMutableArray array]; |
| 80 | + for (NSString *name in allApps) { |
| 81 | + FIRApp *app = allApps[name]; |
| 82 | + [appsList addObject:getDictionaryFromFIRApp(app)]; |
| 83 | + } |
| 84 | + result(appsList.count > 0 ? appsList : nil); |
| 85 | + } else if ([@"FirebaseApp#appNamed" isEqualToString:call.method]) { |
| 86 | + NSString *name = call.arguments; |
| 87 | + FIRApp *app = [FIRApp appNamed:name]; |
| 88 | + result(getDictionaryFromFIRApp(app)); |
| 89 | + } else { |
| 90 | + result(FlutterMethodNotImplemented); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +@end |
0 commit comments