首页
文章
留言
首页
文章
留言
iOS开发之WiFi传输文件
2016 年 11 月 11 日
移动应用
Objective-C
iOS
iOS 开发中难免会有需要通过 WiFi 传输文件这种需求,下面就总结一下使用 WiFi 传输文件的实例,我的一个上线项目已使用该方法成功进行传输文件。这里需要用到 CocoaHTTPServer。 #### 步骤: 1、下载 CocoaHTTPServer 2、解压后,将 CocoaHTTPServer-master 目录下的 Core 导入工程。 3、打开 Samples/SimpleFileUploadServer,将其中的 MyHTTPConnection 类文件、web 文件夹导入工程。 4、打开 Vendor,将其中的 CocoaAsyncSocket、CocoaLumberjack 文件夹导入。 所有要导入的文件如下图所示:注意,其中的 HYBIPHelper 为获取本地ip的工具类,不是必要的,请忽视。  5、打开工程,打开 MyHTTPConnection.m,根据标记 `#pragma mark multipart form data parser delegate` 跳转或者直接找到139行:` - (void) processStartOfPartWithHeader:(MultipartMessageHeader*) header` 方法,将其中 filePath 的值修改为 iOS 的某个目录,这个路径是上传的文件存储的路径。 我们可以已 Caches 为例: ```objectivec //NSString* uploadDirPath = [[config documentRoot] stringByAppendingPathComponent:@"upload"]; NSString *uploadDirPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; ``` 6、在适当的地方配置 server 启动,这里在 ViewController 中操作: ```objectivec #import "ViewController.h" #import "HTTPServer.h" #import "DDLog.h" #import "DDTTYLogger.h" #import "MyHTTPConnection.h" // 获取IP地址 #include
#include
@interface ViewController (){ HTTPServer *httpServer; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; httpServer = [[HTTPServer alloc] init]; [httpServer setType:@"_http._tcp."]; // webPath是server搜寻HTML等文件的路径 NSString *webPath = [[NSBundle mainBundle] resourcePath]; [httpServer setDocumentRoot:webPath]; [httpServer setConnectionClass:[MyHTTPConnection class]]; NSError *err; if ([httpServer start:&err]) { NSLog(@"port %hu",[httpServer listeningPort]); }else{ NSLog(@"%@",err); } // 获取局域网IP地址 [self getIpAddresses]; } // 获取局域网IP地址 - (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); NSLog(@"%@", address); return address; } // 查询手机里面是否已经导入文件成功 - (IBAction)clicked { NSFileManager *fileManager=[NSFileManager defaultManager]; NSArray *pathcaches = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString* cacheDirectory = [pathcaches objectAtIndex:0]; if ([fileManager fileExistsAtPath:cacheDirectory]) { NSArray *childerFiles=[fileManager subpathsAtPath:cacheDirectory]; for (NSString *fileName in childerFiles) { // 文件名(本地文件视频名),用于界面展示 NSLog(@"fileName名 %@", fileName); } } } @end ``` 开始运行代码,测试时一定要在真机上运行,会打印出如下:  7、运行后,在浏览器输入:`ip:port` 访问即可,如果成功,会看到如下页面:  选择一个文件点击:“Submit” 上传,如下图:  8、点击 APP 里的按钮调用:`- (IBAction)clicked` 方法,如果上传成功,会打印如下图:  我们看到了:`text.txt` 文件就是刚从浏览器上传到手机里的文件,`Snapshots` 文件是系统本来就有的文件。如果两台手机连接在同一局域网,另一台手机用浏览器输入 `ip:port` 这个地址,应该也可以通过手机上传文件到手机。
1
相关文章
iOS开发之Touch ID指纹解锁实例
iOS开发之多线程
iOS开发之类与类扩展
iOS开发之收集崩溃信息
iOS开发之定时执行任务
全部分类
前端
后端
运维
架构
算法
数据库
移动应用
桌面应用
程序开发
热门标签
Shell
macOS
CSS
HTML
Linux
Sphinx
CentOS
Nginx
Redis
iOS
PHP
Elasticsearch
NoSQL
Git
Lua
Objective-C
OpenResty
Python
多线程
JavaScript
MySQL
Kubernetes
C++
MongoDB
Kafka
Docker
Composer
GUI
爬虫
Supervisor
Qt
Android
热门文章
PHP开发之字符串处理
Redis基本使用总结
Redis、MemCache、MongoDB比较
iOS开发之面向对象
Supervisor使用总结
iOS开发之多线程
PHP使用Kafka
iOS开发之定时执行任务
macOS常用命令
OpenResty+Lua+Kafka收集日志