我想检索Firebase用户ID。当我使用带有“FIRAuthCredential”的Facebook签名时,我可以检索用户ID,但无法在其外部检索它。因为,我正在创建实时数据库,因此我签署后需要userID。任何帮助都非常感谢?
//In this handler I can retrieve FIR userID using "user.uid"
handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
NSLog(@"Process error");
} else if (result.isCancelled) {
NSLog(@"Cancelled");
} else {
NSLog(@"Logged in");
FIRAuthCredential *credential = [FIRFacebookAuthProvider
credentialWithAccessToken:[FBSDKAccessToken currentAccessToken]
.tokenString];
[[FIRAuth auth] signInWithCredential:credential
completion:^(FIRUser *user, NSError *error) {
//Retrieved here
[[[ self.databaseRef child:@"users"] child:user.uid]
setValue:@{@"username": userName,
@"usergender":userGender,
@"useremail":userEmail,
@"userfbid":userFBID}];
}];
}
}];
但在另一个功能之外,它会出错。
void(purchased)
{
FIRUser *user;
NSString *key = [[self.databaseRef child:@"User-Subscription"] childByAutoId].key;
//Error here, "user.uid" is empty.
NSDictionary *subscriptions = @{@"uid": user.uid,
@"subs_ID": _product.productIdentifier,
@"StartDate":[dateFormatter stringFromDate:[NSDate date]],
@"EndDate": [dateFormatter stringFromDate:expirationDate]};
NSDictionary *childUpdates = @{[@"/User-Subscription/" stringByAppendingString:key]: subscriptions};
[self.databaseRef updateChildValues:childUpdates];
}
答案 0 :(得分:0)
我找到了一个解决方案。在signIn处理程序中,只需在设备中保存userID,然后检索它以供以后使用。但是,如果有人有另一个解决方案,他们会受到欢迎。
//In this handler I can retrieve FIR userID using "user.uid"
NSUserDefaults *saveID = [NSUserDefaults standardUserDefaults];
handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
NSLog(@"Process error");
} else if (result.isCancelled) {
NSLog(@"Cancelled");
} else {
NSLog(@"Logged in");
FIRAuthCredential *credential = [FIRFacebookAuthProvider
credentialWithAccessToken:[FBSDKAccessToken currentAccessToken]
.tokenString];
[[FIRAuth auth] signInWithCredential:credential
completion:^(FIRUser *user, NSError *error) {
//Retrieved here
[[[ self.databaseRef child:@"users"] child:user.uid]
setValue:@{@"username": userName,
@"usergender":userGender,
@"useremail":userEmail,
@"userfbid":userFBID}];
}];
[saveID setObject:user.uid forKey:k_FIRUserID];
[saveID synchronize];
}
}];
//在购买订阅功能
void(purchased)
{
NSString *key = [[self.databaseRef child:@"User-Subscription"] childByAutoId].key;
//Retrieve userID here.
NSString *saveUserID = [saveapp objectForKey:k_FIRUserID];
NSDictionary *subscriptions = @{@"uid": saveUserID,
@"subs_ID": _product.productIdentifier,
@"StartDate":[dateFormatter stringFromDate:[NSDate date]],
@"EndDate": [dateFormatter stringFromDate:expirationDate]};
NSDictionary *childUpdates = @{[@"/User-Subscription/" stringByAppendingString:key]: subscriptions};
[self.databaseRef updateChildValues:childUpdates];
}