2017-07-17 55 views
1

我試圖出口Objective-C的方法反應母語與希望,但是當我檢查的反應本地的返回值,儘管objective-c返回正確的數據,但它始終爲空。導出(橋接)從Objective-C的方法反應本地人,有着無極返回NULL只有

這裏是我的Objective-C的方法。

RCT_EXPORT_METHOD(checkEmail:(NSString *)email 
    resolver:(RCTPromiseResolveBlock)resolve 
    rejecter:(RCTPromiseRejectBlock)reject) 
{ 
    @autoreleasepool { 
     [GRPCCall useInsecureConnectionsForHost:kHostAddress]; 

     AccountService *client = [[AccountService alloc] initWithHost:kHostAddress]; 

     EmailCheckRequest *request = [EmailCheckRequest message]; 
     request.email = email; 
     NSLog(@"Requested Email: %@\n", request.email); 

     [client checkEmailWithRequest:request handler:^(EmailCheckResponse *response, NSError *error) { 
      NSLog(@"Email Check result: %d\n", response.ret); 
      if (response) { 
       NSLog(@"Resolved response\n"); 
       resolve(response); 
      } 
      else { 
       NSLog(@"Rejected response\n"); 
       reject(@"no_events", @"There was no event", error); 
      } 
     }]; 
    } 
} 

而反原生方法的定義是這樣的。

checkEmail = (email:string) => { 
    console.log('Network Check Email is called'); 
    return this._network.checkEmail(email); 
} 

checkEmail = (email:string) => { 
    return new Promise((resolve, reject) => { 
     console.log('Network Check Email is called'); 
     this._network.checkEmail(email).then((res) => { 
      console.log('check email result: ' + res); 
      resolve(res); 
     }); 
    }); 
} 

我測試了這兩種情況下,但checkEmail的返回值始終是

async _checkEmailDuplication() { 
    try { 
     console.log('Check Email Duplication is called'); 
     const res = await MyNetwork.checkEmail(this.state.email); 
     console.log('Check email return value: ' + res); 
     if (res === 0) { 
      console.log('Available Email'); 
      return false; 
     } 
     else { 
      console.log('Email Duplication Check Error'); 
      this.state.errMsg = WSStrings.errCodes[res]; 
      return true; 
     } 
    } 
    catch (e) { 
     console.error('Check Email Call Error', e.message); 
     this.state.errMsg = e.message; 
     return true; 
    } 
} 

以下是來自Xcode的日誌,當我運行上面的代碼時。

2017-07-18 00:51:43.781 [info][tid:com.facebook.React.JavaScript] Check Email Duplication is called 
2017-07-18 00:51:43.782 [info][tid:com.facebook.React.JavaScript] Network Check Email is called 
2017-07-18 00:51:43.839 [5782:110747] Requested Email: [email protected] 
2017-07-18 00:51:43.872 [5782:109339] Email Check result: 2 
2017-07-18 00:51:43.872 [5782:109339] Resolved response 
2017-07-18 00:51:43.873 [info][tid:com.facebook.React.JavaScript] check email result: null 
2017-07-18 00:51:43.874 [info][tid:com.facebook.React.JavaScript] Check email return value: null 
2017-07-18 00:51:43.874 [info][tid:com.facebook.React.JavaScript] Email Duplication Check Error 

它沒有得到正確的返回代碼,而只是NULL,不能進入下一個步驟。

是否有在Objective-C代碼或反應的本地代碼任何語法錯誤?

我沒有Objective-C的背景知識和我沒有在承諾足夠的經驗和應對本地的橋樑,因此,如果有人能幫助我,將不勝感激。

回答

1

最後,我找到了答案。 resolve(response)這部分是問題所在。

響應對象不是由JavaScript端解釋,所以不得不交出的NSDictionary對象這樣的,而不是移交響應對象本身。

  if (response) { 
       NSLog(@"User ID: %d Language: %@\n", response.uid, response.language); 
       NSDictionary *dictRes = @{ 
        @"ret":@(response.ret), 
        @"uid":@(response.uid), 
        @"lang":response.language 
        }; 
       resolve(dictRes); 
      } 

無論如何,這是Objective-C方面的問題,而不是Javascript。

問題解決!!