question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to use under WKWebView

See original GitHub issue

I’m submitting a … (check one with “x”)

[ ] bug report => check the FAQ and search github for a similar issue or PR before submitting
[ ] support request => check the FAQ and search github for a similar issue before submitting
[x] feature request

Current behavior

When running it in an ionic app using the cordova-plugin-wkwebview-engine plugin which switches the rendering engine from UIWebWiew to WKWebView I get the following error:

XMLHttpRequest cannot load file:///Users/arroyo/Library/Developer/CoreSimulator/Devices/75D80D9A-1804-4F70-A65F-648A7DCDEE51/data/Containers/Bundle/Application/305DDE17-69E4-4CDA-B7AB-49C0CEED9F6B/AstroPrint.app/www/assets/i18n/config.json. Cross origin requests are only supported for HTTP.

Expected/desired behavior

The config.json can be read without using XMLHttpRequest.

Reproduction of the problem

What is the expected behavior?

The error does not happen.

What is the motivation / use case for changing the behavior?

Using WKWebView is much faster in iOS. Current my project can’t migrate there because of this error.

Please tell us about your environment:

  • ng2-translate version: 3.1.x
  • Angular version: 2.0.0
  • Browser: [ iOS 10 WKWebView ]
  • Language: [ TypeScript 2.0]

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:18 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
larssncommented, Nov 7, 2017

This plugin seemed to fix my issues: github.com/oracle/cordova-plugin-wkwebview-file-xhr No loader required

Basically it allows file:// paths in wkwebview as far as I can understand.

1reaction
carlgik4commented, Nov 6, 2017

I google a lot and finally got the ngx-translate to work: First, upgrade cordova and ionic cli to latest version

`xxx:Tabs u$ cordova -v 7.1.0

xxx:Tabs u$ ionic -v 3.16.0

xxx:Tabs u$ ionic info

cli packages: (/Users/xxx/NPM/lib/node_modules)

@ionic/cli-utils  : 1.16.0
ionic (Ionic CLI) : 3.16.0

global packages:

cordova (Cordova CLI) : 7.1.0 

local packages:

@ionic/app-scripts : 3.0.1
Cordova Platforms  : ios 4.5.3
Ionic Framework    : ionic-angular 3.8.0

System:

Android SDK Tools : 25.3.1
ios-deploy        : 1.9.2 
ios-sim           : 6.1.2 
Node              : v6.11.5
npm               : 3.10.10 
OS                : macOS Sierra
Xcode             : Xcode 9.0 Build version 9A235 

Environment Variables:

ANDROID_HOME : /Applications/Android/sdk

Misc:

backend : pro`

create a brand new project

ionic start Tabs sidemenu
cd Tabs
npm install @ngx-translate/core @ngx-translate/http-loader --save

The new project will use wkwebview as default, and there are some error with the usage docs: https://ionicframework.com/docs/developer-resources/ng2-translate/ we should use HttpClientModule instead of HttpModule For the app.module.ts

import { TranslateModule, TranslateLoader, TranslateService } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { HttpClientModule, HttpClient } from "@angular/common/http";

export function createTranslateLoader(http: HttpClient) {
  return new TranslateHttpLoader(http, 'assets/i18n/', '.json');
}

@NgModule({
  declarations: [
    ...
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: (createTranslateLoader),
        deps: [HttpClient]
      }
    }),
    IonicModule.forRoot(MyApp),
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    ...
  ],
  providers: [
    ...
    HttpClient,
    TranslateService,
    ...
  ]
})
export class AppModule {}

For app.component.ts

import { TranslateService } from '@ngx-translate/core';

export class MyApp {
  constructor(public platform: Platform, public splashScreen: SplashScreen,
    translate: TranslateService) {
    translate.setDefaultLang('en');
    let browserLang = translate.getBrowserLang();
    translate.use(browserLang.match(/en|zh/) ? browserLang : 'en');
  }
}

After that, create i18n folder in src/assets create en.json { “HELLO” : “hello” }

For page usage, you can use pipe

{{'HELLO' | translate}}

or manually:

export class HomePage {
  constructor(public navCtrl: NavController,
    private translateService: TranslateService) {
      this.translateService.get('HELLO').subscribe(
        value => {
          // value is our translated string
          let alertTitle = value;
          console.log("translate alert title:" + alertTitle);
        }
      )
  }
}

Hope you save your life with these code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to use WKWebView, gives me an error
Hi this is my first time using these forums, how would I fix this issue: My code is here: import Cocoa import WebKit...
Read more >
Unable to load webpage in WKWebView - Stack Overflow
I've tried looking at the logs on safari using devtools, but unfortunately I don't see anything. If I change the url to point...
Read more >
WKWebview - Ionic Framework
WKWebView runs in a separate process from your app. It is likely that it has its own AudioSession separate from your app's AudioSession....
Read more >
The Ultimate Guide to WKWebView - Hacking with Swift
WKWebView is a powerhouse on iOS, providing high-performance web rendering wherever and whenever you need. In this article I've put together ...
Read more >
JavaScript Manipulation on iOS Using WebKit | Capital One
We may want to load content from a website that pairs with a native app version, ... After iOS 8, however, Apple deprecated...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found