Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Files not opening on iOS #2

Closed
scottdc opened this issue Mar 5, 2014 · 25 comments
Closed

Files not opening on iOS #2

scottdc opened this issue Mar 5, 2014 · 25 comments

Comments

@scottdc
Copy link

scottdc commented Mar 5, 2014

Been trying to get this working for a bit now and am stumped. Files do not open but I also do not get an error or anything, nothing happens at all. I've got the event writing to the console and it appears to be firing correctly but I get neither a success or failure message. I've got this same basic case for multiple file types (pdf, doc, etc.) based on extension and none of them work.

case 'rtf':
cordova.plugins.fileOpener2.open(fullPath, 'application/rtf', {
error : function(errorObj) {
console.log('error - ' + errorObj.status + ' - ' + errorObj.message);
},
success : function () {
console.log('sucess');
}
});
break;

@pwlin
Copy link
Owner

pwlin commented Mar 5, 2014

  • What iOS version and device are you using?
  • Which app have you installed on your iOS device for reading rtf files?
  • Is fullPath a valid path? Does the file really exists there?

@scottdc
Copy link
Author

scottdc commented Apr 4, 2014

Sorry it took a while to get back to you on this. So I was in fact having a path issue and have that fixed but now whenever I attempt to open a file the app crashes.

I am testing on iOS7.1 iPhone 4.

Could still be a path issue, I have tried both of these for testing trying to open a PDF with Adobe Reader with the same result:

/Documents/test.pdf

and the full path from root:

/var/mobile/Applications/ . /Documents/noodles.pdf

but am getting a sigabrt at this line:
docController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];

@zatch
Copy link

zatch commented Apr 15, 2014

I seem to be having the same issue. I do get an "Open with..." dialog if my file path starts with "file://", but the file doesn't open after making a selection. My attempts to use paths without schemas/prefixes have all caused my app to crash like scottdc's.

I'm still learning how to navigate the iOS filesystem, so I could just be using the wrong path, but I figured I'd chime in with my experience so far... Any clues you find toward a solution would be much appreciated -- I'll be sure to post my own findings here, as well.

Update: I tested opening my file with the Mail app, just to see what would happen. All it did was create a new email with the name of my file in the subject line. The subject line was only the name, not the full path. It didn't even add the file as an attachment (which isn't the surprising, but I thought it worth mentioning).

@zatch
Copy link

zatch commented Apr 21, 2014

I finally got this working! Some recent changes in Cordova require some extra steps to find the absolute system path for a file. For a File Entry, instead of using toURL() or fullpath, you have to use fileEntry.toNativeURL() to get the full system path. Then you can just use the full system path to open the file in whatever apps can open the file type. :)

@tomraithel
Copy link

Hey, I have a similar problem here. I´m using the example code but I don´t get any error or success - just nothing happens. This is my code:

cordova.plugins.fileOpener2.open(
    currentFileUrl, // e.g. '/var/mobile/Applications/XXXXXXXXX/Library/files/mypdf.pdf'
    'application/pdf',
    {
        error : function(errorObj) {
            alert('Error status: ' + errorObj.status + ' - Error message: ' + errorObj.message);
        },
        success : function () {
            alert('file opened successfully');
        }
    }
);

