WebView页

2018-03-28  本文已影响0人  ljzza

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)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) {
    
}];

}

// 页面开始加载时调用

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

}

上一篇 下一篇

猜你喜欢

热点阅读