2017-07-31 7 views
1

私は、ロケーションサービスが有効になっているかどうかを常に確認する方法を理解しようとしています。常に私はリアルタイムチェッカーのような意味です。私が今持っているのは一つの見方だけです。私はでたときにユーザーがサインインチェックしています - ロケーションサービスが有効になっている場合、彼は署名を、それはその後、使用不能だ場合は、警告ダイアログが表示されます。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)); 
     }); 
    } 

は、私は、ユーザーがサインインしようとすると、この関数を呼び出すの投稿した写真と

例:

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(); 
    } 

が、私は、この関数は全体のアプリケーションシートで仕事をしたいですnログインビューだけではありません。それ、どうやったら出来るの?

答えて

2

これは、Angular Dependency Injectionがコンポーネント/ビュー間で既存のメソッドを再利用するのに役立つ典型的なシナリオです。

アプリケーションでLocationCommonServiceを作成し、ロケーションサービスが有効かどうかを確認するメソッドを定義できます。

ここで、必要な機能を呼び出す必要があるすべてのコンポーネントにLocationCommonServiceを挿入します。

関連する問題