美文网首页收藏ios
iphone本地架设http服务器

iphone本地架设http服务器

作者: ashura_ | 来源:发表于2017-05-08 19:14 被阅读1039次

    简介

    目前一些应用里有用到在Iphone上架设http服务器,其主要用在以下几个方面

    1. 当需要在iphone本地架设一个小型http网站,供用户来使用。
    2. 或者希望通过自己的应用把相应的文件导入到电脑里,例如:目前大多数视频应用,把电脑上的视频导入到本应用。(必须在同一WIFI)
    3. 预加载视频。在浏览网上视频时候,在用到hls等视频切片技术的视频源时候,可以在本地架设一个http服务器预下载后边的视频,进行预加载,以达到视频播放流畅。

    在iOS上,目前推荐使用CocoaHTTPServer开源框架。
    CocoaHTTPServer官方介绍

    CocoaHTTPServer is a small, lightweight, embeddable HTTP server for Mac OS X or iOS applications.

    Sometimes developers need an embedded HTTP server in their app.
    Perhaps it's a server application with remote monitoring.
    Or perhaps it's a desktop application using HTTP for the communication backend.
    Or perhaps it's an iOS app providing over-the-air access to documents.
    Whatever your reason, CocoaHTTPServer can get the job done. It provides:

    Built in support for bonjour broadcasting
    IPv4 and IPv6 support
    Asynchronous networking using GCD and standard sockets
    Password protection support
    SSL/TLS encryption support
    Extremely FAST and memory efficient
    Extremely scalable (built entirely upon GCD)
    Heavily commented code
    Very easily extensible
    WebDAV is supported too!

    环境

    系统 开发工具
    iOS9 xCode 7.3

    用法

    以在本地创建http web为例

    在官网下载源码工程

    这里使用的是一个小forkcocoa-web-resource

    Samples.png
    1. Core文件夹 架设http服务器Core 核心类
    2. Extensions 文件夹 WebDAV实现类
    3. Sample 文件夹 CocoaHTTPServer基本使用
    4. Vendor 第三方日志类

    创建工程

    创建一个名为iPhoneLocalHttpServer的Demo工程

    截屏16_5_29_上午11_36.png

    引入库

    加载资源

    资源加载失败打不开网页404错误

    iPhoneLocalHttpServer_xcodeproj.png

    定义

    #import "HTTPServer.h"
    
    
    interface ViewController ()<UIWebViewDelegate,WebFileResourceDelegate>
    {
        HTTPServer *httpServer;
        UIWebView *mWebView;//测试webview
        UILabel *urlLabel;//显示ip端口的
        NSMutableArray *fileList;
        
    }
    

    实现

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        
        urlLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, 25)];
        [self.view addSubview:urlLabel];
        
        // set up the http server
        httpServer = [[HTTPServer alloc] init];
        [httpServer setType:@"_http._tcp."];
        [httpServer setPort:8088];
        [httpServer setName:@"iPhoneLocalHttpServer"];
        [httpServer setupBuiltInDocroot];
        httpServer.fileResourceDelegate = self;
        
        NSError *error = nil;
        BOOL serverIsRunning = [httpServer start:&error];
        if(!serverIsRunning)
        {
            NSLog(@"Error starting HTTP Server: %@", error);
        }
        [urlLabel setText:[NSString stringWithFormat:@"http://%@:%d", [httpServer hostName], [httpServer port]]];
        
        
        mWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, urlLabel.frame.size.width, [UIScreen mainScreen].bounds.size.height - 64)];
        mWebView.delegate = self;
        mWebView.scalesPageToFit = YES;
        [self.view addSubview:mWebView];
        
    }
    
    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@:%d", @"127.0.0.1", [httpServer port]]]];
            [mWebView loadRequest:request];
        });
    }
    

    模拟器运行:

    截屏16_5_29_下午3_20.png

    safari打开

    使用模拟器safari打开127.0.0.1:8088(真机上不行,还不知道原因)

    截屏16_5_29_下午3_21.png

    iOS http设置

    1. 在Filter中搜索Info.plist,选择Info.plist进行编辑


    2. 按照上面提到的方式添加信息,正确的修改会看到下图这个样子,注意类型NSAppTransportSecurity为Dictionary,NSAllowsArbitraryLoads为Boolean,复制粘贴的时候,不要多了空格,segment fault 页面上直接复制,经常会多一个出空格!


    Demo

    git 地址

    两个GCDWebServer

    相关文章

      网友评论

        本文标题:iphone本地架设http服务器

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