带有WebKit的简单Swift Cocoa应用程序:无法上传图片 [英] Simple Swift Cocoa app with WebKit: Upload picture doesn't work
问题描述
我决定制作自己的FB聊天应用,该应用仅在
I decided to make my own FB chat app that simply shows https://messenger.com on a WebView
after trying other 'freemium' apps.
我的ViewController.swift
只有几行代码,可将URL加载到网络视图中
My ViewController.swift
has just a few lines of code that loads URL on the web view
import Cocoa
import WebKit
class ViewController: NSViewController {
@IBOutlet weak var webView: WebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "https://messenger.com")
let request = NSURLRequest(URL: url!);
webView.mainFrame.loadRequest(request);
}
override var representedObject: AnyObject? {
didSet {
// do nothing
}
}
}
除了向info.plist添加NSAppTransportSecurity
键以通过HTTPS连接取消阻止HTTP流量外,我还没有做任何其他设置.
Besides adding NSAppTransportSecurity
key to info.plist to unblock HTTP traffic via HTTPS connection, I have not done any other settings.
请先看这张图片.
一切看起来都很好&除两件事外都可以正常工作.
Everything looks fine & working except two things.
-
上传图片不起作用-我在图片中标记为1.
Uploading image does not work - I labeled as 1 in the picture.
-
通常(如在其他已发布的应用程序中或在Web浏览器中),如果单击该图标,它将显示资源管理器以上传如下图所示的图片.
normally (as in other released apps or from web browsers) if you click that icon, it shows an explorer to upload a picture like below.
我的应用程序完全忽略了用户对该图标的单击,因此我无法将任何图片上传到聊天中. 有趣的是,如果我将图片拖放到webview
,它可以上传.
My app completely ignores user's click on that icon so I cannot upload any pictures to the chat. Interestingly, if I drag and drops the picture to the webview
, it uploads fine.
共享的图片未显示-我在图片中标记为2.
Shared picture does not show up - I labeled as 2 in the picture.
-
同样,
-
通过其他浏览器或已发布的应用显示了我与参与者共享的图片,如下所示. (当然,我检查了图片)
again, from other browsers or released apps, it shows the pictures that I shared with participants like below. (of course I censored the pictures)
我的应用尝试加载图片,但不显示它们.我可以看到它正在尝试加载,因为在加载时会看到循环进度指示器.
my app tries to load the pics, but does not display them. I can see it trying to load because I see circular progress indicator while loading.
为什么?
-
我怀疑可能有一种方法可以监听
WebView
中触发的JavaScript并链接到文件资源管理器或类似的东西?
I suspect that there might be a way to listen to the JavaScript that's triggered within the
WebView
and link to a file explorer or something like that?
这我不知道.我已经登录Messenger(基本上是Facebook),所以我认为session
在这里不是问题.也许是一些jQuery加载问题?
This I have no idea. I'm logged into Messenger (basically Facebook), so I think session
is not a problem here. Maybe some jQuery loading issue??
我应该怎么做才能解决这些问题?
What should I do to solve these issues?
推荐答案
- There is indeed a delegate method to open a new panel called
runOpenPanelForFileButtonWithResultListener
, documentation here.
在委托方法中,只需像这样创建一个新的NSOpenPanel:
In the delegate method, just create a new NSOpenPanel like this:
func webView(sender: WebView!, runOpenPanelForFileButtonWithResultListener resultListener: WebOpenPanelResultListener!, allowMultipleFiles: Bool) {
let openDialog = NSOpenPanel()
if (openDialog.runModal() == NSOKButton) {
let fileName: String = (openDialog.URL?.path)!
resultListener.chooseFilename(fileName) // Use chooseFilenames for multiple files
}
}
- 我刚刚尝试从Messagers App创建一个WebView,并且图像加载良好.
您应该尝试通过界面构建器(或通过代码)启用WebView选项,例如自动加载图像"或启用动画图像".
- I just tried to create a WebView from Messagers App and images are loading well.
You should try to enable WebView options like "Autoload Images" or "Enable Animated Images" from interface builder (or by code).
这篇关于带有WebKit的简单Swift Cocoa应用程序:无法上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!