0

I am using xamarin.plugin.filepicker

it works fine with android. but with IOS (iPhone 6+) it did not work.

once it come to this code:

var file = await CrossFilePicker.Current.PickFile();

it stuck and does not continue, the application not freese, it works fine, but not get any result from "await CrossFilePicker.Current.PickFile();".

Even if i click "cancel" or select a file, nothing let the code go after this line:

var file = await CrossFilePicker.Current.PickFile();

There are no errors, nothing at all.

I am using the last stable version of the plugin, then I also tried "2.1.14-beta".

the question is : why the code does not go to the "if (file != null)" statement ever?

var file = await CrossFilePicker.Current.PickFile(); << Stop here

if (file != null) << Not come here ever

Thanks in advance

Flying light
  • 183
  • 2
  • 11
  • Can you simply create a new project and try it again? I just create a new project and installed xamarin.plugin.filepicker 2.0.135. And add only two line there: `var file = await CrossFilePicker.Current.PickFile(); if (file != null)`, it works for me. And I run on a simulator. Any difference? – nevermore Apr 11 '19 at 06:52
  • Thank you dear Jack Hua - MSFT. Please can your check and confirm that you code hit "if (file != null)". As it is normal that you will see the file picker, and also can select a file. But you will not hit "if (file != null)" ever. The code stay at "var file = await CrossFilePicker.Current.PickFile();" forever. If you select a file, or even click "cancel" it will not hit the next line of code ever. I tried on simulater and real iphone and same result for me. – Flying light Apr 11 '19 at 07:02
  • You can simply add a breakpoint, or display an alert after this line "var file = await CrossFilePicker.Current.PickFile();" and see if it will reach there or not. – Flying light Apr 11 '19 at 07:03
  • Yes, it did hit. I add a `breakpoint` there and I can see the file is null. How did you check that it doesn't hit? A breakpoint? Log something? – nevermore Apr 11 '19 at 07:04
  • I add a breakpoint after the "await" and it never hit. Using android it can hit. Also, I have a busy indicator, I set it to busy SetBusy(true) before the "await" and reset it after the "await" SetBusy(false). the breakpoint after "await" never hit, and the busy indicator remains busy forever. – Flying light Apr 11 '19 at 07:12
  • As I suggested, create a new project and test it on iOS again first. – nevermore Apr 11 '19 at 07:15
  • I am creating a new one right now, I will let you know the result in 5 minutes. Regards – Flying light Apr 11 '19 at 07:17
  • Sure. Does it work your new project? – nevermore Apr 11 '19 at 07:28
  • It works !!! how, and why!! :) Now What is wrong with my other project!? – Flying light Apr 11 '19 at 07:34
  • Share you code please. – nevermore Apr 11 '19 at 07:35
  • In the new project I call the code from a button click. In my current project I call it from a javascript callback "formswebView.AddLocalCallback("ShowFilePicker", (str) => ShowFilePickerAsync(str));". I also use Device.BeginInvokeOnMainThread to use the main thread. I just tried to call it from a button click in my main project and it works. What is the problem with javascript callback? it works in android with no errors. – Flying light Apr 11 '19 at 07:45
  • Can you share the code of that part? – nevermore Apr 11 '19 at 07:52
  • Is it allowed to send links in "stackoverflow.com", can I upload it for you? – Flying light Apr 11 '19 at 07:53
  • You can upload it to Github. – nevermore Apr 11 '19 at 07:54
  • Allow me 10 minutes to upload it, please – Flying light Apr 11 '19 at 07:58
  • Finally, I am apple to solve it. I will post the answer. Thanks dear Jack Hua - MSFT, your suggestion to create new project focus me to find the problem. – Flying light Apr 11 '19 at 11:03

2 Answers2

0

I can't write a comment... Do you have set the Permissions on IOS in the Info.plist to get access to the Libary?

<key>NSCameraUsageDescription</key>
<string>This app needs access to the camera to take photos.</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>This app needs access to photos.</string>
<key>NSMicrophoneUsageDescription</key>
<string>This app needs access to microphone.</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>This app needs access to the photo gallery.</string>

IOS Privacy Permissions Blog Post

TosT
  • 43
  • 5
  • Thanks TosT But do not you think it must raise any permission error? I will give it a try and let you know – Flying light Apr 10 '19 at 12:55
  • Hello TosT. I just test your suggestion, it did not work. I can take photo with the camera, I can select any file from the phone, this is not a problem. The problem is that the code did not go to the next step, whatever I select a file, use camera, or even click cancel. It keep inside "await CrossFilePicker.Current.PickFile();" forever. – Flying light Apr 10 '19 at 13:43
  • I had a look at the githubpage from the plugin: https://github.com/jfversluis/FilePicker-Plugin-for-Xamarin-and-Windows In the "Important" section the Autor says iOS: You need to Configure iCloud Driver for your app. Have you done this? – TosT Apr 10 '19 at 14:03
  • I really appreciate your continued with me and try to help me, thank you very much. Unfortunately I already put all the necessary settings, and everything was set up exactly as desired. The problem is, for example, I took a picture with the camera, it seems that there is nothing wrong happen, and everything go nice. The program then should go to the next line of code to read this photo bytes array, even if there is an error it must leave the "wait" line. But unfortunately stays inside the "wait" line. – Flying light Apr 10 '19 at 14:15
  • If I open the file picker window, and then click the "Cancel" button, the program should go to the next line, and the file will be null, which is not what happens. The program remains inside the "wait" line, and never moves. The program does not crash and ramain runing. Any suggestions? – Flying light Apr 10 '19 at 14:15
  • Yes, it is, and never went to the "catch(Exception e)" part. It works fine with Android, the problem is with IOS only. – Flying light Apr 10 '19 at 14:28
0

Finally, I fix the problem

I was using javascript callback to fire the file picker event. The problem was in the html code, I used "a" object as you can see:

<a href="#" 
        onclick="UploadImage();" 
        style="text-decoration:none;" data-ajax="false" data-role="none">
</a>

it works find on android, but stuck on IOS. I noticed that the (href="#") fires "FormswebView_OnNavigationStarted" event using IOS. this make two events fire sametime, FormswebView_OnNavigationStarted + my javascriptcallback (ShowUploadAppImagesAsync). that is why it stuck there and did not hit the next line.

Once I changed the "a" object to "div" object, it works fine.

<div 
        onclick="UploadImage();" 
        style="text-decoration:none;" data-ajax="false" data-role="none">
</div>

hope this can help anyone

Thanks to "TosT" and "Jack Hua - MSFT"

Regards

Flying light
  • 183
  • 2
  • 11