Sounds true and I wondered this myself earlier. So I tried the solution with the plugin removed which did not result in a default window.open action (nothing happened).
Just now I tried adding an event listener according to the plugin specs, which I hope isn’t a default method for the traditional Window object. And this fires properly.
function callback() { console.log('callback fired'); }
ref.addEventListener('loadstart', callback);
Anyway, no explanation for your observation or can it be that the typing says interface InAppBrowser extends Window?
I am no expert on TypeScript and plugins. And have observed similar clobber behaviour on the email plugin.
Yes, you’re right, the plugin is loading after all. It is difficult to tell the difference in a browser session using ionic serve, but it is apparent on my actual device. I can use InAppBrowser this way. Sorry for the confusion. I’ve removed the misinformation from my above post.
The fact that you cannot call cordova.InAppBrowser is a bug, but one that a developer can work around with little difficulty.
However, there is still a problem. There are other plugins that suffer from the same bug, and cannot be worked around this way because they don’t override default methods. The one I am struggling with now is NativeAudio. window.plugins doesn’t exist at runtime, so I cannot call window.plugins.NativeAudio.
I added a NativeAudio.d.ts file for it, and I am confident I did it correctly. There are no type errors at compile time.
The plugin works fine in Ionic 1, and other plugins such as InAppBrowser show the same defect, so I believe this is an Ionic 2 bug, not a bug with the plugin.
declare var Media:any; in the ts file you want to use it
No import or any hassling with the definition files is necessary. However, the downside, that you don’t have autocompletion within your IDE, and of course compile time checks for strict typing…
Be aware the media plugin has some issues with getting durations of sound files (always returning -1). I used the Audio Web API directly, which is amazing!!! (https://issues.apache.org/jira/browse/CB-6427)
I tried what you outlined here, but I just get compile and JS runtime errors because the Media type doesn’t exist. I tried it with a method connected to a click button event, but Media simply doesn’t exist as far as the framework is concerned. Here’s the code I used:
testAudio() {
let media: any;
media = new Media("testaudio.mp3');
media.play();
}
import {Page} from 'ionic-framework/ionic';
declare var Media:any;
@Page({
templateUrl: 'build/pages/settings/offline.html',
})
export class TestPage {
constructor() {
let media = new Media('someurl');
media.play();
}
}
My bad. Had it deleted from the repository. Uploaded again. As mentioned, not all plugins work fine, but the basic ones (vibration, device, status, etc.) are.
Seems like the app is still not going. I pulled again your repo and will give it a go locally.
Many thanks anyway for sharing. Typescript, although it brings some constraints, is the way to go and your Cordova plugins integration demo just fix the missing link until they get somehow integrated in ionic-native I presume.
I still try to wrap my head around why should we not try to inject them(the required cordova plugins) as providers basically. Probably a dummy question I guess.
With typings, I don’t need to reference /// <reference path="Cordova.d.ts"/> on the top of *.ts files. The transpile won’t give any errors, and I got the nice intellisence with Visual Code as well.
This is the correct answer moving forward with “typings” over “tsd”. TSD ultimately will become typings in a sense to prevent need of single repo location and allow typings to be resolved and installed from anywhere. Just a quick FYI.
Just as posted install cordova first then cordova/plugins/YOUR_PLUGIN. this is because you prob. don’t need all plugin definitions installed but rather those you’re using. Upon install “cordova” first you will notice that it strips the individual plugin defs.
I get the following error when i run this command typings install cordova --global --save
did I miss something
typings ERR! message Unable to find “cordova” (“npm”) in the registry.
typings ERR! message However, we found “cordova” for 1 other source: "dt"
typings ERR! message You can install these using the “source” option.
typings ERR! message We could use your help adding these typings to the registry: https://github.com/typings/registry
typings ERR! caused by https://api.typings.org/entries/npm/cordova/versions/latest responded with 404, expected it to equal 200