美文网首页
WebView页

WebView页

作者: ljzza | 来源:发表于2018-03-28 13:51 被阅读0次

import "DecorationCarQuestionViewController.h"

import <WebKit/WebKit.h>

@interface DecorationCarQuestionViewController ()<WKNavigationDelegate,WKUIDelegate>
@property(nonatomic,strong)WKWebView *webView;
@property(nonatomic,strong)WKWebViewConfiguration *wkWebConfig;

@end

@implementation DecorationCarQuestionViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.naviTitle = @"Q&A";

    [self loadData];

}

-(void)loadData
{
[YGNetService YGPOST:@"SearchQA" parameters:@{} showLoadingView:NO scrollView:nil success:^(id responseObject) {

    NSLog(@"%@",responseObject);
    NSArray *array = [responseObject valueForKey:@"flist"];
    NSString *htmlString;
    if(array.count)
    {
        htmlString = [array[0] valueForKey:@"qaNum"];
    }
    else
    {
        htmlString = @"";
    }
    
    //以下代码适配大小
    NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
    
    WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
    WKUserContentController *wkUController = [[WKUserContentController alloc] init];
    [wkUController addUserScript:wkUScript];
    
    self.wkWebConfig = [[WKWebViewConfiguration alloc] init];
    self.wkWebConfig.userContentController = wkUController;
    
    
    
    _webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, YGScreenWidth, YGScreenHeight) configuration:self.wkWebConfig];
    
    _webView.UIDelegate = self;
    _webView.navigationDelegate = self;
    _webView.scrollView.alwaysBounceVertical = YES;
    [self.view addSubview:_webView];
    _webView.backgroundColor = [UIColor whiteColor];
    _webView.opaque = NO;

// [_webView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"webbg.png"]]];
_webView.scrollView.showsHorizontalScrollIndicator = NO;

    [_webView loadHTMLString: [NSString stringWithFormat:
                               @"<html> \n"
                               "<head> <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no\">\n"
                               "<style type=\"text/css\"> \n"
                               "max-width:100%%"
                               "</style> \n"
                               "</head> \n"
                               "<body>%@</body> \n"
                               "</html>",htmlString
                               ] baseURL:nil];
 
} failure:^(NSError *error) {
    
}];

}

// 页面开始加载时调用

  • (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
    {
    // [self loadData];
    }
    // 当内容开始返回时调用
  • (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation
    {

}
// 页面加载完成之后调用

  • (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
    // 禁止放大缩小
    NSString *injectionJSString = @"var script = document.createElement('meta');"
    "script.name = 'viewport';"
    "script.content="width=device-width, initial-scale=1.0,maximum-scale=1.0, minimum-scale=1.0, user-scalable=no";"
    "document.getElementsByTagName('head')[0].appendChild(script);";
    [webView evaluateJavaScript:injectionJSString completionHandler:nil];
    }
    // 页面加载失败时调用
  • (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation
    {

}

相关文章

网友评论

      本文标题:WebView页

      本文链接:https://www.haomeiwen.com/subject/xrcofftx.html