利用CocoaHttpServer搭建手機(jī)本地服務(wù)器

緣起

今天用暴風(fēng)影音看視頻,然后發(fā)現(xiàn)它有個(gè)功能,wifi傳片,感覺挺有意思,然后就上網(wǎng)查了下相關(guān)內(nèi)容。

原理

使用CocoaHTTPServer框架,在iOS端建立一個(gè)本地服務(wù)器,只要電腦和手機(jī)連入同一熱點(diǎn)或者說網(wǎng)絡(luò),就可以實(shí)現(xiàn)通過電腦瀏覽器訪問iOS服務(wù)器的頁面,利用POST實(shí)現(xiàn)文件的上傳。

實(shí)現(xiàn)

1.下載CocoaHTTPServer

2.導(dǎo)入CocoaHTTPServer-master目錄下的Core文件夾

3.導(dǎo)入Samples/SimpleFileUploadServer目錄下的MyHTTPConnection類文件和web文件夾

導(dǎo)入web文件夾的時(shí)候,一定要使用真實(shí)的目錄,而不是xcode的虛擬目錄


web文件夾導(dǎo)入.png

4.導(dǎo)入Vendor目錄下的CocoaAsyncSocket、CocoaLumberjack文件夾

5.打開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)訪問即可,如果成功的話會(huì)看到如下界面:

訪問成功.png

8.如果上傳成功,網(wǎng)頁上會(huì)出現(xiàn)上傳的文件名,可以在沙盒里驗(yàn)證文件是否上傳成功

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、首先導(dǎo)入第三方目錄結(jié)構(gòu) 然后導(dǎo)入你需要加載的html資源 2、在appdelegate中開啟服務(wù),獲取端口號(hào) ...
    c608閱讀 16,510評(píng)論 7 15
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評(píng)論 19 139
  • 名詞延伸 通俗的說,域名就相當(dāng)于一個(gè)家庭的門牌號(hào)碼,別人通過這個(gè)號(hào)碼可以很容易的找到你。如果把IP地址比作一間房子...
    楊大蝦閱讀 20,783評(píng)論 2 56
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,253評(píng)論 6 342
  • 剛步去社會(huì)的殿堂,擁有了自己人生中的第一份工作,和第一份收入,自己本身做銷售出生,自己也有些投資的想法,也會(huì)經(jīng)常在...
    lucky秀_b07c閱讀 224評(píng)論 1 5

友情鏈接更多精彩內(nèi)容