【发布时间】:2012-02-15 18:17:17
【问题描述】:
编辑
自从我发布问题后,我发现了另一个有效的 Objc 示例(http://mattgemmell.com/2008/02/24/skinnable-cocoa-ui-with-webkit-and-css + 来源:http://mattgemmell.com/files/source/skinnableapp.zip)
这就是我现在拥有的:
webview_obj_from_jsAppDelegate.py
from Foundation import *
from AppKit import *
import objc
class webview_obj_from_jsAppDelegate(NSObject):
interface = objc.IBOutlet()
def applicationDidFinishLaunching_(self, sender):
NSLog("Application did finish launching.")
def awakeFromNib(self):
self.interface.setFrameLoadDelegate_(self)
path = NSBundle.mainBundle().resourcePath() + '/interface/index.html'
self.interface.setMainFrameURL_(path)
self.interface.windowScriptObject().setValue_forKey_(self, 'AppController')
#this is what I suspect to be the problem
def isSelectorExcludedFromWebScript_(self, aSel):
return NO
def showMessage(self, message):
NSLog(message)
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>title</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta name="description" content="description">
<meta name="keywords" content="">
</head>
<body>
<input id="message_button" type="button" value="Show Message" onClick="window.AppController.showMessage_('I clicked a button and I liked it');" />
</body>
</html>
现在,如果我像这样向 isSelectorExcludedFromWebScript_ 添加一个选择器:
def isSelectorExcludedFromWebScript_(self, aSel):
if aSel is objc.selector(self.showMessage, signature = 'v@:'):
return NO
单击 HTML 按钮会出现此错误:
WebKit discarding exception: <OC_PythonException> <type 'exceptions.ValueError'>: isSelectorExcludedFromWebScript:: returned None, expecting a value
结束编辑
我试图从 javascript 调用 PyObjc 函数,但我无法让它工作。我找到了一个用 Objc 编写的工作示例(https://github.com/ryanb/cocoa-web-app-example),但没有运气翻译它。
几天前我偶然发现了这个(http://stackoverflow.com/questions/2288582/embedded-webkit-script-callbacks-how),它更复杂。不用说,我无法让它工作。
所以如果有人有一个简单的例子(pyobjc & js),如果你能告诉我,我会非常感激。
【问题讨论】:
标签: javascript objective-c webview pyobjc