图一
图一的是我们操作完之后的 效果图。
写在最前:在阅读这篇文章的时候,你可能需要温习一下程序间的互调和传值,可以参考一下我的这篇文章。然后,我们可以根据传值,来跳转到不同的页面。在上篇文章中,给出了使用的演示,所以在这篇文章中,这个操作就不再给出。
图二为 文档目录结构。
图二
用到的三方库:pod 'CocoaHTTPServer'
步骤:
先配置好要分享的网页内容content.html,然后对content.html的内容做URI之后的字符串,复制到index.html的指定位置就可以了,下面给出了示例代码。
// 触发事件
- (void)shareLocalClick {
// 配置 DDLog,只打印到控制台
[DDLog addLogger:[DDTTYLogger sharedInstance]];
// 初始化 server
httpServer = [[HTTPServer alloc] init];
// Tell the server to broadcast its presence via Bonjour.
// This allows browsers such as Safari to automatically discover our service.
[httpServer setType:@"_http._tcp."];
// Bonjour这样的技术允许客户端在运行时动态地发现服务器的端口。但为了方便测试,通常指定一个端口
[httpServer setPort:12345];
// 从嵌套的Web文件夹里读取
NSString *webPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Web"];
[httpServer setDocumentRoot:webPath];
[self startServer];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://127.0.0.1:12345"]];
}
- (void)startServer {
NSError *error;
if([httpServer start:&error]) {
DDLogInfo(@"Started HTTP Server on port %hu", [httpServer listeningPort]);
} else{
DDLogError(@"Error starting HTTP Server: %@", error);
}
}
content.html
粤省事if (window.navigator.standalone == true) {
var lnk = document.getElementById("qbt").click();
} else {
document.getElementById("msg").innerHTML='
}
index.html