0

I have local pdf files in a document directory. My code calls each one to display using WKWebview or PDFViewer, but it only shows on an iPhone XR simulator, but not on any other simulator devices.

Any devices which are older than XR, it shows an error:

libMobileGestalt MobileGestalt.c:890: MGIsDeviceOneOfType is not supported on this platform.

Devices with XS, XS Max, it shows the error:

failed to open pdf ...

as seen in the code.

Does anyone know why?

I use Xcode 10.2.

Here is the code that I use for WKWebviewer

    @IBOutlet weak var webView: WKWebView!
    //only work on XR, don't know why
    override func viewDidLoad() {
        super.viewDidLoad()

        let documentDir = FileManager.SearchPathDirectory.documentDirectory
        let userDir    = FileManager.SearchPathDomainMask.userDomainMask
        let paths      = NSSearchPathForDirectoriesInDomains(documentDir, userDir, true)
        if let dirPath = paths.first
        {

            let pdfURL = URL(fileURLWithPath: dirPath).appendingPathComponent("my file.pdf")
            do {
                //only work in Xr 
                let data = try Data(contentsOf: pdfURL)
                webView.load(data, mimeType: "application/pdf", characterEncodingName:"", baseURL: pdfURL.deletingPathExtension())
                //not working for Xs, Xs Max... later iphone
                //webView.loadFileURL(pdfURL, allowingReadAccessTo: pdfURL)
                //webView.load(URLRequest(url:pdfURL))

                //webView.loadFileURL(pdfURL, allowingReadAccessTo: pdfURL)
                print("open pdf file....")

            }
            catch {
                print("failed to open pdf \(dirPath)" )
            }
            return
        }

and for PDFViewer:

let documentDir = FileManager.SearchPathDirectory.documentDirectory
let userDir    = FileManager.SearchPathDomainMask.userDomainMask
let paths      = NSSearchPathForDirectoriesInDomains(documentDir, userDir, true)
if let dirPath = paths.first
{

    let pdfURL = URL(fileURLWithPath: dirPath).appendingPathComponent("myfile2.pdf")

    view.addSubview(pdfView)

    pdfView.autoresizesSubviews = true
    pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleTopMargin, .flexibleLeftMargin]
    pdfView.displayDirection = .vertical

    //pdfView.autoScales = true

    pdfView.displayMode = .singlePageContinuous
    pdfView.displaysPageBreaks = true

    if let document = PDFDocument(url: pdfURL) {
        pdfView.document = document
    }

    pdfView.maxScaleFactor = 4.0
    pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
JohnMiky
  • 51
  • 5
  • When it fails to load, do you get the “failed to open pdf”? – matt Apr 30 '19 at 05:06
  • The fail to open pdf is only for version XS, XS max, ... any version from XR and above. Any version older XR, generated libMobileGestalt MobileGestalt.c:890: MGIsDeviceOneOfType is not supported on this platform. Do I need to install iOS 11 for each simulator version – JohnMiky Apr 30 '19 at 16:45
  • You are not answering my question. You are using a do/try/catch pattern. I'm asking you: when the Data loading fails, do you catch? – matt Apr 30 '19 at 17:03
  • Yes I do see it catch if loading fail. It did not fail for XR – JohnMiky Apr 30 '19 at 17:28
  • Okay. So in your `catch` clause, after `print("failed to open pdf \(dirPath)" )`, add `print(error)`. Now you'll get a printout of the actual error and you'll have much more of a clue as to what's going wrong here. Basically, by _not_ printing the `error`, you are throwing away the error information and vitiating the entire advantage of the try/catch architecture. – matt Apr 30 '19 at 17:33
  • Matt thank you very much. I found the problem because print(error) YOU HAVE MENTIONED. Thank you so much. Each simulator version save file in different directory document. That is why could not find the file. Thank you EXPERT. – JohnMiky Apr 30 '19 at 17:46

1 Answers1

0

Matt helps me to find out the problem. Thank you Matt. Please see the comments. Each iOS device in simulator has different folder id. When you download to a device simulator document directory, do not expect it could find the same for another simulator device in the same document directory.

JohnMiky
  • 51
  • 5