I also tried file urls with prefix (file://) from fileEntry.toNativeUrl() like @zatch explained, but nothing happens here.

@zatch can you give me an example with the code that worked for you?

Thanks
Tom

@tomraithel
Copy link

Uh, I found the problem. In my config.xml I used following preference:

<preference name="iosPersistentFileLocation" value="Library" />

It seems, that this does not work well with the plugin. After removing this line, it worked.

@pwlin
Copy link
Owner

pwlin commented May 18, 2014

@tomraithel Thank you very much, I added this tip as a note to the readme file.

@pwlin pwlin closed this as completed May 18, 2014
@any2info
Copy link

Hey, I have this exact problem. I'm trying to open a pdf file that has been downloaded to my phone.

When execute the following i get a dialog to select the app that should be used:

            cordova.plugins.fileOpener2.open(
                uri,  //cdvfile://localhost/persistent/myPDF.pdf
                'application/pdf',
                {
                    error: function (e) {
                        console.log('Error status: ' + e.status + ' - Error message: ' + e.message);
                    },
                    success: function () {
                        console.log('file opened successfully');
                    }
                });

I've checked the following:
The succes callback is hit
My config.xml doesn't contain iosPersistentFileLocation preference setting

I'm working with apache cordova tools for VS2015 (CLI 5.1.1) and i'm trying to use ibooks to open the pdf.

I recently upgraded to vs2015 after which this problem arose, so i suspect this to be some kind of cordova configuration issue. Can someone help me with this?

Thanks
A2I

@pwlin
Copy link
Owner

pwlin commented Oct 25, 2015

Try to change plugin's ios source (FileOpener2.m) line:

fileURL = [NSURL URLWithString:path];

to:

fileURL = [NSURL fileURLWithPath:path];

Does that help?

@any2info
Copy link

I found the following lines in the FileOpener2.m file:
//fileURL = [NSURL URLWithString:path];
fileURL = [NSURL fileURLWithPath:path];

I tried enabling the first line (URLWithString) but this resulted in a crash of my app.

@pwlin
Copy link
Owner

pwlin commented Oct 26, 2015

What if you use fileEntry.toURL() in your javascript?

@any2info
Copy link

That solved it. Thank you very much.

@pwlin
Copy link
Owner

pwlin commented Oct 26, 2015

Ok great. I will add a note to README file about using fileEntry.toURL()

@ronycohen
Copy link

Hi,

Where to get the fileEntry.toURL() ?
I don't understand. Do you have an example with fileEntry.toURL() ?

@vdias38
Copy link

vdias38 commented Oct 13, 2016

@ronyrun check code below, and if you need more details about File API I recommend following post: https://www.neontribe.co.uk/cordova-file-plugin-examples/))

window.resolveLocalFileSystemURL(
  path, 
  function (entry) {
    console.log(entry.toURL());
  }, 
  function (err) {
    $log.debug(err);
  }
);

@Belheni
Copy link

Belheni commented Oct 27, 2017

@pwlin and @any2info Can you send me a simple and complete version of opening pdf in ios Please? i really tested all the suggestions(changing in fileOpener, fileEntry.toURL(),fileEntry.toNativeURL()..) and the best version is that i have just a successful message of opening pdf without showing it..I really need a solution for that

@any2info
Copy link

any2info commented Oct 27, 2017

I use the following helper instance to download and open all sort of files.
I removed some code that applied to our internal netwerk but you should be able use this.

I hope this helps anyone out there struggling with this. If i can be of assistance let me know

download helper example.txt

@Belheni
Copy link

Belheni commented Oct 27, 2017

@any2info Thanks for your help but i tested your solution carefully after checking if all plugins exist and nothing is showed.. This is my test really i don't know what's the problem..i will be very thankful if you continue helping me..(if you have a simple project working fine on ios please send it)
TestSolution.txt

@any2info
Copy link

@Belheni I looked at the code snippet you added and this seems fine to me and should work.
The only thing i could find that is off is your invocation of openFile(entry.toURL()). Shouldn't this be OpenFile(entry.toURL())?

Also the false and headers parameter can be removed because you probably don't need them. Which should be the case if you are downloading directly from a web uri. In my case i needed them because we are no longer downloading files using uri's.

If this still doesn't solve anything, i would be happy to look at more of your code if needed.

@Belheni
Copy link

Belheni commented Nov 1, 2017

@pwlin and @any2info i opened the file in ios but how to detect the click done on the file to close it?or how to detect the closing of the file?
another question is that how to display file in ios on the window inappbrowser and not under the window inappbrowser (default)? Please help me

@any2info
Copy link

any2info commented Nov 1, 2017

@Belheni When you want it to open in inappbrowser this should work. Detecting close this maybe

@Belheni
Copy link

Belheni commented Nov 1, 2017

@any2info i work with inappbrowser (for the website) and fileopener2.. i open the PDF file using fileopener2 and it works fines.. my question is how to detect the click done on the file to close it? in android file is displayed on the window inappbrowser but in ios file is displayed under the window inappbrowser.. i want to detect close of PDF to show again the window inappbrowser.. i know that my problem is complicated and i wish that you have a solution

@any2info
Copy link

any2info commented Nov 1, 2017

@Belheni I am afraid im can't help you with that. Maybe you should start stackoverflow item for that since this is no longer is related to files not opening on ios

@pyroxiano
Copy link

The issue I had was with the CGRect passed into presentOpenInMenuFromRect. Changing line 89 to:

CGRect rect = CGRectMake( cont.view.bounds.size.width / 2, 20, 166, 235 );

Worked, and made the dialog appear slightly offset from the screen center.

@juansaa
Copy link

juansaa commented Sep 27, 2018

Hola estoy creando mi app online con PhonegapBuild.
Como agrego el plugin en el config.xml?

Hello, I am creating my online app with PhonegapBuild.
How do I add the plugin in the config.xml?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants