iOS Question WebView LoadUrl of a pdf file doesn't work on iOS 12.5.4.

PierPaduan

Active Member
Licensed User
Longtime User
Hi.
I have an app that use the following code to open pdf files generated by the app itself.
This code was tested on an iPhone with iOS 10 and worked perfectly. This iPhone has been updated to iOS 12.5.4, and the pdf files are no more opened. The pdfView shows only a white page. Anyway the generated pdf file is correct. If I send it to my computer I can open it correctly.
Pleas someone can help me?
Thanks to everyone.
example:
    If File.Exists(File.DirDocuments & "/AAA/Tables", FileList.Get(Cell.Tag)) Then
        pdfView.LoadUrl("file:///" & File.DirDocuments & "/AAA/Tables/" & FileList.Get(Cell.Tag))
        pdfView.ScaleToFit = True
    Else
        hd.ToastMessageShow("pdf doesn't exist", True)
    End If
 

PierPaduan

Active Member
Licensed User
Longtime User
I've just tried this code on a device running the iOS 14.5:
B4X:
File.Copy(File.DirAssets, "aaa.pdf", File.DirDocuments, "aaa.pdf")
WebView.LoadUrl("file://" & File.Combine(File.DirDocuments, "aaa.pdf"))
Works fine.

Thanks Erel.
I have tried your suggestion in a simple test project that I attach here. The project try to open an "Hello World" pdf file when a button is clicked.
The Webview remain white, it doesn't show anything (tested in Debug and in Release too).
The event "WebView1_PageFinished" return Success = False and Url is void.

Please can yout try the test project on your iOS 14.5 ?
Have you any advice?
 

Attachments

  • WebViewLoadUrl.zip
    503.3 KB · Views: 146
Upvote 0

PierPaduan

Active Member
Licensed User
Longtime User
Success = true. Url = file:///var/mobile/Containers/Data/Application/DE2FEF77-7222-4212-ABB8-59839E2F93FD/Documents/helloworld.pdf

Works fine, both on the simulator and on a real device.

Have you tried to restart the device?

Yes, restarted now, but nothing change.
If I log the LoadUrl argument it is: "file:///var/mobile/Containers/Data/Application/7289DF4E-8D5D-43ED-9200-5CDDB227F2CC/Documents/helloworld.pdf", but the WebView1_PageFinished event still return: Success = False and Url = void.

Any advice?
Thanks a Lot.
 
Upvote 0

roumei

Active Member
Licensed User
There seems to be an issue with WKWebView and LoadURL with local files on iOS 12.5.4 (at least). I switched from LoadURL to LoadHTML on iOS < 14. Obviously that's not an option for you.

You can change the folder from File.DirDocuments to File.DirTemp and it should work. Tested on iOS 12.5.4 and iOS 14.6.
 
Upvote 0

PierPaduan

Active Member
Licensed User
Longtime User
There seems to be an issue with WKWebView and LoadURL with local files on iOS 12.5.4 (at least). I switched from LoadURL to LoadHTML on iOS < 14. Obviously that's not an option for you.

You can change the folder from File.DirDocuments to File.DirTemp and it should work. Tested on iOS 12.5.4 and iOS 14.6.

Thanks for your help.
Yes using DirTemp, the LoadUrl opens the pdf file on iOS 12.5.4.
Anyway, for compatibility with the previous app versions and considering that 90% of Apple Devices uses iOS 14, I prefer to keep using DirDocuments.
 
Upvote 0

roumei

Active Member
Licensed User
You can still use File.DirDocuments for everything except for the displaying of the PDF file. It shouldn't be too much effort to copy the PDF from File.DirDocuments to File.DirTemp, display it and delete it after the user has closed the document.
The issue with LoadURL has been around for a while and I doubt that Apple is going to provide a fix.
 
Upvote 0

PierPaduan

Active Member
Licensed User
Longtime User
You can still use File.DirDocuments for everything except for the displaying of the PDF file. It shouldn't be too much effort to copy the PDF from File.DirDocuments to File.DirTemp, display it and delete it after the user has closed the document.
The issue with LoadURL has been around for a while and I doubt that Apple is going to provide a fix.
You are right. Thanks a Lot.
 
Upvote 0

AllanH

Member
Licensed User
Longtime User
I can confirm that this is a bug with IOS 12.5 and Apple have been very sneaky and fixed it in the simulator so you need a real device.
It's a known bug in IOS. Apple "fixed it" in IOS 13.
The OverrideURL will return Success = False and URL = ""
You need to move the files you are loading into the WebView into DirTemp for it to work.
NB you also need to ensure any webpage images are moved to DirTemp as well as the html files.

Since DirTemp can be emptied by the OS at any time, you need to check files are present and re-stock the DirTemp if they have been emptied!

Do watch when moving graphics from DirAssets - I wrote code to copy all my PNG files from DirAssets to DirTemp and it worked fine in debug mode.
However, in release mode, it crashes as it tried to copy some AppIcons that contain "@" in the filename and the IOS said no!
This was really hard to track down as it crashed my app before anything appears - I needed iReleaseLogger running in the Simulator.
The difference between debug/release is due to the virtual assets folder used in debug mode, which does not contain those AppIcons.

Lessons I learned:
  • Get friends/relatives to donate their iPhone from the back of their kitchen drawer that's on an old IOS and don't update it.
  • Don't rely on running your app in a few Simulator IOS versions to be confident it will work on any phone.
  • There are a good % of iPhone users still on IOS <13 either because of worries about battery depletion with newer IOS or because iPhone 5/6 won't upgrade (6s is the oldest model that will upgrade to 13/14).
  • When you have 10's of thousands of happy users, that's a good number of users sending you crash reports at best, or submitted poor reviews at worst.
  • I have built in a menu that allows users to email me bugs. This adds to the email message the Device Model name, IOS version and LastException.Message. This was the clue when I had mysterious bug reports that caused the program to display a white page and failed to work - the Webview was blank and the empty OverrideURL URL entry meant the app also failed to setup correctly. After several emails, I spotted they were all on 12.5.4.
  • Getting users to email you bugs also gives you the option to ask them to help fix it using TestFlight so you don't have to keep releasing new versions to the AppStore.
  • Finally, make sure you compile with a release version onto the simulator and real phones and don't just rely on debug mode testing.
I hope that helps other users.

Allan
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
you need to move the files you are loading into the WebView into DirTemp for it to work.

There is another way, which works at least in current IOS. Put files into Special subfolder (see sample)
 

Attachments

  • s23.zip
    50.3 KB · Views: 144
Upvote 0
Top