OSX WebView very slow and laggy

Jérôme Binachon

I want to make a simple full screen agar.io OS X app (no title, just the game), using a WebView that loads the agar.io website... but it encounter sever lag problem comparing to Safari which is OK ... such as the game is unplayable.

For this, I have create an simple cocoa app without storyboards (I tried with Xcode 6.4 and 7 beta, on Yosemite on a latest MacBook Pro with the best graphic card), add a webview to the window, connected the webview outlet. Here is my code :

//  AppDelegate.swift

import Cocoa
import WebKit

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!

    @IBOutlet weak var webView: WebView!

    func applicationDidFinishLaunching(aNotification: NSNotification) {
        // Insert code here to initialize your application
        let frame = NSScreen.mainScreen()?.frame
        window.setFrame(frame!, display: true)
        let url = NSURL(string : "http://www.agar.io")
        let request = NSURLRequest(URL: url!)
        webView.mainFrame.loadRequest(request)
        window.contentView = webView

    }

    func applicationWillTerminate(aNotification: NSNotification) {
        // Insert code here to tear down your application
    }


}

Maybe I'm missing something ?

Jérôme Binachon

I finally found the solution... that is to use the new WKWebView class instead of the old WebView class. WKWebView class is (nearly?) as fast as safari.

My new code is nearly the same, except I had to manually instantiate the WKWebView as there is no WKWebView object in the UI editor, and change the loadRequest(...) which apply directly to the WKWebView object now :

import Cocoa
import WebKit

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!    

    var webView:WKWebView?

    func applicationDidFinishLaunching(aNotification: NSNotification) {

        let url = NSURL(string : "http://agar.io")
        let request = NSURLRequest(URL: url!)

        webView = WKWebView()
        webView!.loadRequest(request)

        window.contentView = webView

    }

    func applicationWillTerminate(aNotification: NSNotification) {
    // Insert code here to tear down your application
    }

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related