簡介
目前一些應(yīng)用里有用到在Iphone上架設(shè)http服務(wù)器,其主要用在以下幾個(gè)方面
- 當(dāng)需要在iphone本地架設(shè)一個(gè)小型http網(wǎng)站碱工,供用戶來使用娃承。
- 或者希望通過自己的應(yīng)用把相應(yīng)的文件導(dǎo)入到電腦里,例如:目前大多數(shù)視頻應(yīng)用怕篷,把電腦上的視頻導(dǎo)入到本應(yīng)用历筝。(必須在同一WIFI)
- 預(yù)加載視頻。在瀏覽網(wǎng)上視頻時(shí)候廊谓,在用到hls等視頻切片技術(shù)的視頻源時(shí)候梳猪,可以在本地架設(shè)一個(gè)http服務(wù)器預(yù)下載后邊的視頻,進(jìn)行預(yù)加載蒸痹,以達(dá)到視頻播放流暢春弥。
在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!
環(huán)境
系統(tǒng) | 開發(fā)工具 |
---|---|
iOS9 | xCode 7.3 |
用法
以在本地創(chuàng)建http web為例
在官網(wǎng)下載源碼工程
這里使用的是一個(gè)小forkcocoa-web-resource
-
Core文件夾
架設(shè)http服務(wù)器Core 核心類 -
Extensions 文件夾
WebDAV實(shí)現(xiàn)類 -
Sample 文件夾
CocoaHTTPServer基本使用 -
Vendor
第三方日志類
創(chuàng)建工程
創(chuàng)建一個(gè)名為iPhoneLocalHttpServer
的Demo工程
引入庫
加載資源
資源加載失敗打不開網(wǎng)頁404錯(cuò)誤
定義
#import "HTTPServer.h"
interface ViewController ()<UIWebViewDelegate,WebFileResourceDelegate>
{
HTTPServer *httpServer;
UIWebView *mWebView;//測試webview
UILabel *urlLabel;//顯示ip端口的
NSMutableArray *fileList;
}
實(shí)現(xiàn)
- (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];
});
}
模擬器運(yùn)行:
safari打開
使用模擬器safari打開127.0.0.1:8088(真機(jī)上不行匿沛,還不知道原因)
iOS http設(shè)置
-
在Filter中搜索Info.plist扫责,選擇Info.plist進(jìn)行編輯
-
按照上面提到的方式添加信息,正確的修改會(huì)看到下圖這個(gè)樣子逃呼,注意類型NSAppTransportSecurity為Dictionary鳖孤,NSAllowsArbitraryLoads為Boolean,復(fù)制粘貼的時(shí)候抡笼,不要多了空格苏揣,segment fault 頁面上直接復(fù)制,經(jīng)常會(huì)多一個(gè)出空格蔫缸!
Demo
git
地址
兩個(gè)GCDWebServer