2017-09-26 30 views
0

我正在使用admob插件處理Ionic 3項目。我創建了兩個變量(Mybanner和Myinterstital是)存儲AdMob的代碼,我想從外部URL獲得這些變量的內容一樣,如何從一個變量從url到離子變量?

http://example.com/admob.php 

,然後把它放在這裏:

id: this.Myabanner, 
id: this.Myinterstitial, 

因爲我想隨時從網站上更改admob代碼。

import { AdMobFree, AdMobFreeBannerConfig, AdMobFreeInterstitialConfig, AdMobFreeRewardVideoConfig } from '@ionic-native/admob-free'; 

@Component({ 
    selector: 'page-home', 
    templateUrl: 'home.html' 
}) 
export class HomePage { 
    Myabanner= "ca-app-pub-3940256099942544/6300978111"; 
    Myinterstitial= "ca-app-pub-3940256099942544/1033173712"; 
    // this tells the tabs component which Pages 
    // should be each tab's root Page 
    constructor(public navCtrl: NavController, private adMobFree: AdMobFree) { 
    this.showInterstitialAd(); 
    this.ionViewDidLoad(); 
    this.showVideoRewardsAd() 
    } 
    ionViewDidLoad() { 
    const bannerConfig: AdMobFreeBannerConfig = { 
     id: this.Myabanner, 
     isTesting: false, 
     autoShow: true 
    }; 
    this.adMobFree.banner.config(bannerConfig); 
    this.adMobFree.banner.prepare() 
     .then(() => { 

     }) 
     .catch(e => console.log(e)); 

    } 
    async showInterstitialAd() { 
    try { 
     const interstitialConfig: AdMobFreeInterstitialConfig = { 
     id: this.Myinterstitial, 
     isTesting: false, 
     autoShow: true 
     } 

     this.adMobFree.interstitial.config(interstitialConfig); 

     const result = await this.adMobFree.interstitial.prepare(); 
     console.log(result); 
    } 
    catch (e) { 
     console.error(e) 
    } 
    } 
    async showVideoRewardsAd() { 
    try { 
     const videoRewardsConfig: AdMobFreeRewardVideoConfig = { 
     //id: 'ca-app-pub-3940256099942544/5224354917', 
     isTesting: true, 
     autoShow: true 
     } 

     this.adMobFree.rewardVideo.config(videoRewardsConfig); 

     const result = await this.adMobFree.rewardVideo.prepare(); 
     console.log(result); 
    } 
    catch (e) { 
     console.error(e); 
    } 
    } 
} 

回答

0

在你的PHP,分配您的AdMob代碼在數組轉換數組到JSON輸出。

在您的離子中,通過使用Http Cordova插件,您可以獲得該json值。

https://ionicframework.com/docs/native/http/

import { HTTP } from '@ionic-native/http'; 

constructor(private http: HTTP) { } 

... 

this.http.get('http://example.com/admob.php', {}, {}) 
.then(data => { 

    console.log(data.status); 
    console.log(data.data); // data received by server 
    console.log(data.headers); 

}) 

.catch(error => { 

    console.log(error.status); 
    console.log(error.error); // error message as string 
    console.log(error.headers); 

}); 
+0

是啊,這多虧的方式,但我怎麼可以存儲成變量 – Mr2upp