2017-07-31 44 views
1

我想弄清楚如何總是檢查位置服務是否啓用。總是我的意思是像一個實時檢查。我現在所擁有的只是一種觀點。 。當用戶登錄我檢查 - 如果啓用了定位服務,他在簽署然而,如果它是unenabled則會出現一個警告對話框:如何隨時檢查定位服務是否在Ionic 2中啓用?

這是我的功能,如果它啓用了檢查:

checkLocation() { 
    this.diagnostic.isLocationEnabled().then(
     (isAvailable) => { 
     console.log('Is available? ' + isAvailable); 
     if (isAvailable) { 
      this.navCtrl.setRoot(UserTypePage); 
     } else { 
      alert('Please turn on the location service'); 
      if (this.autoLogin) { 
      this.autoLogin(); 
      } 
     } 
     }).catch((e) => { 
     console.log(e); 
     alert(JSON.stringify(e)); 
     }); 
    } 

我當用戶試圖登錄調用這個函數在與Facebook

例:

facebookLogin(): void { 
    this.global = ShareService.getInstance(); 
    this.subscriptions.push(this.authProvider.loginWithFacebook().subscribe((user) => { 
     this.loading.dismiss().then(() => { 

     this.global.setUserName(user.displayName); 
     this.global.setProfilePicture(user.photoURL); 
     this.global.setUserId(this.authProvider.currentUserId); 
     this.tokenstore(); 
     this.checkLocation(); //HERE 
     }) 

    }, error => { 
     this.loading.dismiss().then(() => { 
     let alert = this.alertCtrl.create({ 
      message: error.message, 
      buttons: [ 
      { 
       text: "Ok", 
       role: 'cancel' 
      } 
      ] 
     }); 
     alert.present(); 
     }); 
    }, (err) => { 
     console.error("error: " + JSON.stringify(err)); 
    })); 
    this.loading = this.loadingCtrl.create({ 
     content: 'Signing in...' 
    }); 
    this.loading.present(); 
    } 

我想這個功能在整個應用A工作不只是在登錄視圖中。我怎麼做?

回答

2

這是一個經典場景,其中Angular Dependency Injection將幫助您跨組件/視圖重用現有方法。

您可以在您的應用程序中創建LocationCommonService並定義檢查Location服務是否啓用的方法。

現在在所有需要調用所需函數的組件中注入LocationCommonService。

相關問題