Ios 使用WKWebView创建自定义步骤
我对iOS开发比较陌生,目前正在使用Swift开发原型ResearchKit应用程序。其中一项要求是将Ios 使用WKWebView创建自定义步骤,ios,swift,subclass,wkwebview,researchkit,Ios,Swift,Subclass,Wkwebview,Researchkit,我对iOS开发比较陌生,目前正在使用Swift开发原型ResearchKit应用程序。其中一项要求是将WKWebView嵌入到ORKTask中,该任务包括三个步骤:ORKQuestionStep、orkwebview步骤、ORKCompletionStep。我似乎找不到多少关于如何使用Swift创建自定义步骤的子类ORKStep和ORKStepViewController的信息。有人能告诉我如何使用Swift子类化ORKStep和ORKStepViewController来显示WKWebView
WKWebView
嵌入到ORKTask
中,该任务包括三个步骤:ORKQuestionStep
、orkwebview步骤
、ORKCompletionStep
。我似乎找不到多少关于如何使用Swift创建自定义步骤的子类ORKStep
和ORKStepViewController
的信息。有人能告诉我如何使用Swift子类化ORKStep
和ORKStepViewController
来显示WKWebView
的正确方向吗
提前谢谢你 在ResearchKit用户邮件列表中,袁朱的回答:
我想你可以:
ORKActiveStep
,并正确配置它-(void)taskViewController:(ORKTaskViewController*)taskViewController stepViewController illappear:(ORKStepViewController*)stepViewController
委托方法customView
属性将webView
插入ORKActiveStepViewController
我构建了自己的ORKWebView,如下所示。我很高兴听到关于如何改进的评论 1。子类化ORKActiveStep
class Javascript_Step : ORKActiveStep {
static func stepViewControllerClass ( ) -> Javascript_Step_View_Controller.Type {
return Javascript_Step_View_Controller.self
}
}
2。子类ORKActiveStepViewController
在这里,您将修改您的WebView及其组件。
为了退出WebView并传递结果,我使用Javascript
import Foundation
import ResearchKit
import WebKit
class Javascript_Step_View_Controller : ORKActiveStepViewController, WKScriptMessageHandler {
weak var webView: WKWebView!
var html : String = "<!DOCTYPE html><html><head> <meta charset=\"UTF-8\"> <title>JS Widget</title> <style type=\"text/css\"> h1 { color: red } </style></head><body> <h1>Test</h1> <input type=\"button\" value=\"Say hello\" onClick=\"Finish_This_Widget('Result is String')\" /> <script type=\"text/javascript\"> function Finish_This_Widget(string) { App.Finish_Widget(string) } </script></body></html>"
// Here the result message posted by Javascript can be handled
func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) {
if let result = message.body as? String {
print("userContentController: \(result)")
// With the incoming reult of your webview, the ORKActiveStep timer will be started
start()
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Here you set the function name to call from javascript
let controller = WKUserContentController()
controller.addScriptMessageHandler(self, name: "Finish_Widget")
let config = WKWebViewConfiguration()
config.userContentController = controller
let frame = CGRectMake(20, 20, 200, 200)
let webView = WKWebView(frame: frame, configuration: config)
self.customView = webView
// Set the view constraints (warning message with following unproper settings)
self.customView?.superview!.translatesAutoresizingMaskIntoConstraints = false
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[demoView]-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["demoView": webView])) view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[demoView]-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["demoView": webView]))
// Load the html string
webView.loadHTMLString(html, baseURL: NSBundle.mainBundle().bundleURL)
webView.translatesAutoresizingMaskIntoConstraints = false
self.webView = webView
}
}
开放性问题
如何注入html?如果我不希望静态WebView始终具有相同的html,那么在哪一点上可以从外部设置html字符串?一个间接解决方案是通过NSUserDefaults加载html字符串。如果你知道一个更好的解决方案,我会很高兴听到!
// This functions gives you the Step you can work with finanlly
func Javascript_Widget_Step ( identifier : String, title : String, text : String ) -> ORKActiveStep {
let active_Step = Javascript_Step(identifier: identifier)
// Set title and text for the step (which is optional)
active_Step.title = title
active_Step.text = text
// set stepduration to a minimum -> after javascript function call, step will be finished
active_Step.stepDuration = 0.001
// if this is false, it will not switch to the next step after the javascript call automatically
active_Step.shouldContinueOnFinish = true
return active_Step
}