活动模块UIWebView

今天还有接下来的几天要跟webView打交道了:
下边是我找的一些跟WebvView相关的资料:
http://www.jianshu.com/p/3d7e4804033b
http://bxbxbai.gitcafe.io/2015/08/16/talk-about-bybird-app/
http://www.jianshu.com/p/ca496cb680fe

首先,让我们了解一下UIWebView。

加载本地html页面:

NSString *webPath = [[NSBundle mainBundle]pathForResource:@“HelloWord"ofType:@"html”];//获取文件路径
NSURL *webURL = [NSURL fileURLWithPath:webPath];//通过文件路径字符串设置URL
NSURLRequest *URLRequest = [NSURL RequestrequestWithURL:webURL];//设置请求提交的相关URL
[self.webViewloadRequest:URLRequest];//提交请求
1、请求网络获取html

我们的项目需要带参数的网络请求,返回一个html页面

//********************** AF begin *************************
    //增加这几行代码;
    AFSecurityPolicy *securityPolicy = [[AFSecurityPolicy alloc] init];
    [securityPolicy setAllowInvalidCertificates:YES];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //这里进行设置;
    [manager setSecurityPolicy:securityPolicy];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    [manager POST:ACTIVITYLIST

       parameters:params

          success:^(AFHTTPRequestOperation *operation,id responseObject){

              NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
              NSLog(@"成功: %@", string);
          }
          failure:^(AFHTTPRequestOperation *operation,NSError *error){
              NSLog(@"失败: %@", error);
          }];
    //********************** AF over ********************
2、拿到str之后展示在webView上
[self.activityWebView loadHTMLString:string baseURL:nil];
3、拦截URL处理

在代理方法-(BOOL)webView:(UIWebView
)webView shouldStartLoadWithRequest:(NSURLRequest )request navigationType:(UIWebViewNavigationType)navigationType中拦截处理跳转URL

4、js调用Object-c函数
 //js交互
    JSContext *context = [self.webView  valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];

//未登录或者token失效,请求登录,toLoginByAPP是约定好的函数名称
    context[@"toLoginByAPP"] = ^() {
        NSLog(@"+++++++Begin toLoginByAPP+++++++");
        NSArray *args = [JSContext currentArguments];
        for (JSValue *jsVal in args) {
            NSLog(@"toLoginByAPP:%@", jsVal);
        }
        JSValue *this = [JSContext currentThis];
        NSLog(@"toLoginByAPP——this: %@",this);
        NSLog(@"-------End toLoginByAPP-------");

    };
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值