apache / cordova-plugin-file

Apache Cordova File Plugin
https://cordova.apache.org/
Apache License 2.0
741 stars 756 forks source link

File access with cordova-ios 6.0.0 #394

Open Polm90 opened 3 years ago

Polm90 commented 3 years ago

I use the file plugin and I access try to access to local files via this:

let path = cordova.file.dataDirectory + nameFile; window.resolveLocalFileSystemURL(path, success, error);

and with cordova-ios 6.0.0 I always receive error (NOT_FOUND_ERR). I do not explicitly use the "file://" scheme, but actually, if I inspect the code at runtime, I see that path has the following value:

"file:///Users/[username]/Library/Developer/CoreSimulator/Devices/E56CD54C-AA0C-43F8-BB37-9D91CA3754C1/data/Containers/Data/Application/683C763A-1710-4729-83E0-DC693A142188/Library/NoCloud/[filename]" So, maybe, I just need to change some preference in the config.xml or to do something else simple.

I also tried to read this blog post and I tried to add the preference options indicated but they didn't work (probably, I've missed something or I've not understood something properly).

I'm also using the ionic-webview

Thank you for any advice you can give me!

caleb87 commented 3 years ago

I have the same issue. It works on Android but fails with error code 1 on iOS.

Returns error 1 for path: cordova.file.applicationDirectory + 'www/file.json'

stephio commented 3 years ago

Anybody has a solution for this?

Polm90 commented 3 years ago

@stephio rolling back to cordova-ios 5.1.1 :D

GitToTheHub commented 3 years ago

@Polm90 Is this possible? Because Apple has the requirement since April to compile Apps with XCode 11. Does cordova-ios 5.1.1 compile with XCode 11? I'm asking this, because I'm building in the cloud and not by myself on a machine :)

breautek commented 3 years ago

@Polm90 Is this possible? Because Apple has the requirement since April to compile Apps with XCode 11. Does cordova-ios 5.1.1 compile with XCode 11? I'm asking this, because I'm building in the cloud and not by myself on a machine :)

Official support for xcode 11 comes since cordova-ios@6, but I've successfully used xcode 11 on cordova-ios@5.x in the past.

Polm90 commented 3 years ago

I am currently use Cordova 5.1.1 to compile with xCode 11 and I have no issues

GitToTheHub commented 3 years ago

Ok thank you both for the information, I'm glad to hear this and in the worst case I can switch to cordova-ios 5.1.1 back. Because I'm having trouble too with WKURLSchemeHandler support. But I'm trying now not to use it and to use cordova-ios 6.0.0 anyway to build,. I will see if that's works to get the old behaviour back :)

GitToTheHub commented 3 years ago

Yeah, not using WKURLSchemeHandler seems to get the old behaviour back, though I'm using cordova-ios 6.0.0. I just commented following out:

<!-- <preference name="scheme" value="app" />
<preference name="hostname" value="localhost" />-->

I removed the "cordova-plugin-wkwebview-engine"-Plugin and everything is working fine. So no problems to upgrade to cordova-ios 6.0.0, it's just you can't use correctly the WKURLSchemeHandler, it's too buggy sadly :(

GitToTheHub commented 3 years ago

Just for sharing my experiences with cordova-ios 6.0.0 and WKURLSchemeHandler:

After setting my own scheme and hostname in config.xml to app& localhost, the javascript window.location returns app://localhost and document.currentScript.src the same app://localhost/.. , but cordova.file.applicationDirectory returns a file-url.

However, if I try to resolve the file-URL it's functioning. I tested it a lot with different file-URLS (files and directories) and everything was good. I only didn't tested it with cordova.file.dataDirectory, only with cordova.file.applicationDirectory, but I think it should work either.

The error is more, when you try to resolve an app://localhost-URL with resolveLocalFileSystemURL, this does not work you get an ENCODING_ERR. But vice-versa, if you try to add programatically with javascript on your current html document a script- or link-element with a file-url, this doesn't work. Even worse, the onload, or onerror-method is never called on your element (this is definetly an error!). But you can load your files with app://localhost in your document with a script- or link-element, this works.

breautek commented 3 years ago

I'm not really an expert with iOS development but there was a discussion on slack similar to this, and I think using WKWebKit.convertFilePath to convert file:// urls to cdvfile:// urls was a workaround.

The original slack discussion was about xhr requests, but I'm making an assumption it will help if you try using these urls in the html dom (such as in an <img> tag) as well.

The full slack discussion can be found at: https://cordova.slack.com/archives/C068CHRJ5/p1591718503254000

GitToTheHub commented 3 years ago

Thank you for the hint of WKWebKit.convertFilePath. I didn't knew, there exists such a JS-function now. I already saw here and there the use of cdvfile:// and that this could be a solution. Do you know, if I must add the cdvfile-protocol like the app-protocol to my config.xml, or does it function without it?

Respective Slack: I can't view the discussion, because the workspace-administrator must invite me and I don't know how I can do that :)

breautek commented 3 years ago

Do you know, if I must add the cdvfile-protocol like the app-protocol to my config.xml, or does it function without it?

I'm not sure, this is not something I use in my apps. You may have to experiment. Any file I write/manipulate with the file plugin, I use the file reader API to read it. Which does lead to another work around, which is reading blobs and using object urls, but the convertFilePath looks much more cleaner, and might work better with larger files... like videos.

I can't view the discussion, because the workspace-administrator must invite me and I don't know how I can do that :)

Oh that's odd... our slack is public via http://slack.cordova.io/ maybe you need to register first to see the discussion.

GitToTheHub commented 3 years ago

Yeah thank you, I'm already using the solution with the object urls. I think i will continue using this and wait what will happen in the meantime with cordova-ios and the file-plugin.

Regarding Slack: Thank you. I could now successful register me there :)

AraHovakimyan commented 3 years ago

@Polm90 For Cordova iOS 6.x.x can be used

cordova plugin add https://github.com/AraHovakimyan/cordova-plugin-wkwebviewxhrfix

It works fine for me.

kapilrc commented 3 years ago

If you are using @AraHovakimyan's Plugin, make sure you remove below preference from project's config.xml

<preference name="scheme" value="app" />
<preference name="hostname" value="localhost"  /> 

this works seamlessly with Cordova iOS 6.x.x & node v10.0.0