Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

alectian/loadLocalWebResource

Open more actions menu

Repository files navigation

loadLocalWebResource

iOS 通过webview加载本地web资源

实现思路: 在iOS端启动一个本地HTTP服务器,将Web资源路径设置为服务器的根目录,使用webview访问本地服务器即可。

###具体步骤: 需要的资源: 1.iOS端通过cocoapods集成CVCocoaHTTPServeriOS。 2.将静态打包后的资源文件拖入项iOS目中,添加时选择Create floder references方式

添加后的样式

以上,所有资源已备齐,接下来是关键代码。

1.启动服务器

#import <CVCocoaHTTPServeriOS/CVCocoaHTTPServeriOS-umbrella.h>

- (void)startServer{
    HTTPServer *server = [[HTTPServer alloc] init];
    [server setType:@"_http.tcp"];
    NSString * localPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"webpack"];
    [server setDocumentRoot:localPath];
    NSError *error;
    NSString *portStr;
    if ([server start:&error]) {
        UInt16 port = [self.localHttpServer listeningPort];
        portStr = [NSNumber numberWithInt:port].stringValue;
        NSLog(@"服务器启动成功-->端口号:%@",portStr);
    } else {
        NSLog(@"服务器启动出错-->%@",error);
    }
}

2.webview加载本地web资源

- (void)setWebview{
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config];
    self.webView.UIDelegate = self;
    self.webView.navigationDelegate = self;
    [self.view addSubview:self.webView];
}

- (void)loadLocalHttpServer{
    NSString *str = [NSString stringWithFormat:@"http://localhost:%@", port];
    NSURL *url = [NSURL URLWithString:str];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
     NSLog(@"加载完成");
}

About

iOS 通过webview加载本地web资源

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Morty Proxy This is a proxified and sanitized view of the page, visit original site.