turbolinks / turbolinks-ios

Native iOS adapter for building hybrid apps with Turbolinks 5
MIT License
881 stars 92 forks source link

Image upload #91

Closed mityakoval closed 7 years ago

mityakoval commented 7 years ago

Greetings!

Can't make image upload work in the iOS app.

'Choose file' dialog is opened. But choosing from photo library raises this:

[Generic] Creating an image format with an unknown type is an error

And taking a photo with camera either does not do anything or I get this:

JavaScript error: Error: Existing connection must be closed before opening (http://mityakoval.com/assets/application.js:9:8778)

I tried to implement didFinishPickingMediaWithInfo callback, but it never gets executed:

extension ViewController: UIImagePickerControllerDelegate {
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        print("\nPicked an image\n");
    }
}

Does anyone have any suggestions on how to handle this issue?

Thank you in advance!

pmaojo commented 7 years ago

I have the same problem. Im reading this http://stackoverflow.com/questions/39009889/xcode-8-creating-an-image-format-with-an-unknown-type-is-an-error

zachwaugh commented 7 years ago

How are you initiating the file upload, from the web or native? I'd have to see some more code, but this shouldn't be related to anything in Turbolinks.

mityakoval commented 7 years ago

@zachwaugh In my case file upload is initiated from the web. It's a <input type='file'> tag. Other than this code nothing is handling file upload in my iOS app:

extension ViewController: UIImagePickerControllerDelegate {
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        print("\nI have picked an image\n");
    }
}
zachwaugh commented 7 years ago

No native code will be called if you're initiating the upload from the web. That should be handled entirely by the web view. That UIImagePickerControllerDelegate is only when you're natively instantiating a UIImage​Picker​Controller and presenting it.

I'm going to close this because it doesn't seem like a Turbolinks issue. If you get it working in Safari, but then doesn't work with TL, let me know and I can take a look at what's going on.

mityakoval commented 7 years ago

@zachwaugh The thing is that it works in Safari on mobile. The file picker is opened and I can either take a photo or choose from library. And it successfully updates the avatar:

But in the turbolinks app I get the file picker, but when I choose a photo nothing happens:

pmaojo commented 7 years ago

I ended using this https://github.com/nabilfreeman/ios-universal-webview-boilerplate this has the uploads working. Remember to include in info.plist the needed keys, becouse that could be the problem.

I would really love a much more complete turbolinks starter for xcode 8 and swift 3.

pbmarcano commented 7 years ago

@zachwaugh I too have this working perfectly fine for Safari on Mobile but not on the TL app. I can use my camera or the file picker.

When trying to choose photos from the photo library on the TL app this surfaces in my console: 2017-07-18 14:19:12.361990-0400 Slap[29440:7703802] [Generic] Creating an image format with an unknown type is an error

And when trying to take a photo through the TL app this surfaces in my console: 2017-07-18 14:18:52.246862-0400 Slap[29440:7703802] [App] if we're in the real pre-commit handler we can't actually add any new fences due to CA restriction

Both will appear to update the file field on the webview, displaying the filenames of the photos, but once I hit submit the form never processes. It just hangs.

The forms are using XHR and submit fine when leaving the file field blank. If you need gists or access to the repo I'll gladly provide but as of now I am left scratching my head.

@mityakoval have you discovered a solution to this?

zachwaugh commented 7 years ago

I commented this on #91, but I'll add it here as well.

It's hard to troubleshoot without a reproducible sample I can run. I don't see anything in Turbolinks specifically that should affect this. A few questions/suggestions:

pbmarcano commented 7 years ago

@mityakoval, So I was able to upload the photo by choosing camera/library, and the form never submitted.

Apparently <input type="file"> tags by default struggle to submit over XHR. Since Turbolinks for iOS prevents standard HTML form submissions, my form was not submitting when I tried to pass a file.

I'm using Rails for my app so I was able to add the remotipart gem to help the file field submit on iOS.

Is there a better solution, @zachwaugh?

zachwaugh commented 7 years ago

We tend to do uploads natively or via XHR, but I don't have any general recommendations there. If Turbolinks.framework is preventing some behavior you need though, I'd recommend implementing your own WKNavigationDelegate and then you'll have full control.

annieogrady commented 7 years ago

@mityakoval - any progress on your resolving this issue? We have similar issue.

unknown-undefined commented 5 years ago

If you call WKWebView.reload in UIViewController.viewWillAppear? WKWebView would refresh, then the files gone.

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    if let presentedVC = self.presentedViewController,
        presentedVC is UIImagePickerController {
        // files
        // no op

    } else {
        // not files

        refreshWebView()
    }
}