2017-05-03 149 views
0

我正在开发一个NativeScript应用程序,它将使用监听手机的通话状态。对于这个我使用CXCallObserver和CXCallObserverDelegate我设置如下:CXCallObserverDelegate:callChanged不触发

module.exports = { 
     phoneDelegate: NSObject.extend({ 
      initWithResolveReject: function(resolve, reject){ 
       var self = this.super.init(); 
       if(self){ 
        this.resolve = resolve; 
        this.reject = reject; 
       } 
       return self; 
      }, 

      callObserverCallChanged: function(observer, call){ 
       console.log("This log is not triggering"); 
       if(call.hasEnded){ 
        // call has ended 
        this.resolve({phoneState: "ended"}); 
       } 
       if(call.isOutgoing && !call.hasConnected){ 
        // Dialing out 
        this.resolve({phoneState: "outgoing call"}); 
       } 
       if(!call.isOutgoing && !call.hasConnected && !call.hasEnded){ 
        // Call is incoming 
        this.resolve({phoneState: "incoming call"}); 
       } 
       if(call.hasConnected && !call.hasEnded){ 
        // Call is ongoing 
        this.resolve({phoneState: "ongoing call"}); 
       } 
      } 
     }, { 
      protocols: [CXCallObserverDelegate] 
     }), 

     registerListener: function(){ 
      return new Promise((resolve, reject) => { 
       try{ 
        this.callObserver = new CXCallObserver(); 

        let myCallDelegate = this.phoneDelegate.alloc().initWithResolveReject(resolve, reject); 
        this.callObserver.setDelegateQueue(myCallDelegate, null); 
        console.log("phone listener registered"); 
       } catch(error) { 
        reject({error: error}); 
       } 
      }) 
     } 
    } 

侦听器得到注册,因为它应该,至少没有错误抛出,最后控制台登录“registerListener”作为执行这应该。

当我尝试拨打电话时,传入或传出都没有任何反应。至少第一个控制台登录“callObserverCallChanged”应该在任何电话状态改变时执行。但没有任何反应。

任何人有什么建议可能是错的?

+0

我已经向NativeScript的github页面报告过这个问题,所以任何感兴趣的人都可以按照这里(https://github.com/NativeScript/NativeScript/issues/4099) –

回答

0

let myCallDelegate = this.phoneDelegate.alloc()。initWithResolveReject(resolve,reject); this.callObserver.setDelegateQueue(myCallDelegate,null);

好的解决方案很尴尬。由于未将其分配给类,因此myCallDelegate实例在执行任何操作之前已被销毁。那么这里的解决方案是:

this.myCallDelegate = this.phoneDelegate.alloc().initWithResolveReject(resolve, reject); 
this.callObserver.setDelegateQueue(this.myCallDelegate, null); 

因为要花费他的时间和发现这个错误,