swift osx webview输入类型=文本无法正常工作

时间:2017-05-26 07:41:06

标签: swift macos

我以编程方式创建了窗口和webview。

enter image description here

点击所有按钮,单选按钮。 但是,如果我单击输入并想要输入文本,则不会单击。光标更改,但焦点不在输入中。键盘没有打字。 输入上的右键工作正常,我可以在其中粘贴文本。

//
//  WindowCommand.swift
//  DeskAlerts
//
//  Created by mihail on 26/04/2017.
//  Copyright © 2017 Toolbarstudio Inc. All rights reserved.
//

import Foundation
import Cocoa
import SINQ
import WebKit
extension NSCoder {
    class func empty() -> NSCoder {
        let data = NSMutableData()
        let archiver = NSKeyedArchiver(forWritingWith: data)
        archiver.finishEncoding()
        return NSKeyedUnarchiver(forReadingWith: data as Data)
    }
}



class WindowCommand:NSViewController, WebFrameLoadDelegate, WebUIDelegate, WebEditingDelegate, WebPolicyDelegate, WebDownloadDelegate, WebResourceLoadDelegate
{
    var Coder: NSCoder?

    var Window: NSWindow?

    init() {
        Coder = NSCoder.empty()
        super.init(coder: Coder!)!
        InitParentWindow()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func InitParentWindow()
    {
        let rect = NSRect.init(x: 0.0, y: 0.0, width: 500.0 , height: 500.0)
        Window = NSWindow(contentRect: rect , styleMask: NSWindowStyleMask.borderless, backing: NSBackingStoreType.retained, defer: false, screen: MainScreen)


        Window?.isOpaque = true
        Window?.backgroundColor = NSColor.clear
        Window?.hasShadow = false
        Window?.isReleasedWhenClosed = true
        Window?.acceptsMouseMovedEvents = true
        Window?.allowsConcurrentViewDrawing = true
        Window?.ignoresMouseEvents = false
        Window?.isDocumentEdited = true
        Window?.level = Int(CGWindowLevelForKey(.maximumWindow))
        Window?.makeKeyAndOrderFront(self)
        Window?.contentViewController = self
    }

    func Close()
    {
        Window?.close()
    }


    override func viewDidLoad() {
        super.viewDidLoad()


        let preferences = WebPreferences()
        preferences.allowsAirPlayForMediaPlayback = true
        preferences.allowsAnimatedImageLooping = true
        preferences.allowsAnimatedImages = true
        preferences.arePlugInsEnabled = true
        preferences.autosaves = true
        preferences.cacheModel = WebCacheModel.documentBrowser
        preferences.isJavaEnabled = true
        preferences.isJavaScriptEnabled = true
        preferences.javaScriptCanOpenWindowsAutomatically = true
        preferences.loadsImagesAutomatically = true
        preferences.privateBrowsingEnabled = false
        preferences.shouldPrintBackgrounds = false
        preferences.tabsToLinks = false
        preferences.usesPageCache = false
        preferences.suppressesIncrementalRendering = true

        self.view.frame = NSRect.init(x: 0, y: 0, width: 500, height: 500)
        CaptionBrowser = WebView.init(frame: NSRect.init(x: 0, y: 0, width: 500, height: 500)
        let nillvalue:WebView? = nil
        if (CaptionBrowser != nillvalue)
        {
            CaptionBrowser?.uiDelegate = self
            CaptionBrowser?.frameLoadDelegate = self
            CaptionBrowser?.editingDelegate = self
            CaptionBrowser?.policyDelegate = self
            CaptionBrowser?.resourceLoadDelegate = self
            CaptionBrowser?.downloadDelegate = self
            CaptionBrowser?.windowScriptObject.setValue(HtmlInteropClass(data: Alert!, owner: self), forKey: "external")
            CaptionBrowser?.preferences = preferences
            self.view.addSubview(CaptionBrowser!, positioned: NSWindowOrderingMode.above, relativeTo: nil)


            var url = NSURL(string: (ParentWindowParams?.captionhref)!)
            CaptionBrowser?.mainFrame.load(URLRequest.init(url: (url! as URL) as URL))



            let mainbrowserwidth = 480

            let mainbrowserheight = 150






            MainBrowser = WebView.init(frame: CGRect(x:10 ,y:150, width:480 ,height:150))
            MainBrowser?.uiDelegate = self
            MainBrowser?.frameLoadDelegate = self
            MainBrowser?.editingDelegate = self
            MainBrowser?.policyDelegate = self
            MainBrowser?.resourceLoadDelegate = self
            MainBrowser?.downloadDelegate = self
            MainBrowser?.preferences = preferences
            MainBrowser?.windowScriptObject.setValue(HtmlInteropClass(data: Alert!, owner: self), forKey: "external")

            self.view.addSubview(MainBrowser!, positioned: NSWindowOrderingMode.above, relativeTo: CaptionBrowser)

            let filePath = Bundle.main.path(forResource: "test", ofType: "html", inDirectory: "Resources")
            url = NSURL(string: filePath!)
            MainBrowser?.mainFrame.load(URLRequest.init(url: url! as URL))


            //url = NSURL(string: Alerthref!)
            //MainBrowser?.mainFrame.load(URLRequest.init(url: url! as URL))
        }
    }

    func webView(_ sender: WebView!, createWebViewWith request: URLRequest!) -> WebView! {
        return sender
    }

    func webView(_ webView: WebView!, decidePolicyForNavigationAction actionInformation: [AnyHashable : Any]!, request: URLRequest!, frame: WebFrame!, decisionListener listener: WebPolicyDecisionListener!)
    {

            listener.use()


    }

    func webView(_ webView: WebView!, decidePolicyForNewWindowAction actionInformation: [AnyHashable : Any]!, request: URLRequest!, newFrameName frameName: String!, decisionListener listener: WebPolicyDecisionListener!) {

            listener.use()

    }

    func webView(_ sender: WebView!, runJavaScriptAlertPanelWithMessage message: String!, initiatedBy frame: WebFrame!) {
        print(message)
    }
    func webView(_ sender: WebView!, runJavaScriptTextInputPanelWithPrompt prompt: String!, defaultText: String!, initiatedBy frame: WebFrame!) -> String! {


        return ""
    }

    override func loadView() {
      super.loadView()
    }

    override var representedObject: Any? {
        didSet {
            // Update the view, if already loaded.
        }
    }


}

1 个答案:

答案 0 :(得分:0)

发现了一个麻烦。 Window = NSWindow(contentRect:rect,styleMask:NSWindowStyleMask.borderless,支持:NSBackingStoreType.retained,defer:false,screen:MainScreen)

如果我做财产 styleMask:NSWindowStyleMask.borderless 输入不可用。