WebView select file from photo library

时间:2015-11-12 11:53:40

标签: ios swift webview segue

I'm working on a project where I'm using 2 views, MainView and SecondView. MainView is more or less a start pages and then I use the segue function to go from MainView to SecondView.

In SecondView I have WebView and on this website I have a file uploader. So the user should be able to upload images from his or her photo library to the site.

The website is accessed with this code:

if let url = NSURL(string: "https://example.com/uploader") {
        webView.loadRequest(NSURLRequest(URL: url))
    }

The problem is that when I try to upload an image it only segues back to my MainView

enter image description here

So when I press Photo Library it segues back to MainView.

However I recreated the WebView version of the app in a new project with only one view and then everything works like a charm as shown below

enter image description here

So basically my question is, how do I prevent the app from segueing back when I select Photo Library?

Link to Download the project and see for yourself.

5 个答案:

答案 0 :(得分:2)

快速解决此问题的方法是使用WebView将以下代码添加到ViewController:

override func dismissViewControllerAnimated(flag: Bool, completion: (() -> Void)?) {
    if self.presentedViewController != nil {
        super.dismissViewControllerAnimated(flag, completion: completion)
    }
}

这篇文章与您的问题有关: iOS 8 SDK: modal UIWebView and camera/image picker

答案 1 :(得分:2)

当我经常使用ViewControllers时,我正在处理这个问题。

使用ViewController将以下代码添加到WebView确实解决了iOS模拟器中的问题。

override func dismissViewControllerAnimated(flag: Bool, completion: (() -> Void)?) 
{
    if self.presentedViewController != nil {
        super.dismissViewControllerAnimated(flag, completion: completion)
    }
}

但是,在实际设备上运行时仍然存在问题。为了解决这个问题,我在MainViewController中添加了该代码。

希望这可以解决它并快乐编码。

答案 2 :(得分:2)

我尝试使用Sasha Nicolas的黑客,但现在使用新的Swift版本无法正常工作。我现在正在使用Swift 4。 Xcode给了我一些提示,这是正常的。

override func dismiss(animated flag: Bool, completion: (() -> Void)?) {
    if self.presentedViewController != nil {
        super.dismiss(animated: flag, completion: completion)
    }
}

我把它放在我的视图控制器代码的最后,就在我的主类的}之前。

请勿忘记在info.plist中为相机和照片添加添加权限,并在您查看控制器的课程开头使用此代码。

class ViewControllerLogado: UIViewController, UIImagePickerControllerDelegate,UINavigationControllerDelegate {}

答案 3 :(得分:0)

适用于Swift 3.0

override func dismissViewControllerAnimated(flag: Bool, completion: (() -> Void)?) 
{
    if self.presentedViewController != nil 
    {
       super.dismiss(animated: flag, completion: completion)
    }
}

答案 4 :(得分:0)

我添加了这个

info.plist文件的NSPhotoLibraryUsageDescription

以及如何使用该文件的说明。它适用于Xcode 8.2.1