iOS WKWebView简单实现


复制代码
#import “ViewController.h”
#import <WebKit/WebKit.h>

@interface ViewController ()
@property (nonatomic,strong)WKWebView *webView;
@end

@implementation ViewController

(void)viewDidLoad {
[super viewDidLoad];

_webView = [[WKWebView alloc] initWithFrame:self.view.bounds];//初始化
[self.view addSubview:_webView];

//1.网络
_webView.allowsBackForwardNavigationGestures = YES;
 NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com/"]]; 

[_webView loadRequest:request];  


//2.本地html 
 //获取bundlePath 路径
    NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
    
    //获取本地html目录 basePath
    NSString *basePath = [NSString stringWithFormat: @"%@/dist", bundlePath];
    
    //获取本地html目录 baseUrl
    NSURL *baseUrl = [NSURL fileURLWithPath: basePath isDirectory: YES];
    
    NSLog(@"%@", baseUrl);
    //html 路径
    
    NSString *indexPath = [NSString stringWithFormat: @"%@/index.html", basePath];
    //html 文件中内容
    NSString *indexContent = [NSString stringWithContentsOfFile: indexPath encoding: NSUTF8StringEncoding error:nil];
    //显示内容
    [webview loadHTMLString: indexContent baseURL: baseUrl];

}
复制代码

 

WKWebView实现背景透明

复制代码
@interface 你的页面 ()<WKNavigationDelegate>

 //遵循协议

    _webView.navigationDelegate = self;

//关键一句
self.webView.opaque = NO;
//或者
[self.webView setOpaque:NO];

// 似乎这两句没什么用
    self.webView.backgroundColor = [UIColor clearColor];
    self.webView.scrollView.backgroundColor = UIColor.clearColor;
复制代码

亲测有效

如果你要实现网页文件显示,比如说这个 

https://img.book118.com/sr1/M00/06/0A/wKh2AlywRP2IFAFzAAEuKOCu_hcAAcEngFumBwAAS5A024.png

那么可以这样实现

复制代码
#import <UIKit/UIKit.h>  
#import <WebKit/WebKit.h>  
  
@interface ViewController : UIViewController  
  
@property (nonatomic, strong) WKWebView *webView;  
  
@end  
  
@implementation ViewController  
  
- (void)viewDidLoad {  
    [super viewDidLoad];  
  
    // 初始化WKWebView并添加到视图  
    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];  
    self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:configuration];  
    [self.view addSubview:self.webView];  
  
    // 创建包含<img>标签的HTML字符串  
    NSString *imageUrl = @"https://img.book118.com/sr1/M00/06/0A/wKh2AlywRP2IFAFzAAEuKOCu_hcAAcEngFumBwAAS5A024.png";  
    NSString *htmlString = [NSString stringWithFormat:@"<html><body><img src='%@' style='width:100%%;height:auto;'/></body></html>", imageUrl];  
  
    // 加载HTML字符串到WKWebView  
    [self.webView loadHTMLString:htmlString baseURL:nil];  
}  
  
@end
复制代码

 

posted on   高彰  阅读(2082)  评论(0编辑  收藏  举报

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
历史上的今天:
2018-06-05 如何成就一家伟大的公司
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示