IOS利用CocoaHttpServer搭建手機(jī)本地服務(wù)器
緣起
今天用暴風(fēng)影音看視頻,然后發(fā)現(xiàn)它有個(gè)功能,wifi傳片,感覺(jué)挺有意思,然后就上網(wǎng)查了下相關(guān)內(nèi)容。
原理
使用CocoaHTTPServer框架,在iOS端建立一個(gè)本地服務(wù)器,只要電腦和手機(jī)連入同一熱點(diǎn)或者說(shuō)網(wǎng)絡(luò),就可以實(shí)現(xiàn)通過(guò)電腦瀏覽器訪問(wèn)iOS服務(wù)器的頁(yè)面,利用POST實(shí)現(xiàn)文件的上傳。
實(shí)現(xiàn)
1.下載CocoaHTTPServer
2.導(dǎo)入CocoaHTTPServer-master目錄下的Core文件夾
3.導(dǎo)入Samples/SimpleFileUploadServer目錄下的MyHTTPConnection類(lèi)文件和web文件夾

4.導(dǎo)入Vendor目錄下的CocoaAsyncSocket、CocoaLumberjack文件夾
5.打開(kāi)MyHTTPConnection.m文件,根據(jù)標(biāo)記 #pragma mark multipart form data parser delegate 跳轉(zhuǎn)或者直接找到139行的 *- (void) processStartOfPartWithHeader:(MultipartMessageHeader ) header 方法,把第151行的uploadDirPath改為
NSString *uploadDirPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
這個(gè)路徑是上傳文件的存儲(chǔ)路徑
6.在適當(dāng)?shù)牡胤脚渲胹erver啟動(dòng)。這里以AppDelegate為例
#import "AppDelegate.h"
#import <ifaddrs.h>
#import <arpa/inet.h>
#import "HTTPServer.h"
#import "DDLog.h"
#import "DDTTYLogger.h"
#import "MyHTTPConnection.h"
@interface AppDelegate ()
@property (nonatomic, strong) HTTPServer * httpServer;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_httpServer = [[HTTPServer alloc] init];
[_httpServer setPort:1234];
[_httpServer setType:@"_http._tcp."];
// webPath是server搜尋HTML等文件的路徑
NSString * webPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"web"];
[_httpServer setDocumentRoot:webPath];
[_httpServer setConnectionClass:[MyHTTPConnection class]];
NSError *err;
if ([_httpServer start:&err]) {
NSLog(@"port %hu",[_httpServer listeningPort]);
}else{
NSLog(@"%@",err);
}
NSString *ipStr = [self getIpAddresses];
NSLog(@"ip地址 %@", ipStr);
return YES;
}
- (NSString *)getIpAddresses{
NSString *address = @"error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
if (success == 0)
{
// Loop through linked list of interfaces
temp_addr = interfaces;
while(temp_addr != NULL)
{
if(temp_addr->ifa_addr->sa_family == AF_INET)
{
// Check if interface is en0 which is the wifi connection on the iPhone
if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"])
{
// Get NSString from C String
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
}
}
temp_addr = temp_addr->ifa_next;
}
}
// Free memory
freeifaddrs(interfaces);
return address;
}
7.運(yùn)行后,控制臺(tái)會(huì)打印出端口號(hào)和ip,在電腦端瀏覽器里輸入ip+端口號(hào)訪問(wèn)即可,如果成功的話會(huì)看到如下界面:

8.如果上傳成功,網(wǎng)頁(yè)上會(huì)出現(xiàn)上傳的文件名,可以在沙盒里驗(yàn)證文件是否上傳成功
以上就是IOS利用CocoaHttpServer搭建手機(jī)本地服務(wù)器的詳細(xì)內(nèi)容,更多關(guān)于IOS用CocoaHttpServer搭建服務(wù)器的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- [Asp.Net Core]用Blazor Server Side實(shí)現(xiàn)圖片驗(yàn)證碼
- [Asp.Net Core] 淺談Blazor Server Side
- Ant Design Blazor 組件庫(kù)的路由復(fù)用多標(biāo)簽頁(yè)功能
- HTTP中header頭部信息詳解
- Golang簡(jiǎn)單實(shí)現(xiàn)http的server端和client端
- Golang實(shí)現(xiàn)http server提供壓縮文件下載功能
- 在Golang中使用http.FileServer返回靜態(tài)文件的操作
- 基于http.server搭建局域網(wǎng)服務(wù)器過(guò)程解析
- golang的httpserver優(yōu)雅重啟方法詳解
- Blazor Server 應(yīng)用程序中進(jìn)行 HTTP 請(qǐng)求
相關(guān)文章
ios動(dòng)態(tài)設(shè)置lbl文字標(biāo)簽的高度
本文給大家分享的是ios動(dòng)態(tài)設(shè)置lbl文字標(biāo)簽的高度寬度的方法,一共給大家匯總了3種方法,小伙伴們根據(jù)自己的項(xiàng)目需求自由選擇。2015-05-05
IOS中Weex 加載 .xcassets 中的圖片資源的實(shí)例詳解
這篇文章主要介紹了IOS中Weex 加載 .xcassets 中的圖片資源的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文介紹能幫助到大家,實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-08-08
iOS倒計(jì)時(shí)的實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了iOS倒計(jì)時(shí)的實(shí)現(xiàn)方法,點(diǎn)擊進(jìn)行倒計(jì)時(shí)準(zhǔn)備,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
iOS開(kāi)發(fā)中Subview的事件響應(yīng)以及獲取subview的方法
這篇文章主要介紹了iOS開(kāi)發(fā)中Subview的事件響應(yīng)以及獲取subview的方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-09-09
iOS UILabel 設(shè)置內(nèi)容的間距及高度的計(jì)算示例
本篇文章主要介紹了iOS UILabel 設(shè)置內(nèi)容的間距及高度的計(jì)算示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
IOS開(kāi)發(fā)基礎(chǔ)之二維數(shù)組詳解
這篇文章主要介紹了IOS開(kāi)發(fā)基礎(chǔ)之二維數(shù)組詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04
iOS開(kāi)發(fā)實(shí)現(xiàn)隨機(jī)圖片驗(yàn)證碼封裝
這篇文章主要介紹了iOS開(kāi)發(fā)實(shí)現(xiàn)隨機(jī)圖片驗(yàn)證碼封裝,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08

