美文网首页
App内搭建HTTP服务

App内搭建HTTP服务

作者: __Gavin__ | 来源:发表于2018-06-07 18:52 被阅读0次

目标

  • App内启停HTTP服务。
  • 供其它设备访问。
  • 可以下载指定资源。

主要内容

  • CocoaHTTPServer 框架使用
  • iOS 拍照、存储、读取
  • 搭建简单的HTML界面

具体步骤

添加 CocoaHTTPServer 框架

下载 CocoaHTTPServer 框架,将 HTTP、GCDAsyncSocket、Response、Categories 的相关内容引入到项目。

初始化并启动服务

  • 创建服务首页: /web/index.html
/* index.html */
<!DOCTYPE html>
<html>
<head>
    <title>CocoaHTTPServer</title>
</head>
<body>
    <h2>Welcome to CocoaHTTPServer</h2>
</body>
</html>
  • 初始化服务及启停
- (void)initHTTPServer
{
    self.httpServer = [[HTTPServer alloc] init];
    [self.httpServer setType:@"_http._tcp."];

    [self setHttpServerDocumentRoot];
}

- (void)startupServer
{
    NSError *error;
    [self.httpServer start:&error];
    if (error)
    {
        NSLog(@"%@", error);
    }
    else
    {
        self.port = [self.httpServer listeningPort];
        NSLog(@"Server started port:%d", self.port);
    }
}

- (void)stopServer
{
    [self.httpServer stop:YES];
}

此时访问手机IP及服务监听端口组成的地址:xxx.xxx.xx.xx: port,即可看到 Welcome to CocoaHTTPServer

拍照存储

  • 相机初始化及运行
- (void)initAVCaptureSession
{
    // 创建session
    self.captureSession = [[AVCaptureSession alloc] init];
    
    // 初始化设备
    NSError *error;
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
    // 初始化输入
    self.videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:device error:&error];
    if (error)
    {
        NSLog(@"%@", error);
    }
    
    // 初始化输出
    self.stillImgOutput = [[AVCaptureStillImageOutput alloc] init];
    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG, AVVideoCodecKey, nil];
    [self.stillImgOutput setOutputSettings:outputSettings];
    
    // 配置session
    if ([self.captureSession canAddInput:self.videoInput])
    {
        [self.captureSession addInput:self.videoInput];
    }
    if ([self.captureSession canAddOutput:self.stillImgOutput])
    {
        [self.captureSession addOutput:self.stillImgOutput];
    }
    
    // previewLayer
    self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
    [self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
    self.previewLayer.frame = CGRectMake(0, 0, MAINSCREEN_WIDTH, MAINSCREEN_HEIGHT);
    [self.view.layer addSublayer:self.previewLayer];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.captureSession startRunning]; // startRunning
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
    [self.captureSession stopRunning]; // stopRunning
}
  • 拍照存储与记录
- (void)tapBtnClicked:(UIButton *)tapBtn
{
    AVCaptureConnection *stillImageConnection = [self.stillImgOutput        connectionWithMediaType:AVMediaTypeVideo];
    UIDeviceOrientation curDeviceOrientation = [[UIDevice currentDevice] orientation];
    AVCaptureVideoOrientation avcaptureOrientation = [self avOrientationForDeviceOrientation:curDeviceOrientation];
    [stillImageConnection setVideoOrientation:avcaptureOrientation];
    [stillImageConnection setVideoScaleAndCropFactor:1];
    
    __weak typeof(self) weakself = self;
    [self.stillImgOutput captureStillImageAsynchronouslyFromConnection:stillImageConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
        
        NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];

        [weakself.captureSession stopRunning];
        
        // 存储进Caches
        [weakself saveImageData:imageData];
    }];
}

- (void)saveImageData:(NSData *)imageData
{
    BOOL isDirectory = NO;
    if (![[NSFileManager defaultManager] fileExistsAtPath:ImageDirPath isDirectory:&isDirectory])
    {
        NSError *error;
        [[NSFileManager defaultManager] createDirectoryAtPath:ImageDirPath withIntermediateDirectories:YES attributes:nil error:&error];
    }
    
    NSString *imagePath = [ImageDirPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", [self getTimeString]]];
    [imageData writeToFile:imagePath atomically:YES];
    
    // 记录进 plist
    [self saveImageDetailToPlist:imagePath];
}

- (void)saveImageDetailToPlist:(NSString *)imagePath
{
    NSString *filename = [[imagePath componentsSeparatedByString:@"/"] lastObject];
    
    NSMutableArray *imgDetailAry = [NSMutableArray arrayWithContentsOfFile:ImagesPlistPath];
    if (!imgDetailAry)
    {
        NSMutableDictionary *imgDetailDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:filename, @"filename", imagePath, @"imagePath", nil];
        imgDetailAry = [NSMutableArray arrayWithObject:imgDetailDict];
        [imgDetailAry writeToFile:ImagesPlistPath atomically:YES];
    }
    else
    {
        NSMutableDictionary *imgDetailDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:filename, @"filename", imagePath, @"imagePath", nil];
        [imgDetailAry addObject:imgDetailDict];
        [imgDetailAry writeToFile:ImagesPlistPath atomically:YES];
    }
    
    ShowMBProgressHUDText(@"照片已存储")
    [self.captureSession startRunning];
}

动态搭建服务首页

  • 根据照片信息决定 index 内容
