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];

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM