iOS-富文本实现 和wkweb网页实现

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"注册即同意《金购会员服务协议》《金购隐私政策》"];     //可变属性的字符串

[attributedString addAttribute:NSLinkAttributeName
 
                         value:@"huiyuanxieyi://"
 
                         range:[[attributedString string] rangeOfString:@"《金购会员服务协议》"]];     //给可变字符串配置富文本属性

[attributedString addAttribute:NSLinkAttributeName
 
                         value:@"yinsixieyi://"
 
                         range:[[attributedString string] rangeOfString:@"《金购隐私政策》"]];     //同上
//textView的代理方法

-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{
    
    if ([[URL scheme] isEqualToString:@"huiyuanxieyi"]){
        
        WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];     //WKWebViewConfiguration是一个具有集合属性的对象,用来初始化web view@帮忙包含用于配置@link WKWebView@/链接的属性
        
        WKUserContentController *userCC = config.userContentController;     //WKUserContentController为对象JavaScript提供一种消息方式到web view。与web view相关联的用户控制器由它指定web配置视图
        
        /*
         
         *此处添加了一个跳转到web页面实现事件
         
         */
        
        //JavaScript调用oc  添加处理脚本
        
        [userCC addScriptMessageHandler:self name:@"ClickFinish"];
        
        WKWebView *webView =[ [WKWebView alloc] initWithFrame:self.view.frame configuration:config];
        
        webView.scrollView.scrollEnabled = YES;
        
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.jingomall.cn/xxxxxxxxxxxx.html"]];
        
        [self.view addSubview:webView];
        
        [self.webView loadRequest:request];
        
        self.webView.navigationDelegate= self;
        
        return NO;
        
    }
    
    return YES;
    
}

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
    
    if ([message.name isEqualToString:@"ClickFinish"])
        
    {
        
        [self.webView removeFromSuperview];
        
    }
    
}

//web返回控制器

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
    
    //    [self.view addSubview:webView];
    
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    //    self.VIPButton.userInteractionEnabled = YES;
    
    //    self.privacyButton.userInteractionEnabled =YES;
    
}

你可能感兴趣的:(iOS-富文本实现 和wkweb网页实现)