国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

IOS利用CocoaHttpServer搭建手機本地服務器

瀏覽:8日期:2022-09-16 16:33:57
緣起

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

原理

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

實現(xiàn)

1.下載CocoaHTTPServer

2.導入CocoaHTTPServer-master目錄下的Core文件夾

3.導入Samples/SimpleFileUploadServer目錄下的MyHTTPConnection類文件和web文件夾

IOS利用CocoaHttpServer搭建手機本地服務器

4.導入Vendor目錄下的CocoaAsyncSocket、CocoaLumberjack文件夾

5.打開MyHTTPConnection.m文件,根據(jù)標記 #pragma mark multipart form data parser delegate 跳轉或者直接找到139行的 *- (void) processStartOfPartWithHeader:(MultipartMessageHeader ) header 方法,把第151行的uploadDirPath改為

NSString *uploadDirPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

這個路徑是上傳文件的存儲路徑

6.在適當?shù)牡胤脚渲胹erver啟動。這里以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 interfacestemp_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 iPhoneif([[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.運行后,控制臺會打印出端口號和ip,在電腦端瀏覽器里輸入ip+端口號訪問即可,如果成功的話會看到如下界面:

IOS利用CocoaHttpServer搭建手機本地服務器

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

以上就是IOS利用CocoaHttpServer搭建手機本地服務器的詳細內容,更多關于IOS用CocoaHttpServer搭建服務器的資料請關注好吧啦網其它相關文章!

標簽: IOS
相關文章:
主站蜘蛛池模板: 泸西县| 东港市| 普兰店市| 石家庄市| 尚义县| 平阳县| 海淀区| 闽清县| 宜兴市| 边坝县| 抚顺市| 交城县| 井冈山市| 淳安县| 临武县| 正镶白旗| 西平县| 蒙城县| 新干县| 张家界市| 吴忠市| 陵川县| 深州市| 辽中县| 深州市| 专栏| 精河县| 昌宁县| 中江县| 全南县| 班戈县| 萝北县| 灵武市| 伊川县| 耿马| 荥阳市| 佛山市| 云安县| 永顺县| 邵阳县| 甘孜县|