pwlin / cordova-plugin-file-opener2

A File Opener Plugin for Cordova
MIT License
313 stars 585 forks source link

Files not opening on iOS #2

Closed scottdc closed 10 years ago

scottdc commented 10 years ago

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 commented 10 years ago
scottdc commented 10 years ago

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 commented 10 years ago

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 commented 10 years ago

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 commented 10 years ago

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 commented 10 years ago

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 commented 10 years ago

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

any2info commented 8 years ago

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 commented 8 years ago

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

fileURL = [NSURL URLWithString:path];

to:

fileURL = [NSURL fileURLWithPath:path];

Does that help?

any2info commented 8 years ago

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 commented 8 years ago

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

any2info commented 8 years ago

That solved it. Thank you very much.

pwlin commented 8 years ago

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

ronycohen commented 8 years ago

Hi,

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

vdias38 commented 7 years ago

@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 commented 6 years ago

@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 commented 6 years ago

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 commented 6 years ago

@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 commented 6 years ago

@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 commented 6 years ago

@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 commented 6 years ago

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

Belheni commented 6 years ago

@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 commented 6 years ago

@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 commented 5 years ago

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 commented 5 years ago

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?