iOS 15.4 got "TypeError" when setting WKWebView address to a custom url scheme

I am using custom url scheme to transfer some data from WKWebView to App.

The JS code I use in WKWebView is similar to this:

try{
  window.location.href = 'bsfit://'+window.some_custom_data;
}catch(err){
  alert(err);
}

Before iOS 15.4, this has worked perfectly.

When tested on iOS 15.4 Beta5, I got an error "TypeError: Invalid URL".

Is this change intended in iOS 15.4 regarding custom URL schemes?

Since I can't change the existing app code, is there a workaround?

Thanks.

  • Hey,

    Do the following:

    Before: window.location.href = 'bsfit://'+window.some_custom_data;

    After: window.location.href = 'https://bsfit/'+window.some_custom_data;

    And access host not scheme on native side.

Add a Comment

Accepted Reply

We solved the problem. Use encodeURIComponent to process the data string.

It looks like this.

try{
  window.location.href = 'bsfit://'+encodeURIComponent(window.some_custom_data);
}catch(err){
  alert(err);
}

For now, this method is compatible with iOS 15.4 and earlier versions (iOS >= 9).

Replies

did you solve it ?

  • encodeURIComponent(window.some_custom_data)

Add a Comment

We solved the problem. Use encodeURIComponent to process the data string.

It looks like this.

try{
  window.location.href = 'bsfit://'+encodeURIComponent(window.some_custom_data);
}catch(err){
  alert(err);
}

For now, this method is compatible with iOS 15.4 and earlier versions (iOS >= 9).