iOS File Transfer Download not working

  • 1
  • Question
  • Updated 3 years ago
Hello, with the help of some members of this community, I was able to access the file system and create folder in iOS, but when I try to download a file from an API (server) to that folder, it does not work on iOS but works fine on Android. I am using PhoneGap Build

My current config file:


<?xml version="1.0" encoding="utf-8"?>
<widget xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0" id="com.app.reflex" version="0.0.1">
<icon src="img/icons/icon57.png" width="57" height="57" />
<icon src="img/icons/icon57.png" gap:platform="ios" width="57" height="57" />
<icon src="img/icons/icon72.png" gap:platform="ios" width="72" height="72" />
<icon src="img/icons/icon114.png" gap:platform="ios" width="114" height="114" />
<icon src="img/icons/icon120.png" gap:platform="ios" width="120" height="120" />
<icon src="img/icons/icon144.png" gap:platform="ios" width="144" height="144" />
<icon src="img/icons/icon152.png" gap:platform="ios" width="152" height="152" />
<name>Reflex</name>
<description>App.</description>
<author href="http://cordova.io" email="customerservice@mydomain.com">App</author>
<content src="index.html" />
<access origin="*"/>

<preference name="phonegap-version" value="3.7.0" />

<preference name="permissions" value="none" />
<preference name="orientation" value="portrait" />
<preference name="target-device" value="universal" />
<preference name="fullscreen" value="true" />
<preference name="webviewbounce" value="true" />
<preference name="prerendered-icon" value="true" />
<preference name="stay-in-webview" value="false" />
<preference name="ios-statusbarstyle" value="black-opaque" />
<preference name="detect-data-types" value="true" />
<preference name="exit-on-suspend" value="false" />
<preference name="show-splash-screen-spinner" value="true" />
<preference name="auto-hide-splash-screen" value="true" />
<preference name="disable-cursor" value="false" />
<preference name="android-minSdkVersion" value="7" />
<preference name="android-installLocation" value="auto" />
<preference name="SplashScreen" value="screen" />
<preference name="windows-target-version" value="8.0" />

<gap:plugin name="org.apache.cordova.camera" source="npm"/>
<gap:plugin name="org.apache.cordova.media-capture" source="npm"/>
<gap:plugin name="org.apache.cordova.contacts" source="npm"/>
<gap:plugin name="org.apache.cordova.device" source="npm"/>
<gap:plugin name="org.apache.cordova.file" source="npm" />
<gap:plugin name="org.apache.cordova.file-transfer" source="npm" />
<gap:plugin name="org.apache.cordova.media" source="npm"/>
<gap:plugin name="org.apache.cordova.splashscreen" source="npm"/>
<gap:plugin name="org.apache.cordova.vibration" source="npm"/>
<gap:plugin name="cordova-plugin-flashlight" source="npm" version="3.0.0"/>
<gap:plugin name="cordova-plugin-whitelist" source="npm" />
<gap:plugin name="org.apache.cordova.dialogs" source="npm"/>
<gap:plugin name="org.apache.cordova.inappbrowser" source="npm"/>

<icon src="icon.png"/>

<plugin name="InAppBrowser" value="CDVInAppBrowser" />

</widget>


My download code, this below saves a file with format = "name.originalExt.pdf" so something like test.doc.pdf.


function startDownloadProcess(fileurl, fn) {
// alert("start process: fileurl: " + fileurl + " filename: " + fn);
wait();
fullFileURL = fileurl;
window.resolveLocalFileSystemURL(docPath.toURL() + fileName, fileExists, downloadAsset(fileurl, fn));
}

// download file
function downloadAsset(url, fn) {
var options = new FileUploadOptions();
options.mimeType = "application/json";
options.httpMethod = 'GET';
var headers={'auth': window.localStorage.getItem("auth")};
options.headers = headers;

var fileTransfer = new FileTransfer();
alert("About to start transfer ... " + docPath.toURL() + "Documents/" + fn);
fileTransfer.download(encodeURI(url + '//' + window.localStorage.getItem("auth")), docPath.toURL() + "Documents/" + fn + ".pdf",
function (entry) {
alert("Download complete.");
},
function (err) {
alert("Download error code: " + err.code);
}, options
);
}


Any suggestions?

Thanks

Yuriy
Photo of Yuriy Mirskiy

Yuriy Mirskiy

  • 40 Posts
  • 0 Reply Likes

Posted 5 years ago

  • 1
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
1. You should use the names of the plugins as mentioned on NPM.
2. Remove the 'plugin' element in your config. It is invalid
3. which callback function is called when you run that code? "It does not work" is not something developers say. Be specific! You don't bring your car to a mechanic and say "car doesn't work", either, do you?
Photo of Yuriy Mirskiy

Yuriy Mirskiy

  • 40 Posts
  • 0 Reply Likes
When you say I should use the names of the plugins as mentioned on NPM, can you provide an example?

Also, the gap:plugin element needs to be removed? If yes, how would I add the plugins to PhoneGap Build?

What does not work is the actual FileTransfer()'s download function. But it only does not work for iOS, it works fine for Android. I am able to download the file into the specified folder. On iOS, this alert is the only thing that gets called:

alert("About to start transfer ... " + docPath.toURL() + "Documents/" + fn);

What is strange, the download function's callback for success or error are not called either. It gets stuck on download function and that's it.

Thanks for your response and help

Yuriy
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
1. Plugins in NPM are called 'cordova-plugin-....'. Just go to NPM and find the plugins that you need. You will find the proper names, there.

2. I didn't say "remove every gap:plugin element", but just the "plugin" element from your config.

3. Could it be that the download just hangs in iOS9? Have you tried previous iOS versions?
Photo of Yuriy Mirskiy

Yuriy Mirskiy

  • 40 Posts
  • 0 Reply Likes
Got it, I removed the <plugin name="InAppBrowser" value="CDVInAppBrowser" /> element.

The original version that I tried on was 9.1. I tried to run it on an emulator using 8 but the app won't run at all.
Photo of Yuriy Mirskiy

Yuriy Mirskiy

  • 40 Posts
  • 0 Reply Likes
@Petra, also, the idea behind my app is to have it working on the latest iOS version anyways. Do you know why it would hang?
Photo of Yuriy Mirskiy

Yuriy Mirskiy

  • 40 Posts
  • 0 Reply Likes
Also, please note, I am using PGB 3.7.0
Photo of Yi Ming Kuan

Yi Ming Kuan

  • 1840 Posts
  • 78 Reply Likes
Does the same error occur on cli-5.2.0?

-yiming
Photo of Yuriy Mirskiy

Yuriy Mirskiy

  • 40 Posts
  • 0 Reply Likes
I am sorry for not responding sooner, I was away from this project and working on another one (much needed rest). I tried this with cli-5.2.0 and still the download function is not working. It does not fire up an error message nor the success message.
Photo of Yuriy Mirskiy

Yuriy Mirskiy

  • 40 Posts
  • 0 Reply Likes
I figured out the issue, I removed the "options" from the download function (the parameter) and it worked on iOS and Android. Thanks everyone for your help.
Photo of Ulysses Alves

Ulysses Alves

  • 1 Post
  • 0 Reply Likes
Hey, Yuriy, I see this is an 2 years old post, but I'm getting the same error in a phonegap project I've developed around May/April 2017. It works great for Android, but the onprogress event is not being fired in iOS, so the download never starts and the progress bar is never filled up.

Do you still remember in code level detais how you got this working? If so, could you please share your solution with us? Thanks.
(Edited)