【发布时间】:2016-11-11 18:28:02
【问题描述】:
在我的 iOS 应用程序中,我在 Objective C 中使用 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];
}
}
能否请您指出上述代码中的任何错误。
谢谢 工作组
【问题讨论】:
标签: firebase firebase-realtime-database