ios - 如何正确实现 didFinishNavigation
问题描述
我正在尝试在 didFinishNavigation 上实现一段代码,该代码段在 webview 中调用 js 函数。但似乎我没有正确实现这一点。
#import "DummyWebView.h"
#import "DummyOptions.h"
#import <WebKit/WebKit.h>
@implementation DummyWebView
- (void)viewDidLoad {
[super viewDidLoad];
CGRect screen = [[UIScreen mainScreen] bounds];
WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(screen), CGRectGetHeight(screen))];
NSString *urlString = @"https://5e9dad3079f67.htmlsave.net";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[self.view addSubview:webView];
}
-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
NSString *json = [NSString stringWithFormat:@"%s %s %s %s %s %s", "{", "\"status\" :", "\"", "bassey", "\"","}"];
NSString *function = [NSString stringWithFormat:@"%s %@ %s", "test('", json, "')"];
[webView evaluateJavaScript:function completionHandler:^(NSString *result, NSError *error)
{
NSLog(@"Error %@",error);
NSLog(@"Result %@",result);
}];
}
- (void)viewDidAppear:(BOOL)animated{
NSLog(@"viewDidAppear loaded successfully");
}
@end
请问我做错了什么?
解决方案
我刚刚得到这个工作。回到 Objective-C 有点受创!
在你的头文件中:
#import <UIKit/UIKit.h>
#import <WebKit/WebKit.h>
@interface ViewController : UIViewController <WKNavigationDelegate>
@end
您需要在WebKit
此处导入并让您ViewController
符合WKNavigationDelegate
.
在方法文件中:
WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(screen), CGRectGetHeight(screen))];
webView.navigationDelegate = self;
使用委托集,它现在将调用该函数:
推荐阅读
- python - 熊猫。选择特定列
- python - Hadoop 得到了预期的 JSON。是否启用了 WebHDFS?得到 ''
- java - 如何在eclipse中配置构建路径条目
- google-chrome - 运行 Flutter Web 应用程序时创建服务器套接字失败
- ansible - Ansible Tower 我们可以将主机名作为变量传递给作业模板吗
- php - 尝试填写 PDF 得到“不支持对象流”
- java - 如何转换地图
> 进入 JsonObject - pandas - Matplotlib Line vs. Bar plot DateTime 轴格式
- c++ - 如何使用任何退出代码退出 C++ 程序?
- openedx - OpenEdX,IRONWOOD - 重定向问题