iOS web与js的简单交互
我们在封装网页的时候经常会遇到需要往网页里面的控件添加数据,但是怎么添加又成了难点。本人最近在开发的时候就遇到这样的事,解决之后,来和大家分享一下。
//以必应网站为例
[web loadRequest:
[NSURLRequestrequestWithURL:
[NSURL URLWithString:@"http://www.bing.com/?FORM=Z9FD1"]]];
以下就是主要代码,只有几句
//获取当前页面的title
NSString *title =
[webView
stringByEvaluatingJavaScriptFromString:
@"document.title"];
NSLog(@"title====%@",title);
//获取当前URL
NSString *URL =
[webView
stringByEvaluatingJavaScriptFromString: @"document.location.href"];
NSLog(@"URL===%@",URL);
//得到网页代码
NSString *html =
[webView
stringByEvaluatingJavaScriptFromString:
@"document.documentElement.innerHTML" ];
NSLog(@"html====%@",html);
//拼接字符串 根据网页name找到控价并赋值
NSString *str = @"随_的简书";
NSString *JSStr =
[NSString stringWithFormat:
@"document.getElementsByName('q')[0].value = ('%@');",str];
[webView stringByEvaluatingJavaScriptFromString: JSStr];
以下是全部代码,commit+c commit+v可以直接运行
@interface ViewController ()
@property (weak, nonatomic) UIWebView *web;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIWebView *web =
[[UIWebView alloc] initWithFrame:
[UIScreen mainScreen].bounds];
UIButton *back =
[[UIButton alloc] initWithFrame:CGRectMake
(0, 0, self.view.frame.size.width,self.view.frame.size.width / 7.5)];
[back setBackgroundColor: [UIColor orangeColor]];
[back setTitle:@"back" forState:UIControlStateNormal];
[back addTarget:self
action: @selector(back)
forControlEvents:UIControlEventTouchUpInside];
//以必应为例
[web loadRequest:
[NSURLRequest requestWithURL:
[NSURL URLWithString:
@"http://www.bing.com/?FORM=Z9FD1"]]];
web.delegate = self;
web.scalesPageToFit =YES;
web.scrollView.delegate = self;
self.web = web;
[self.view addSubview:web];
[self.view addSubview:back];
}
#pragma mark ---Delegate
-(void) webViewDidStartLoad:
(UIWebView *)webView{
NSLog(@"开始加载---") ;
}
- (void) webViewDidFinishLoad:
(UIWebView *)webView {
NSLog(@"加载完成---");
//获取当前页面的title
NSString *title =
[webView
stringByEvaluatingJavaScriptFromString:
@"document.title"];
NSLog(@"title====%@",title);
//获取当前URL
NSString *URL =
[webView
stringByEvaluatingJavaScriptFromString:
@"document.location.href"];
NSLog(@"URL===%@",URL);
//得到网页代码
NSString *html =
[webView
stringByEvaluatingJavaScriptFromString:
@"document.documentElement.innerHTML" ];
NSLog(@"html====%@",html);
//拼接字符串 根据网页name找到控价并赋值
NSString *str = @"随_的简书";
NSString *JSStr =
[NSString stringWithFormat:
@"document.getElementsByName('q')[0].value = ('%@');",str];
[webView stringByEvaluatingJavaScriptFromString:JSStr];
}
- (void) webView:
(UIWebView *)webView
didFailLoadWithError:(NSError *)error {
NSLog(@"加载失败===%@",error);
}
//当网页位置为顶部 不允许继续下拉
- (void) scrollViewDidScroll:
(UIScrollView *)scrollView {
if (self.web.frame.origin.y == 0) {
self.web.scrollView.bounces = NO;
return;
}
}
//webView的每次页面跳转都会执行,在这里可以得到想要的数据
- (BOOL)webView:
(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
NSLog(@"页面跳转");
return YES;
}
//返回
- (void) back {
[self.web goBack];
}
相关文章
- JS 逆向 --- 过无限debugge、hook、js混淆还原、控制流混淆
- List多个字段标识过滤 IIS发布.net core mvc web站点 ASP.NET Core 实战:构建带有版本控制的 API 接口 ASP.NET Core 实战:使用 ASP.NET Core Web API 和 Vue.js 搭建前后端分离项目 Using AutoFac
- JS监听不到被操作后dom的事件,js动态生成的DOM绑定事件失效,解决方案
- iOS中JS 与OC的交互(JavaScriptCore.framework)
- iOS 线程操作库 PromiseKit
- iOS 设置app语言中文,比如 copy中文,拍照按钮cancel 中文
- 【HarmonyOS】【JS】鸿蒙Js camera怎么拍照并使用image显示出来
- iOS~判断应用是否有定位权限
- Hello_IOS ios开发transform属性
- Swift Playgrounds 4 开发教程之 01 这个版本适合iOS 和 iPadOS 应用程序的IDE么
- iOS 开发中使用 NSURLProtocol 拦截 HTTP 请求
- 一行代码实现iOS序列化与反序列化(runtime)
- IOS 基于APNS消息推送原理与实现(JAVA后台)
- iOS开发UI篇—ios应用数据存储方式(归档)
- 《iOS应用软件设计之道》—— 1.10 减少问题
- 推荐4款高星星JS库:canvas库-Fabric.js、JavaScript客户端文件上传库-FilePond、客户端保存文件解决方案-FileSaver、JavaScript在线解压 ZIP 文件-JSZip
- iOS - 使用WKWebView时OC调JS的user-select属性控制用户操作
- js 判断当前操作系统是ios还是android还是电脑端
- JQuery/JS插件 json2.js
- iOS The nullability qualifiers (为空性修饰符)
- iOS storyboard创建Navigation Controller