Un-deprecating UIWebView in Xamarin Forms


Un-deprecating UIWebView in Xamarin Forms

Date first published: 29th May 2020

Introduction

Since the 30th of April, many software houses and developers using Microsoft Xamarin will have fallen foul of the dreaded 'Deprecated UIWebView' issue which stops new apps being released to the Apple Appstore!! Microsoft has provided a fix in the latest versions of Xamarin but upgrading is fraught with dangers and may not even be possible for many companies. As a result, businesses are now faced with the painful decision of either dropping IOS support or significantly delaying the launch of their new apps.

Having spent hours trying to unsuccessfully upgrade Xamarin I fixed the problem directly within the old version of the Xamarin code myself. It took less than 40 minutes before I had a working build deployed to the Apple store with minimal disruption to my own code.

In this latest article of my Streetwise Tech series I present the solution in easy to follow steps with screenshots and samples taken from live apps. I also describe some of the issues encountered in the upgrade attempt to help anyone considering that route.

Please note that this solution works because I'm not using a WebView however it may offer useful insights to those who do but are still facing a challenge with upgrading.

Current Situation

I'm running Xamarin Forms version 2.4 with Microsoft Visual Studio 2015. I kicked off a project 2 years ago building a game to run on Apple, Google and Amazon. My game, Otherworld: Epic Adventure, was successfully deployed in March and now I want to release a number of the sub games as separate apps in their own right.

Getting these games out quickly is an essential part of my marketing strategy. Besides, the code is all working and I don't want to redesign or redevelop it right now.

The other point I must mention is that I don't own an Apple Mac, (yes it really is possible to build IOS apps without having one), I run all my tests initially against Windows and Android and then I use Microsoft App Center to perform the IOS build. Only after the app is available on the Apple App Store do I then test it on an IPhone.

Problems Upgrading

Depending on your situation, upgrading to the new version of Xamarin Forms (Microsoft recommends 4.6 or higher) may have a massive impact on your code base and your delivery timescale. Only after loosing most of my hair in the attempt did I decide to rethink and go with the solution I present in this article.

However, here are some of the bigger problems I encountered trying to move from Xamarin Forms version 2.4 and Visual Studio 2015 to the latest version 4.7. These issues have solutions but they are time consuming or may have a large impact to the project.

1. Nuget Package Manager

The Nuget Package Manager in Visual Studio 2015 doesn't recognise version 4.6 as the package name format is unsupported. Straight away there is an issue as the maximum version of Xamarin that 2015 recognises is 4.5 which doesn't contain the necessary fix.

I fixed this by installing the Nuget packages from the command line but this causes the package manager UI inside VS to break and I'm forced now to use the command line for all Nuget tasks.

2. Broken User Interface

The new version of Xamarin lays out the screen differently. Immediately after installing the latest Xamarin Forms and rebuilding I find that none of my screens display. Everything is showing a black background with no controls. First major breaking change - I'm going to have to tweak if not re-code all my screens to get them to work which will require everything to be retested.

3. Unsupported Project Types

Visual Studio 2015 didn't cut the mustard so I then tried to use 2019 which seems sensible as it should easily support the latest versions of Xamarin.

2019 happily ported over my older solution and project files but a number of my projects wouldn't load. Most notably the Windows build as well as the UML design project. One of the reasons I started this with 2015 (I could have used 2017) was because I wanted the UML designer which had been deprecated in 2017.

4. Android SDK Death Star

The new version of Xamarin also wanted a new version of the Android SDK. Visual Studio 2019 kindly downloaded it for me in the background - taking up 45GB of disk space in the process. Actually, it used all my C drive, crashed Visual Studio and corrupted the Android installation while doing it.

Fixing The Issue

The crux of the problem is a control inside Xamarin Forms that references the UIWebView class from the Apple SDK. The UIWebView is deprecated and all references to it must be removed from Xamarin Forms before a new app can be uploaded.

The Microsoft article contains full details of the issue:

https://devblogs.microsoft.com/xamarin/uiwebview-deprecation-xamarin-forms/

The Solution - step by step

So how can this actually be fixed? Here is a step by step guide with screenshots to help.

1. Get the Xamarin code

This issue is in Xamarin.Forms and in particular the Xamarin.Forms.Platform.IOS project.

  • Download the Xamarin source, it can be obtained from GitHub. Here is the link to the version I've got however its easy to pick the right one for your project:

https://github.com/xamarin/Xamarin.Forms/tree/2.4.0/Xamarin.Forms.Platform.iOS

  • The code is nice, well organised and within a solution and folders. Locate the Xamarin.Forms.Platform.IOS project and copy the whole folder over into your existing solution.

2. Fix Project References

  • Open the Nuget Package Manager and install the correct version of Xamarin Forms against the Xamarin.Forms.Platform.IOS project to make sure that all the references are to the same libraries:
  • When this has finished open the Xamarin.Forms.Platform.IOS references folder in your solution and remove the self reference. This picture shows the references that should be left:
  • Finally change the reference in the IOS version of your own app project to our new version of Xamarin.Forms.Platform.IOS.

3. Change the Code

Now we've got the projects and references sorted out its time to actually change the code.

  • Locate the offending WebViewRenderer class within the Xamarin.Forms.Platforms.IOS project which derives from UIWebView.
  • We can't just delete the code, instead replace it with something else that doesn't use the UIWebView, in the screenshot below I've simply derived it from PageRenderer.
  • Obviously if you require a WebView control then you will have to get more creative at this point and potentially look to implement it using the new base class or maybe even import the new code from the Xamarin 4.7.
  • The code should now build correctly, with your projects correctly referencing our new updated version of Xamarin.Forms.Platform.IOS.

4. Deploy to the Store

Once the IPA file is built you can go ahead and upload it, as you can see its accepted by the App Store.


I was also able to distribute it via TestFlight and get it down successfully onto my IPhone.

Conclusion

At the start I didn't even think about modifying Xamarin itself but actually it was extremely easy. 40 minutes later I had a working build on the AppStore and now I also have my own version of Xamarin. This gives a lot of control and the ability to fix minor issues in the platform rather than work around them or blindly upgrade to new versions.

I will of course use the latest version with new projects, but I can now complete my current work without major disruption to the code or timescale.

I hope you have found this article useful. If there is any information missing then please leave a comment and I'll update the article. Also, please check out my other article on designing mobile apps to support testing or find out about my mobile project Otherworld: Epic Adventure.

Thanks and stay safe!

Graeme Clarke


.