javascriptangulartypescriptwebkit

window.webkit in Typescript


I need to rewrite a javascript code into typescript (angular), the webpage is opened by an IOS app or an Android app. I just want to send a message to the application.

How can i do to send a message to the parent app or how can i use window.webkit ?

notifyTheApp(postData) {
  if (navigator.userAgent.match(/(iPod|iPhone|iPad)/)) {
    if(window.webkit.messageHandlers)      
     window.webkit.messageHandlers.mpos.postMessage(JSON.stringify(postData));
    } 
    else {
      if(window.external.notify)
        window.external.notify(JSON.stringify(postData));
    }

mpos is the iOS application

ERROR in src/app/sms-validation/sms-validation.component.ts(98,17): error TS2339: Property 'webkit' does not exist on type 'Window'.


Solution

  • You can use type assertion (https://basarat.gitbooks.io/typescript/content/docs/types/type-assertion.html) to temporarily cast window the any type. You will loose intellisense for that statement though.

    (window as any).webkit.messageHandlers