Firebase帮助 - 多个数据库

时间:2016-11-11 18:28:02

标签: firebase firebase-realtime-database

我在Objective C中的iOS应用中遇到了很多Firebase问题。

该应用使用Firebase作为自定义登录数据库。

该应用最初连接到包含用户名和密码的firebase数据库。

我已经引入了另一个与firebase的连接,以便将数据实时检索到tableView中。这是一个单独的firebase项目。我不确定如何将两个数据库作为一个项目的一部分。

我添加了GoogleService-Info.plist,它只允许一个DATABASE_URL。是否有可能有多个DATABASE_URL?我想在必要时在项目之间切换。

登录代码基于firebase 1.0版。该应用通过“网络服务”连接到firebase。网址。

我已经更新到Firebase的最新版本,现在代码已经破了。我更新了一些已弃用的引用并清除了大部分错误。

我在运行应用程序并尝试登录时遇到错误。登录时没有与firebase项目的url的连接。

我将在下面添加登录功能的代码。

- (IBAction)login:(id)sender
{
    if (txtEmail.text.length > 0 && txtToken.text.length > 0) {
        if ([sharedHelper isConnectedToInternet]) {
            [self.activityView startAnimating];

            //If the user has purchased but not logged in, when they do login we want to add their purchases to their user
            NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];

            [settings setObject:txtEmail.text forKey:@"userEmail"];
            [settings setObject:txtToken.text forKey:@"userToken"];

            NSMutableDictionary *purchase = [settings objectForKey:@"UnRecordedPurchase"];
            NSString *subscriptionLength = @"0";
            NSString *expiryDate = @"";
            NSString *receiptData = @"";
            if (purchase)
            {
                subscriptionLength = [purchase objectForKey:@"subscriptionLength"];
                expiryDate = [purchase objectForKey:@"expiryDate"];
                receiptData = [purchase objectForKey:@"receiptData"];
            }
            [settings setObject:nil forKey:@"UnRecordedPurchase"];
            [settings synchronize];

            //Firebase *firebase = [[Firebase alloc] initWithUrl:kLOGINWEBSERVICEURL];
            FIRDatabaseReference *firebase= [[FIRDatabase database] reference];

            [firebase observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
                [self.activityView stopAnimating];

                if (snapshot.value == [NSNull null]) {
                    // No data found
                    NSString *errorMessage = @"Unable to check login details - please contact support@ambay.com";
                    [self showStatusMessage:errorMessage];

                } else {
                    NSMutableArray *matchingEmailChildren = [[NSMutableArray alloc] initWithCapacity:0];

                    for (FIRDataSnapshot *child in snapshot.children) {
                        NSDictionary *dict = child.value;

                        // Check the email address

                        if ([[[dict valueForKey:@"Email"] lowercaseString] isEqualToString:[self.txtEmail.text lowercaseString]]) {
                            [matchingEmailChildren addObject:child];
                        }
                    }

                    if ([matchingEmailChildren count] > 0) {
                        NSMutableArray *matchingTokenChildren = [[NSMutableArray alloc] initWithCapacity:0];

                        // Check for matching token
                        for (FIRDataSnapshot *child in matchingEmailChildren) {
                            NSDictionary *dict = child.value;

                            if ([[[dict valueForKey:@"Token"] lowercaseString] isEqualToString:[self.txtToken.text lowercaseString]]) {

                                [matchingTokenChildren addObject:child];
                            }
                        }

                        if ([matchingTokenChildren count] > 0) {
                            BOOL loginSuccess = NO;

                            // Check expiry date
                            for (FIRDataSnapshot *child in matchingTokenChildren) {
                                NSDictionary *dict = child.value;

                                // Get today's date, just as the date without time values
                                NSCalendar *calendar = [NSCalendar currentCalendar];
                                NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay
                                                                           fromDate:[NSDate date]];
                                [components setHour:12];
                                [components setMinute:0];
                                [components setSecond:0];

                                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                                [dateFormatter setDateFormat:@"dd/MM/yyyy"];

                                NSDate *todaysDate = [calendar dateFromComponents:components];

                                // Format expiry date to just the date component

                                components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay
                                                         fromDate:[dateFormatter dateFromString:[dict valueForKey:@"ExpiryDate"]]];

                                [components setHour:12];
                                [components setMinute:0];
                                [components setSecond:0];

                                NSDate *expiryDate = [calendar dateFromComponents:components];

                                if ([expiryDate compare:todaysDate] == NSOrderedSame || [expiryDate compare:todaysDate] == NSOrderedDescending) {

                                    NSMutableDictionary *userDict = [[NSMutableDictionary alloc] init];
                                    [userDict setObject:self.txtEmail.text forKey:@"Username"];
                                    [userDict setObject:self.txtToken.text forKey:@"Token"];

                                    // Change format of expiry date for compatibility purposes and convert to string to save locally
                                    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                                    [dateFormatter setDateFormat:@"yyyyMMdd"];
                                    NSString *expiryDateAsString = [dateFormatter stringFromDate:expiryDate];

                                    [userDict setObject:expiryDateAsString forKey:@"ExpiryDate"];

                                    [sharedHelper addUserWithDictionary:userDict fromCloud:NO];

                                    [self loginSuccess];

                                    loginSuccess = YES;

                                    break;
                                }
                            }

                            if (loginSuccess == NO) {
                                NSString *errorMessage = @"Your Annual Subscription has expired. Contact support@ambay.com";
                                [self showStatusMessage:errorMessage];
                            }

                        } else {
                            NSString *errorMessage = @"Your Code is incorrect for this email address.";
                            [self showStatusMessage:errorMessage];

                            [self.txtToken becomeFirstResponder];
                        }

                    } else {
                        NSString *errorMessage = @"We cannot find your email address on our system.\r\n\r\n For assistance contact support@ambay.com";
                        [self showStatusMessage:errorMessage];

                        [self.txtEmail becomeFirstResponder];
                    }
                }

            } withCancelBlock:^(NSError *error) {
                NSString *errorMessage = @"Unable to check login details - please contact support@ambay.com";
                [self showStatusMessage:errorMessage];
            }];

        } else {
            NSString *errorMessage = @"Unable to validate email & code details. Please check your internet connection.";
            [self showStatusMessage:errorMessage];
        }
    } else {
        NSString *errorMessage = @"Please enter email address and code.";
        [self showStatusMessage:errorMessage];
    }
}

请指出上述代码中的任何错误。

由于 WG

1 个答案:

答案 0 :(得分:0)

如果您只需要一个额外的数据库,那么您应该查看Firebase Auth。它允许使用电子邮件和密码以及Google,Facebook,Twitter和GitHub等提供商登录。然后,您只需要一个数据库URL,然后引用Firebase Auth。