- (void)initIndexHtmlDynamicContents
{
    NSString *indexPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"web/index.html"];

    NSMutableArray *imgDetailAry = [NSMutableArray arrayWithContentsOfFile:ImagesPlistPath];
    NSString *htmlStr;
    if (!imgDetailAry)
    {
        htmlStr = @"<!DOCTYPE html><html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/><title>CocoaHTTPServer</title></head><body> <h2>暂无照片</h2> </body></html>";
    }
    else
    {
        NSString *ulString = [NSString string];
        for (int i = 0; i < imgDetailAry.count; i++)
        {
            NSDictionary *imgDict = imgDetailAry[i];
            ulString = [ulString stringByAppendingString:[NSString stringWithFormat:@"<li>%@--</li><a href=\"%@:%d/%d\">download</a>", imgDict[@"filename"], [self getIPAddress], self.port, i]];
        }
        htmlStr = [NSString stringWithFormat:@"<!DOCTYPE html><html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/><title>CocoaHTTPServer</title></head><body> <h2>The Pictures</h2><ul>%@</ul></body></html>", ulString];
    }
    
    NSError *writeToFileError;
    [htmlStr writeToFile:indexPath atomically:YES encoding:NSUTF8StringEncoding error:&writeToFileError];
}

实现下载

  • 给 index.html 添加下载按钮
// 动态更新 index.html
- (void)updateIndexHtmlDynamicContents:(NSMutableArray *)imgDetailAry
{
    NSString *htmlStr;
    NSString *ulString;
    NSString *indexPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"web/index.html"];
    for (int i = 0; i < imgDetailAry.count; i++)
    {
        NSDictionary *imgDict = imgDetailAry[i];
        ulString = [ulString stringByAppendingString:[NSString stringWithFormat:@"<li>%@--</li><a href=\"%@:%d/%d\">download</a>", imgDict[@"filename"], [self getIPAddress], self.port, i]];
    }
    htmlStr = [NSString stringWithFormat:@"<!DOCTYPE html><html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/><title>CocoaHTTPServer</title></head><body> <h2>The Pictures</h2><ul>%@</ul></body></html>", ulString];

    NSError *writeToFileError;
    [htmlStr writeToFile:indexPath atomically:YES encoding:NSUTF8StringEncoding error:&writeToFileError];
}
  • 创建 MyHTTPConnection 类
    实现如下代理方法:
/* 返回要下载的文件 */
- (NSObject<HTTPResponse> *)httpResponseForMethod:(NSString *)method URI:(NSString *)path
{
    if (path.length == 1)
    {
        return [super httpResponseForMethod:method URI:path];
    }

    NSMutableArray *imgDetailAry = [NSMutableArray arrayWithContentsOfFile:ImagesPlistPath];
    NSDictionary *imgDetailDict = imgDetailAry[path.integerValue];
    
    DITHTTPFileResponse *fileResponse = [[DITHTTPFileResponse alloc] initWithFilePath:imgDetailDict[@"imagePath"] forConnection:self];
    fileResponse.filename = imgDetailDict[@"filename"];
    
    return fileResponse;
}
  • 给 Server 配置 MyHTTPConnection
/* 初始化 server 时配置 */
[self.httpServer setConnectionClass:[MyHTTPConnection class]];
  • 创建 MyHTTPFileResponse 类
    实现如下代理方法:
/* 告诉浏览器这是下载以及下载的文件名称 */
- (NSDictionary *)httpHeaders
{
    NSMutableDictionary *headersDict = [NSMutableDictionary dictionary];
    [headersDict setValue:@"application/octet-stream" forKey:@"Content-Type"];
    [headersDict setValue:[NSString stringWithFormat:@"attachment; filename=%@", self.filename] forKey:@"Content-Disposition"];
    
    return headersDict;
}

总结

  • 其它设备向App内服务上传资源同样可以实现,缓缓再写。
  • Github: DiTing
  • 截图如下:


    IMG_0271.PNG
    IMG_0272.PNG

相关文章

  • App内搭建HTTP服务

    目标 App内启停HTTP服务。 供其它设备访问。 可以下载指定资源。 主要内容 CocoaHTTPServer ...

  • AndroidVideoMedia: Error opening

    解决办法是在App内搭建个微型的http服务器(NanoHTTPD)然后通过该本地的微型服务器访问本地视频文件来达...

  • nodejs搭建服务器

    1.使用内置http搭建服务 2.基于express模块搭建服务

  • [头参数]01 - 搭建服务器

    目录 使用node搭建http服务端 1. 使用node搭建http服务端 代码 response.end来返回数...

  • Go语言第4天 - Web服务

    1. 搭建http服务器 使用Go语言标准库“net/http”就可以很方便的提供http服务了。 2. 搭建We...

  • 利用Nginx+Subversion搭建SVN http协议服务

    大概步骤 搭建svn服务 使用Apache HTTP服务将svn协议转成http协议 nginx转发http请求到...

  • 安装json-server

    这个包可以快速帮我们搭建一个http服务,为什么搭建http服务呢?是因为我们使用axios的时候需要向服务端发送...

  • Node小知识

    一、检测是否安装成功 二、 node文件 三、自己搭建http服务 四、创建http服务

  • Node.js Web 模块

    Node.js 提供了 http 模块,http 模块主要用于搭建 HTTP 服务端和客户端,使用 HTTP 服务...

  • Linux的网络应用服务-http服务

    目录 http简介1.1 关于http协议1.2 服务器实现的思路 搭建http服务器 1. http简介 1.1...

网友评论

      本文标题:App内搭建HTTP服务

      本文链接:https://www.haomeiwen.com/subject/bzcisftx.html