• <bdo id='x2TQS'></bdo><ul id='x2TQS'></ul>
    <tfoot id='x2TQS'></tfoot>
      <i id='x2TQS'><tr id='x2TQS'><dt id='x2TQS'><q id='x2TQS'><span id='x2TQS'><b id='x2TQS'><form id='x2TQS'><ins id='x2TQS'></ins><ul id='x2TQS'></ul><sub id='x2TQS'></sub></form><legend id='x2TQS'></legend><bdo id='x2TQS'><pre id='x2TQS'><center id='x2TQS'></center></pre></bdo></b><th id='x2TQS'></th></span></q></dt></tr></i><div id='x2TQS'><tfoot id='x2TQS'></tfoot><dl id='x2TQS'><fieldset id='x2TQS'></fieldset></dl></div>
    1. <legend id='x2TQS'><style id='x2TQS'><dir id='x2TQS'><q id='x2TQS'></q></dir></style></legend>

      <small id='x2TQS'></small><noframes id='x2TQS'>

        UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效?

        时间:2023-10-22
        • <bdo id='5ud3D'></bdo><ul id='5ud3D'></ul>

          <small id='5ud3D'></small><noframes id='5ud3D'>

          <legend id='5ud3D'><style id='5ud3D'><dir id='5ud3D'><q id='5ud3D'></q></dir></style></legend>
          • <i id='5ud3D'><tr id='5ud3D'><dt id='5ud3D'><q id='5ud3D'><span id='5ud3D'><b id='5ud3D'><form id='5ud3D'><ins id='5ud3D'></ins><ul id='5ud3D'></ul><sub id='5ud3D'></sub></form><legend id='5ud3D'></legend><bdo id='5ud3D'><pre id='5ud3D'><center id='5ud3D'></center></pre></bdo></b><th id='5ud3D'></th></span></q></dt></tr></i><div id='5ud3D'><tfoot id='5ud3D'></tfoot><dl id='5ud3D'><fieldset id='5ud3D'></fieldset></dl></div>

                    <tbody id='5ud3D'></tbody>
                1. <tfoot id='5ud3D'></tfoot>

                  本文介绍了UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的 iOS 7+ 应用程序中有一个模块,它是 UIWebView.html 页面加载创建自定义形状按钮的 javascript(使用 Raphaeljs 库).使用 UIWebView,我将委托设置为 self.每次按下我的自定义按钮之一时,都会调用委托方法 webView: shouldStartLoadWithRequest: navigationType: .请求不应由 html 处理,而应由 iOS 代码处理.所以我使用了一个请求约定(在stackoverflow上的某个地方阅读),使用inapp"作为我的请求方案.然后我检查主机并采取适当的措施.

                  I have a module inside my iOS 7+ app which is a UIWebView. The html page loads a javascript that creates custom-shaped buttons (using the Raphaeljs library). With UIWebView, I set delegate to self. The delegate method webView: shouldStartLoadWithRequest: navigationType: is called each time one of my custom button is pressed. The requests should not be handled by the html, but rather by the iOS code. So I used a request convention (read somewhere here on stackoverflow) using "inapp" as the scheme of my requests. I then check for the host and take the appropriate action.

                  此代码在 iOS 7 上运行良好.但在 iOS 8 上 Web 视图显示为空白(错误?),因此我决定在 iOS 8 设备上使用 WKWebView.Web 视图现在渲染得很好(而且速度惊人地快!),但我的按钮没有效果.

                  This code works fine on iOS 7. But the web views appear blank on iOS 8 (bug?), so I decided to use WKWebView for iOS 8 devices. The web views now render fine (and amazingly faster!), but my buttons have no effect.

                  我尝试使用 - (WKNaviation *)loadRequest:(NSURLRequest *)request,但它没有被调用.

                  I tried using - (WKNaviation *)loadRequest:(NSURLRequest *)request, but it's not called.

                  我找不到 UIWebView 委托方法 webView: shouldStartLoadWithRequest: navigationType: 的直接等效项.使用 WKWebView 处理这些请求的最佳方式是什么?

                  I can't find a direct equivalent of the UIWebView delegate method webView: shouldStartLoadWithRequest: navigationType:. What's the best way of handling those requests with WKWebView?

                  推荐答案

                  重新阅读您的描述,您实际上需要了解的是如何使用 WKWebView 重新实现 Javascript/Objective-C 桥.

                  Re-reading your description it looks like what you actually need to know about is how to reimplement a Javascript/Objective-C bridge using WKWebView.

                  我自己刚刚按照 http://tetontech.wordpress.com/2014/07/17/objective-c-wkwebview-to-javascript-and-back/ 和 http://nshipster.com/wkwebkit/

                  I've just done this myself, following the tutorial at http://tetontech.wordpress.com/2014/07/17/objective-c-wkwebview-to-javascript-and-back/ and the info at http://nshipster.com/wkwebkit/

                  WKWebView 有一个内置的 Javascript 和 Objective-C/Swift 之间的通信方式:WKScriptMessageHandler.

                  WKWebView has a built-in way of communicating between Javascript and Objective-C/Swift: WKScriptMessageHandler.

                  首先,在视图控制器的标头中包含 WebKit 标头和 WKScriptMessageHandler 协议:

                  First, include the WebKit headers and WKScriptMessageHandler protocol in your view controller's header:

                  #import <UIKit/UIKit.h>
                  #import <WebKit/WebKit.h>
                  @interface ViewController : UIViewController <WKScriptMessageHandler>
                  
                  @end
                  

                  当初始化你的WKWebView时,你需要用一个脚本消息处理程序来配置它.随意命名,但对我来说,为您的应用命名似乎是有意义的.

                  The when initialising your WKWebView, you need to configure it with a script message handler. Name it whatever you want, but to me it seems like naming it for your app makes sense.

                      WKWebViewConfiguration *theConfiguration = 
                            [[WKWebViewConfiguration alloc] init];
                      [theConfiguration.userContentController 
                            addScriptMessageHandler:self name:@"myApp"];
                  
                      _theWebView = [[WKWebView alloc] initWithFrame:self.view.frame 
                                        configuration:theConfiguration];
                      [_theWebView loadRequest:request];
                      [self.view addSubview:_theWebView];
                  

                  现在,实现 userContentController:didReceiveScriptMessage:.当您的 webview 收到一条消息时,它会触发,因此它会完成您之前使用 webView:shouldStartLoadWithRequest:navigationType: 所做的工作.

                  Now, implement userContentController:didReceiveScriptMessage:. This fires when your webview receives a message, so it does the work you were previously doing with webView:shouldStartLoadWithRequest:navigationType:.

                  - (void)userContentController:(WKUserContentController *)userContentController 
                                          didReceiveScriptMessage:(WKScriptMessage *)message {
                      NSDictionary *sentData = (NSDictionary *)message.body;
                      NSString *messageString = sentData[@"message"];
                      NSLog(@"Message received: %@", messageString);
                  }
                  

                  您现在可以接收来自 Javascript 的消息了.您需要添加到 Javascript 中的函数调用如下:

                  You're now ready to receive messages from Javascript. The function call you need to add to your Javascript is this:

                  window.webkit.messageHandlers.myApp.postMessage({"message":"Hello there"});
                  

                  这篇关于UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 IOS 10 中使用 IFRAME 将数据从 javascript 传递到 Objective-c 时,在 shou 下一篇:如何获取在 UIWebView 中显示的 HTML 页面的标题?

                  相关文章

                2. <i id='b0zeW'><tr id='b0zeW'><dt id='b0zeW'><q id='b0zeW'><span id='b0zeW'><b id='b0zeW'><form id='b0zeW'><ins id='b0zeW'></ins><ul id='b0zeW'></ul><sub id='b0zeW'></sub></form><legend id='b0zeW'></legend><bdo id='b0zeW'><pre id='b0zeW'><center id='b0zeW'></center></pre></bdo></b><th id='b0zeW'></th></span></q></dt></tr></i><div id='b0zeW'><tfoot id='b0zeW'></tfoot><dl id='b0zeW'><fieldset id='b0zeW'></fieldset></dl></div>

                3. <legend id='b0zeW'><style id='b0zeW'><dir id='b0zeW'><q id='b0zeW'></q></dir></style></legend>

                      <bdo id='b0zeW'></bdo><ul id='b0zeW'></ul>

                    <small id='b0zeW'></small><noframes id='b0zeW'>

                    <tfoot id='b0zeW'></tfoot>