mirror of
https://github.com/zhigang1992/react-native-firebase.git
synced 2026-04-24 04:24:52 +08:00
[ios] Firebase iOS v5 support
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
#if __has_include(<FirebaseAnalytics/FIRAnalytics.h>)
|
||||
#import <FirebaseAnalytics/FIRAnalytics.h>
|
||||
#import <FirebaseAnalytics/FIRAnalyticsConfiguration.h>
|
||||
#import <FirebaseCore/FIRAnalyticsConfiguration.h>
|
||||
|
||||
@implementation RNFirebaseAnalytics
|
||||
RCT_EXPORT_MODULE();
|
||||
|
||||
@@ -128,15 +128,7 @@ RCT_EXPORT_METHOD(signOut:
|
||||
RCT_EXPORT_METHOD(signInAnonymously:(NSString *) appDisplayName
|
||||
resolver:(RCTPromiseResolveBlock) resolve
|
||||
rejecter:(RCTPromiseRejectBlock) reject) {
|
||||
FIRApp *firApp = [RNFirebaseUtil getApp:appDisplayName];
|
||||
|
||||
[[FIRAuth authWithApp:firApp] signInAnonymouslyWithCompletion:^(FIRUser *user, NSError *error) {
|
||||
if (error) {
|
||||
[self promiseRejectAuthException:reject error:error];
|
||||
} else {
|
||||
[self promiseWithUser:resolve rejecter:reject user:user];
|
||||
}
|
||||
}];
|
||||
[self signInAnonymously:appDisplayName withData:false resolver:resolve rejecter:reject];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -149,13 +141,22 @@ RCT_EXPORT_METHOD(signInAnonymously:(NSString *) appDisplayName
|
||||
RCT_EXPORT_METHOD(signInAnonymouslyAndRetrieveData:(NSString *) appDisplayName
|
||||
resolver:(RCTPromiseResolveBlock) resolve
|
||||
rejecter:(RCTPromiseRejectBlock) reject) {
|
||||
[self signInAnonymously:appDisplayName withData:true resolver:resolve rejecter:reject];
|
||||
}
|
||||
|
||||
-(void)signInAnonymously:(NSString *)appDisplayName
|
||||
withData:(BOOL)withData
|
||||
resolver:(RCTPromiseResolveBlock) resolve
|
||||
rejecter:(RCTPromiseRejectBlock) reject {
|
||||
FIRApp *firApp = [RNFirebaseUtil getApp:appDisplayName];
|
||||
|
||||
[[FIRAuth authWithApp:firApp] signInAnonymouslyAndRetrieveDataWithCompletion:^(FIRAuthDataResult *authResult, NSError *error) {
|
||||
[[FIRAuth authWithApp:firApp] signInAnonymouslyWithCompletion:^(FIRAuthDataResult *authResult, NSError *error) {
|
||||
if (error) {
|
||||
[self promiseRejectAuthException:reject error:error];
|
||||
} else {
|
||||
} else if (withData) {
|
||||
[self promiseWithAuthResult:resolve rejecter:reject authResult:authResult];
|
||||
} else {
|
||||
[self promiseWithUser:resolve rejecter:reject user:authResult.user];
|
||||
}
|
||||
}];
|
||||
}
|
||||
@@ -174,15 +175,7 @@ RCT_EXPORT_METHOD(signInWithEmailAndPassword:(NSString *) appDisplayName
|
||||
pass:(NSString *) password
|
||||
resolver:(RCTPromiseResolveBlock) resolve
|
||||
rejecter:(RCTPromiseRejectBlock) reject) {
|
||||
FIRApp *firApp = [RNFirebaseUtil getApp:appDisplayName];
|
||||
|
||||
[[FIRAuth authWithApp:firApp] signInWithEmail:email password:password completion:^(FIRUser *user, NSError *error) {
|
||||
if (error) {
|
||||
[self promiseRejectAuthException:reject error:error];
|
||||
} else {
|
||||
[self promiseWithUser:resolve rejecter:reject user:user];
|
||||
}
|
||||
}];
|
||||
[self signInWithEmail:appDisplayName email:email password:password withData:false resolver:resolve rejecter:reject];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -196,16 +189,27 @@ RCT_EXPORT_METHOD(signInWithEmailAndPassword:(NSString *) appDisplayName
|
||||
*/
|
||||
RCT_EXPORT_METHOD(signInAndRetrieveDataWithEmailAndPassword:(NSString *) appDisplayName
|
||||
email:(NSString *) email
|
||||
pass:(NSString *) password
|
||||
password:(NSString *) password
|
||||
resolver:(RCTPromiseResolveBlock) resolve
|
||||
rejecter:(RCTPromiseRejectBlock) reject) {
|
||||
[self signInWithEmail:appDisplayName email:email password:password withData:true resolver:resolve rejecter:reject];
|
||||
}
|
||||
|
||||
-(void)signInWithEmail:(NSString *)appDisplayName
|
||||
email:(NSString *)email
|
||||
password:(NSString *)password
|
||||
withData:(BOOL)withData
|
||||
resolver:(RCTPromiseResolveBlock) resolve
|
||||
rejecter:(RCTPromiseRejectBlock) reject {
|
||||
FIRApp *firApp = [RNFirebaseUtil getApp:appDisplayName];
|
||||
|
||||
[[FIRAuth authWithApp:firApp] signInAndRetrieveDataWithEmail:email password:password completion:^(FIRAuthDataResult *authResult, NSError *error) {
|
||||
[[FIRAuth authWithApp:firApp] signInWithEmail:email password:password completion:^(FIRAuthDataResult *authResult, NSError *error) {
|
||||
if (error) {
|
||||
[self promiseRejectAuthException:reject error:error];
|
||||
} else {
|
||||
} else if (withData) {
|
||||
[self promiseWithAuthResult:resolve rejecter:reject authResult:authResult];
|
||||
} else {
|
||||
[self promiseWithUser:resolve rejecter:reject user:authResult.user];
|
||||
}
|
||||
}];
|
||||
}
|
||||
@@ -249,15 +253,7 @@ RCT_EXPORT_METHOD(createUserWithEmailAndPassword:(NSString *) appDisplayName
|
||||
pass:(NSString *) password
|
||||
resolver:(RCTPromiseResolveBlock) resolve
|
||||
rejecter:(RCTPromiseRejectBlock) reject) {
|
||||
FIRApp *firApp = [RNFirebaseUtil getApp:appDisplayName];
|
||||
|
||||
[[FIRAuth authWithApp:firApp] createUserWithEmail:email password:password completion:^(FIRUser *user, NSError *error) {
|
||||
if (error) {
|
||||
[self promiseRejectAuthException:reject error:error];
|
||||
} else {
|
||||
[self promiseWithUser:resolve rejecter:reject user:user];
|
||||
}
|
||||
}];
|
||||
[self createUserWithEmail:appDisplayName email:email password:password withData:false resolver:resolve rejecter:reject];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -271,18 +267,28 @@ RCT_EXPORT_METHOD(createUserWithEmailAndPassword:(NSString *) appDisplayName
|
||||
*/
|
||||
RCT_EXPORT_METHOD(createUserAndRetrieveDataWithEmailAndPassword:(NSString *) appDisplayName
|
||||
email:(NSString *) email
|
||||
pass:(NSString *) password
|
||||
password:(NSString *) password
|
||||
resolver:(RCTPromiseResolveBlock) resolve
|
||||
rejecter:(RCTPromiseRejectBlock) reject) {
|
||||
[self createUserWithEmail:appDisplayName email:email password:password withData:true resolver:resolve rejecter:reject];
|
||||
}
|
||||
|
||||
-(void)createUserWithEmail:(NSString *)appDisplayName
|
||||
email:(NSString *)email
|
||||
password:(NSString *)password
|
||||
withData:(BOOL)withData
|
||||
resolver:(RCTPromiseResolveBlock) resolve
|
||||
rejecter:(RCTPromiseRejectBlock) reject {
|
||||
FIRApp *firApp = [RNFirebaseUtil getApp:appDisplayName];
|
||||
|
||||
[[FIRAuth authWithApp:firApp] createUserAndRetrieveDataWithEmail:email password:password
|
||||
completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) {
|
||||
if (error) {
|
||||
[self promiseRejectAuthException:reject error:error];
|
||||
} else {
|
||||
[self promiseWithAuthResult:resolve rejecter:reject authResult:authResult];
|
||||
}
|
||||
[[FIRAuth authWithApp:firApp] createUserWithEmail:email password:password completion:^(FIRAuthDataResult *authResult, NSError *error) {
|
||||
if (error) {
|
||||
[self promiseRejectAuthException:reject error:error];
|
||||
} else if (withData) {
|
||||
[self promiseWithAuthResult:resolve rejecter:reject authResult:authResult];
|
||||
} else {
|
||||
[self promiseWithUser:resolve rejecter:reject user:authResult.user];
|
||||
}
|
||||
}];
|
||||
}
|
||||
|
||||
@@ -536,21 +542,7 @@ RCT_EXPORT_METHOD(signInWithCredential:(NSString *) appDisplayName
|
||||
secret:(NSString *) authSecret
|
||||
resolver:(RCTPromiseResolveBlock) resolve
|
||||
rejecter:(RCTPromiseRejectBlock) reject) {
|
||||
FIRApp *firApp = [RNFirebaseUtil getApp:appDisplayName];
|
||||
|
||||
FIRAuthCredential *credential = [self getCredentialForProvider:provider token:authToken secret:authSecret];
|
||||
|
||||
if (credential == nil) {
|
||||
return reject(@"auth/invalid-credential", @"The supplied auth credential is malformed, has expired or is not currently supported.", nil);
|
||||
}
|
||||
|
||||
[[FIRAuth authWithApp:firApp] signInWithCredential:credential completion:^(FIRUser *user, NSError *error) {
|
||||
if (error) {
|
||||
[self promiseRejectAuthException:reject error:error];
|
||||
} else {
|
||||
[self promiseWithUser:resolve rejecter:reject user:user];
|
||||
}
|
||||
}];
|
||||
[self signInWithCredential:appDisplayName provider:provider token:authToken secret:authSecret withData:false resolver:resolve rejecter:reject];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -569,6 +561,16 @@ RCT_EXPORT_METHOD(signInAndRetrieveDataWithCredential:(NSString *) appDisplayNam
|
||||
secret:(NSString *) authSecret
|
||||
resolver:(RCTPromiseResolveBlock) resolve
|
||||
rejecter:(RCTPromiseRejectBlock) reject) {
|
||||
[self signInWithCredential:appDisplayName provider:provider token:authToken secret:authSecret withData:true resolver:resolve rejecter:reject];
|
||||
}
|
||||
|
||||
-(void)signInWithCredential:(NSString *)appDisplayName
|
||||
provider:(NSString *) provider
|
||||
token:(NSString *) authToken
|
||||
secret:(NSString *) authSecret
|
||||
withData:(BOOL)withData
|
||||
resolver:(RCTPromiseResolveBlock) resolve
|
||||
rejecter:(RCTPromiseRejectBlock) reject {
|
||||
FIRApp *firApp = [RNFirebaseUtil getApp:appDisplayName];
|
||||
|
||||
FIRAuthCredential *credential = [self getCredentialForProvider:provider token:authToken secret:authSecret];
|
||||
@@ -580,8 +582,10 @@ RCT_EXPORT_METHOD(signInAndRetrieveDataWithCredential:(NSString *) appDisplayNam
|
||||
[[FIRAuth authWithApp:firApp] signInAndRetrieveDataWithCredential:credential completion:^(FIRAuthDataResult *authResult, NSError *error) {
|
||||
if (error) {
|
||||
[self promiseRejectAuthException:reject error:error];
|
||||
} else {
|
||||
} else if (withData) {
|
||||
[self promiseWithAuthResult:resolve rejecter:reject authResult:authResult];
|
||||
} else {
|
||||
[self promiseWithUser:resolve rejecter:reject user:authResult.user];
|
||||
}
|
||||
}];
|
||||
}
|
||||
@@ -764,15 +768,7 @@ RCT_EXPORT_METHOD(signInAndRetrieveDataWithCustomToken:(NSString *) appDisplayNa
|
||||
customToken:(NSString *) customToken
|
||||
resolver:(RCTPromiseResolveBlock) resolve
|
||||
rejecter:(RCTPromiseRejectBlock) reject) {
|
||||
FIRApp *firApp = [RNFirebaseUtil getApp:appDisplayName];
|
||||
|
||||
[[FIRAuth authWithApp:firApp] signInAndRetrieveDataWithCustomToken:customToken completion:^(FIRAuthDataResult *authResult, NSError *error) {
|
||||
if (error) {
|
||||
[self promiseRejectAuthException:reject error:error];
|
||||
} else {
|
||||
[self promiseWithAuthResult:resolve rejecter:reject authResult:authResult];
|
||||
}
|
||||
}];
|
||||
[self signInWithCustomToken:appDisplayName customToken:customToken withData:true resolver:resolve rejecter:reject];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -786,13 +782,23 @@ RCT_EXPORT_METHOD(signInWithCustomToken:(NSString *) appDisplayName
|
||||
customToken:(NSString *) customToken
|
||||
resolver:(RCTPromiseResolveBlock) resolve
|
||||
rejecter:(RCTPromiseRejectBlock) reject) {
|
||||
[self signInWithCustomToken:appDisplayName customToken:customToken withData:false resolver:resolve rejecter:reject];
|
||||
}
|
||||
|
||||
-(void)signInWithCustomToken:(NSString *)appDisplayName
|
||||
customToken:(NSString *) customToken
|
||||
withData:(BOOL)withData
|
||||
resolver:(RCTPromiseResolveBlock) resolve
|
||||
rejecter:(RCTPromiseRejectBlock) reject {
|
||||
FIRApp *firApp = [RNFirebaseUtil getApp:appDisplayName];
|
||||
|
||||
[[FIRAuth authWithApp:firApp] signInWithCustomToken:customToken completion:^(FIRUser *user, NSError *error) {
|
||||
[[FIRAuth authWithApp:firApp] signInWithCustomToken:customToken completion:^(FIRAuthDataResult *authResult, NSError *error) {
|
||||
if (error) {
|
||||
[self promiseRejectAuthException:reject error:error];
|
||||
} else if (withData) {
|
||||
[self promiseWithAuthResult:resolve rejecter:reject authResult:authResult];
|
||||
} else {
|
||||
[self promiseWithUser:resolve rejecter:reject user:user];
|
||||
[self promiseWithUser:resolve rejecter:reject user:authResult.user];
|
||||
}
|
||||
}];
|
||||
}
|
||||
@@ -868,11 +874,11 @@ RCT_EXPORT_METHOD(_confirmVerificationCode:(NSString *) appDisplayName
|
||||
NSString *verificationId = [defaults stringForKey:@"authVerificationID"];
|
||||
FIRAuthCredential *credential = [[FIRPhoneAuthProvider provider] credentialWithVerificationID:verificationId verificationCode:verificationCode];
|
||||
|
||||
[[FIRAuth authWithApp:firApp] signInWithCredential:credential completion:^(FIRUser *user, NSError *error) {
|
||||
[[FIRAuth authWithApp:firApp] signInAndRetrieveDataWithCredential:credential completion:^(FIRAuthDataResult *authResult, NSError *error) {
|
||||
if (error) {
|
||||
[self promiseRejectAuthException:reject error:error];
|
||||
} else {
|
||||
[self promiseWithUser:resolve rejecter:reject user:user];
|
||||
[self promiseWithUser:resolve rejecter:reject user:authResult.user];
|
||||
}
|
||||
}];
|
||||
}
|
||||
@@ -893,26 +899,7 @@ RCT_EXPORT_METHOD(linkWithCredential:(NSString *) appDisplayName
|
||||
authSecret:(NSString *) authSecret
|
||||
resolver:(RCTPromiseResolveBlock) resolve
|
||||
rejecter:(RCTPromiseRejectBlock) reject) {
|
||||
FIRApp *firApp = [RNFirebaseUtil getApp:appDisplayName];
|
||||
FIRAuthCredential *credential = [self getCredentialForProvider:provider token:authToken secret:authSecret];
|
||||
|
||||
if (credential == nil) {
|
||||
return reject(@"auth/invalid-credential", @"The supplied auth credential is malformed, has expired or is not currently supported.", nil);
|
||||
}
|
||||
|
||||
FIRUser *user = [FIRAuth authWithApp:firApp].currentUser;
|
||||
|
||||
if (user) {
|
||||
[user linkWithCredential:credential completion:^(FIRUser *_Nullable _user, NSError *_Nullable error) {
|
||||
if (error) {
|
||||
[self promiseRejectAuthException:reject error:error];
|
||||
} else {
|
||||
[self promiseWithUser:resolve rejecter:reject user:_user];
|
||||
}
|
||||
}];
|
||||
} else {
|
||||
[self promiseNoUser:resolve rejecter:reject isError:YES];
|
||||
}
|
||||
[self linkWithCredential:appDisplayName provider:provider authToken:authToken authSecret:authSecret withData:false resolver:resolve rejecter:reject];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -931,23 +918,35 @@ RCT_EXPORT_METHOD(linkAndRetrieveDataWithCredential:(NSString *) appDisplayName
|
||||
authSecret:(NSString *) authSecret
|
||||
resolver:(RCTPromiseResolveBlock) resolve
|
||||
rejecter:(RCTPromiseRejectBlock) reject) {
|
||||
[self linkWithCredential:appDisplayName provider:provider authToken:authToken authSecret:authSecret withData:true resolver:resolve rejecter:reject];
|
||||
}
|
||||
|
||||
-(void)linkWithCredential:(NSString *)appDisplayName
|
||||
provider:(NSString *)provider
|
||||
authToken:(NSString *)authToken
|
||||
authSecret:(NSString *)authSecret
|
||||
withData:(BOOL)withData
|
||||
resolver:(RCTPromiseResolveBlock)resolve
|
||||
rejecter:(RCTPromiseRejectBlock)reject {
|
||||
FIRApp *firApp = [RNFirebaseUtil getApp:appDisplayName];
|
||||
FIRAuthCredential *credential = [self getCredentialForProvider:provider token:authToken secret:authSecret];
|
||||
|
||||
|
||||
if (credential == nil) {
|
||||
return reject(@"auth/invalid-credential", @"The supplied auth credential is malformed, has expired or is not currently supported.", nil);
|
||||
}
|
||||
|
||||
|
||||
FIRUser *user = [FIRAuth authWithApp:firApp].currentUser;
|
||||
if (user) {
|
||||
[user linkAndRetrieveDataWithCredential:credential
|
||||
completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) {
|
||||
if (error) {
|
||||
[self promiseRejectAuthException:reject error:error];
|
||||
} else {
|
||||
[self promiseWithAuthResult:resolve rejecter:reject authResult:authResult];
|
||||
}
|
||||
}];
|
||||
if (error) {
|
||||
[self promiseRejectAuthException:reject error:error];
|
||||
} else if (withData) {
|
||||
[self promiseWithAuthResult:resolve rejecter:reject authResult:authResult];
|
||||
} else {
|
||||
[self promiseWithUser:resolve rejecter:reject user:authResult.user];
|
||||
}
|
||||
}];
|
||||
} else {
|
||||
[self promiseNoUser:resolve rejecter:reject isError:YES];
|
||||
}
|
||||
@@ -1003,28 +1002,7 @@ RCT_EXPORT_METHOD(reauthenticateWithCredential:(NSString *) appDisplayName
|
||||
authSecret:(NSString *) authSecret
|
||||
resolver:(RCTPromiseResolveBlock) resolve
|
||||
rejecter:(RCTPromiseRejectBlock) reject) {
|
||||
FIRApp *firApp = [RNFirebaseUtil getApp:appDisplayName];
|
||||
|
||||
FIRAuthCredential *credential = [self getCredentialForProvider:provider token:authToken secret:authSecret];
|
||||
|
||||
if (credential == nil) {
|
||||
return reject(@"auth/invalid-credential", @"The supplied auth credential is malformed, has expired or is not currently supported.", nil);
|
||||
}
|
||||
|
||||
FIRUser *user = [FIRAuth authWithApp:firApp].currentUser;
|
||||
|
||||
if (user) {
|
||||
[user reauthenticateWithCredential:credential completion:^(NSError *_Nullable error) {
|
||||
if (error) {
|
||||
[self promiseRejectAuthException:reject error:error];
|
||||
} else {
|
||||
FIRUser *userAfterAuth = [FIRAuth authWithApp:firApp].currentUser;
|
||||
[self promiseWithUser:resolve rejecter:reject user:userAfterAuth];
|
||||
}
|
||||
}];
|
||||
} else {
|
||||
[self promiseNoUser:resolve rejecter:reject isError:YES];
|
||||
}
|
||||
[self reauthenticateWithCredential:appDisplayName provider:provider authToken:authToken authSecret:authSecret withData:false resolver:resolve rejecter:reject];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1043,6 +1021,16 @@ RCT_EXPORT_METHOD(reauthenticateAndRetrieveDataWithCredential:(NSString *) appDi
|
||||
authSecret:(NSString *) authSecret
|
||||
resolver:(RCTPromiseResolveBlock) resolve
|
||||
rejecter:(RCTPromiseRejectBlock) reject) {
|
||||
[self reauthenticateWithCredential:appDisplayName provider:provider authToken:authToken authSecret:authSecret withData:true resolver:resolve rejecter:reject];
|
||||
}
|
||||
|
||||
-(void)reauthenticateWithCredential:(NSString *) appDisplayName
|
||||
provider:(NSString *) provider
|
||||
authToken:(NSString *) authToken
|
||||
authSecret:(NSString *) authSecret
|
||||
withData:(BOOL) withData
|
||||
resolver:(RCTPromiseResolveBlock) resolve
|
||||
rejecter:(RCTPromiseRejectBlock) reject {
|
||||
FIRApp *firApp = [RNFirebaseUtil getApp:appDisplayName];
|
||||
|
||||
FIRAuthCredential *credential = [self getCredentialForProvider:provider token:authToken secret:authSecret];
|
||||
@@ -1057,8 +1045,10 @@ RCT_EXPORT_METHOD(reauthenticateAndRetrieveDataWithCredential:(NSString *) appDi
|
||||
[user reauthenticateAndRetrieveDataWithCredential:credential completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) {
|
||||
if (error) {
|
||||
[self promiseRejectAuthException:reject error:error];
|
||||
} else {
|
||||
} else if (withData) {
|
||||
[self promiseWithAuthResult:resolve rejecter:reject authResult:authResult];
|
||||
} else {
|
||||
[self promiseWithUser:resolve rejecter:reject user:authResult.user];
|
||||
}
|
||||
}];
|
||||
} else {
|
||||
|
||||
@@ -164,7 +164,7 @@ RCT_EXPORT_METHOD(transactionBegin:(NSString *)appDisplayName
|
||||
} else if ([type isEqualToString:@"set"]) {
|
||||
NSDictionary *options = command[@"options"];
|
||||
if (options && options[@"merge"]) {
|
||||
[transaction setData:data forDocument:ref options:[FIRSetOptions merge]];
|
||||
[transaction setData:data forDocument:ref merge:true];
|
||||
} else {
|
||||
[transaction setData:data forDocument:ref];
|
||||
}
|
||||
@@ -280,7 +280,7 @@ RCT_EXPORT_METHOD(documentBatch:(NSString *)appDisplayName
|
||||
} else if ([type isEqualToString:@"SET"]) {
|
||||
NSDictionary *options = write[@"options"];
|
||||
if (options && options[@"merge"]) {
|
||||
batch = [batch setData:data forDocument:ref options:[FIRSetOptions merge]];
|
||||
batch = [batch setData:data forDocument:ref merge:true];
|
||||
} else {
|
||||
batch = [batch setData:data forDocument:ref];
|
||||
}
|
||||
|
||||
@@ -65,16 +65,14 @@ queryListenOptions:(NSDictionary *) queryListenOptions {
|
||||
}
|
||||
};
|
||||
|
||||
FIRQueryListenOptions *options = [[FIRQueryListenOptions alloc] init];
|
||||
bool includeMetadataChanges;
|
||||
if (queryListenOptions && queryListenOptions[@"includeMetadataChanges"]) {
|
||||
[options includeDocumentMetadataChanges:true];
|
||||
[options includeQueryMetadataChanges:true];
|
||||
includeMetadataChanges = true;
|
||||
} else {
|
||||
[options includeDocumentMetadataChanges:false];
|
||||
[options includeQueryMetadataChanges:false];
|
||||
includeMetadataChanges = false;
|
||||
}
|
||||
|
||||
id<FIRListenerRegistration> listener = [_query addSnapshotListenerWithOptions:options listener:listenerBlock];
|
||||
id<FIRListenerRegistration> listener = [_query addSnapshotListenerWithIncludeMetadataChanges:includeMetadataChanges listener:listenerBlock];
|
||||
_listeners[listenerId] = listener;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,11 +65,13 @@ static NSMutableDictionary *_listeners;
|
||||
[self handleDocumentSnapshotEvent:listenerId documentSnapshot:snapshot];
|
||||
}
|
||||
};
|
||||
FIRDocumentListenOptions *options = [[FIRDocumentListenOptions alloc] init];
|
||||
bool includeMetadataChanges;
|
||||
if (docListenOptions && docListenOptions[@"includeMetadataChanges"]) {
|
||||
[options includeMetadataChanges:TRUE];
|
||||
includeMetadataChanges = true;
|
||||
} else {
|
||||
includeMetadataChanges = false;
|
||||
}
|
||||
id<FIRListenerRegistration> listener = [_ref addSnapshotListenerWithOptions:options listener:listenerBlock];
|
||||
id<FIRListenerRegistration> listener = [_ref addSnapshotListenerWithIncludeMetadataChanges:includeMetadataChanges listener:listenerBlock];
|
||||
_listeners[listenerId] = listener;
|
||||
}
|
||||
}
|
||||
@@ -80,7 +82,7 @@ static NSMutableDictionary *_listeners;
|
||||
rejecter:(RCTPromiseRejectBlock) reject {
|
||||
NSDictionary *dictionary = [RNFirebaseFirestoreDocumentReference parseJSMap:[RNFirebaseFirestore getFirestoreForApp:_appDisplayName] jsMap:data];
|
||||
if (options && options[@"merge"]) {
|
||||
[_ref setData:dictionary options:[FIRSetOptions merge] completion:^(NSError * _Nullable error) {
|
||||
[_ref setData:dictionary merge:true completion:^(NSError * _Nullable error) {
|
||||
[RNFirebaseFirestoreDocumentReference handleWriteResponse:error resolver:resolve rejecter:reject];
|
||||
}];
|
||||
} else {
|
||||
|
||||
@@ -118,7 +118,11 @@ RCT_EXPORT_METHOD(downloadFile:(NSString *) appDisplayName
|
||||
rejecter:(RCTPromiseRejectBlock) reject) {
|
||||
FIRStorageReference *fileRef = [self getReference:path appDisplayName:appDisplayName];
|
||||
NSURL *localFile = [NSURL fileURLWithPath:localPath];
|
||||
FIRStorageDownloadTask *downloadTask = [fileRef writeToFile:localFile];
|
||||
|
||||
__block FIRStorageDownloadTask *downloadTask;
|
||||
dispatch_sync(dispatch_get_main_queue(), ^{
|
||||
downloadTask = [fileRef writeToFile:localFile];
|
||||
});
|
||||
|
||||
// listen for state changes, errors, and completion of the download.
|
||||
[downloadTask observeStatus:FIRStorageTaskStatusResume handler:^(FIRStorageTaskSnapshot *snapshot) {
|
||||
@@ -293,13 +297,19 @@ RCT_EXPORT_METHOD(putFile:(NSString *) appDisplayName
|
||||
|
||||
- (void)uploadFile:(NSString *)appDisplayName url:(NSURL *)url firmetadata:(FIRStorageMetadata *)firmetadata path:(NSString *)path resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject {
|
||||
FIRStorageReference *fileRef = [self getReference:path appDisplayName:appDisplayName];
|
||||
FIRStorageUploadTask *uploadTask = [fileRef putFile:url metadata:firmetadata];
|
||||
__block FIRStorageUploadTask *uploadTask;
|
||||
dispatch_sync(dispatch_get_main_queue(), ^{
|
||||
uploadTask = [fileRef putFile:url metadata:firmetadata];
|
||||
});
|
||||
[self addUploadObservers:appDisplayName uploadTask:uploadTask path:path resolver:resolve rejecter:reject];
|
||||
}
|
||||
|
||||
- (void)uploadData:(NSString *)appDisplayName data:(NSData *)data firmetadata:(FIRStorageMetadata *)firmetadata path:(NSString *)path resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject {
|
||||
FIRStorageReference *fileRef = [self getReference:path appDisplayName:appDisplayName];
|
||||
FIRStorageUploadTask *uploadTask = [fileRef putData:data metadata:firmetadata];
|
||||
__block FIRStorageUploadTask *uploadTask;
|
||||
dispatch_sync(dispatch_get_main_queue(), ^{
|
||||
uploadTask = [fileRef putData:data metadata:firmetadata];
|
||||
});
|
||||
[self addUploadObservers:appDisplayName uploadTask:uploadTask path:path resolver:resolve rejecter:reject];
|
||||
}
|
||||
|
||||
@@ -307,27 +317,31 @@ RCT_EXPORT_METHOD(putFile:(NSString *) appDisplayName
|
||||
// listen for state changes, errors, and completion of the upload.
|
||||
[uploadTask observeStatus:FIRStorageTaskStatusResume handler:^(FIRStorageTaskSnapshot *snapshot) {
|
||||
// upload resumed, also fires when the upload starts
|
||||
NSDictionary *event = [self getUploadTaskAsDictionary:snapshot];
|
||||
[self sendJSEvent:appDisplayName type:STORAGE_EVENT path:path title:STORAGE_STATE_CHANGED props:event];
|
||||
[self getUploadTaskAsDictionary:snapshot handler:^(NSDictionary *event) {
|
||||
[self sendJSEvent:appDisplayName type:STORAGE_EVENT path:path title:STORAGE_STATE_CHANGED props:event];
|
||||
}];
|
||||
}];
|
||||
|
||||
[uploadTask observeStatus:FIRStorageTaskStatusPause handler:^(FIRStorageTaskSnapshot *snapshot) {
|
||||
// upload paused
|
||||
NSDictionary *event = [self getUploadTaskAsDictionary:snapshot];
|
||||
[self sendJSEvent:appDisplayName type:STORAGE_EVENT path:path title:STORAGE_STATE_CHANGED props:event];
|
||||
[self getUploadTaskAsDictionary:snapshot handler:^(NSDictionary *event) {
|
||||
[self sendJSEvent:appDisplayName type:STORAGE_EVENT path:path title:STORAGE_STATE_CHANGED props:event];
|
||||
}];
|
||||
}];
|
||||
[uploadTask observeStatus:FIRStorageTaskStatusProgress handler:^(FIRStorageTaskSnapshot *snapshot) {
|
||||
// upload reported progress
|
||||
NSDictionary *event = [self getUploadTaskAsDictionary:snapshot];
|
||||
[self sendJSEvent:appDisplayName type:STORAGE_EVENT path:path title:STORAGE_STATE_CHANGED props:event];
|
||||
[self getUploadTaskAsDictionary:snapshot handler:^(NSDictionary *event) {
|
||||
[self sendJSEvent:appDisplayName type:STORAGE_EVENT path:path title:STORAGE_STATE_CHANGED props:event];
|
||||
}];
|
||||
}];
|
||||
|
||||
[uploadTask observeStatus:FIRStorageTaskStatusSuccess handler:^(FIRStorageTaskSnapshot *snapshot) {
|
||||
// upload completed successfully
|
||||
NSDictionary *resp = [self getUploadTaskAsDictionary:snapshot];
|
||||
[self sendJSEvent:appDisplayName type:STORAGE_EVENT path:path title:STORAGE_STATE_CHANGED props:resp];
|
||||
[self sendJSEvent:appDisplayName type:STORAGE_EVENT path:path title:STORAGE_UPLOAD_SUCCESS props:resp];
|
||||
resolve(resp);
|
||||
[self getUploadTaskAsDictionary:snapshot handler:^(NSDictionary *event) {
|
||||
[self sendJSEvent:appDisplayName type:STORAGE_EVENT path:path title:STORAGE_STATE_CHANGED props:event];
|
||||
[self sendJSEvent:appDisplayName type:STORAGE_EVENT path:path title:STORAGE_UPLOAD_SUCCESS props:event];
|
||||
resolve(event);
|
||||
}];
|
||||
}];
|
||||
|
||||
[uploadTask observeStatus:FIRStorageTaskStatusFailure handler:^(FIRStorageTaskSnapshot *snapshot) {
|
||||
@@ -352,10 +366,14 @@ RCT_EXPORT_METHOD(putFile:(NSString *) appDisplayName
|
||||
return @{@"bytesTransferred": @(task.progress.completedUnitCount), @"ref": task.reference.fullPath, @"state": [self getTaskStatus:task.status], @"totalBytes": @(task.progress.totalUnitCount)};
|
||||
}
|
||||
|
||||
- (NSDictionary *)getUploadTaskAsDictionary:(FIRStorageTaskSnapshot *)task {
|
||||
NSString *downloadUrl = [task.metadata.downloadURL absoluteString];
|
||||
NSDictionary *metadata = [task.metadata dictionaryRepresentation];
|
||||
return @{@"bytesTransferred": @(task.progress.completedUnitCount), @"downloadURL": downloadUrl != nil ? downloadUrl : [NSNull null], @"metadata": metadata != nil ? metadata : [NSNull null], @"ref": task.reference.fullPath, @"state": [self getTaskStatus:task.status], @"totalBytes": @(task.progress.totalUnitCount)};
|
||||
- (void)getUploadTaskAsDictionary:(FIRStorageTaskSnapshot *)task
|
||||
handler:(void(^)(NSDictionary *))handler {
|
||||
[[task reference] downloadURLWithCompletion:^(NSURL * _Nullable URL, NSError * _Nullable error) {
|
||||
NSString *downloadUrl = [URL absoluteString];
|
||||
NSDictionary *metadata = [task.metadata dictionaryRepresentation];
|
||||
NSDictionary *dictionary = @{@"bytesTransferred": @(task.progress.completedUnitCount), @"downloadURL": downloadUrl != nil ? downloadUrl : [NSNull null], @"metadata": metadata != nil ? metadata : [NSNull null], @"ref": task.reference.fullPath, @"state": [self getTaskStatus:task.status], @"totalBytes": @(task.progress.totalUnitCount)};
|
||||
handler(dictionary);
|
||||
}];
|
||||
}
|
||||
|
||||
- (FIRStorageMetadata *)buildMetadataFromMap:(NSDictionary *)metadata {
|
||||
|
||||
Reference in New Issue
Block a user