WIFI传输

移动开发
类似多看和iReader的WIFI传输功能。

源码简介:类似多看和iReader的WIFI传输功能。

源码效果:

源码片段:

  1. #import "CocoaWebResourceViewController.h" 
  2.   
  3. @implementation CocoaWebResourceViewController 
  4.   
  5. // load file list 
  6. - (void)loadFileList 
  7.     [fileList removeAllObjects]; 
  8.     NSString* docDir = [NSString stringWithFormat:@"%@/Documents", NSHomeDirectory()]; 
  9.     NSDirectoryEnumerator *direnum = [[NSFileManager defaultManager] 
  10.                                       enumeratorAtPath:docDir]; 
  11.     NSString *pname; 
  12.     while (pname = [direnum nextObject]) 
  13.     { 
  14.   
  15.         [fileList addObject:pname]; 
  16.     } 
  17.   
  18. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
  19. - (void)viewDidLoad { 
  20.     fileList = [[NSMutableArray alloc] init]; 
  21.     [self loadFileList]; 
  22.       
  23.     // set up the http server 
  24.     httpServer = [[HTTPServer alloc] init]; 
  25.     [httpServer setType:@"_http._tcp."];     
  26.     [httpServer setPort:8080]; 
  27.     [httpServer setName:@"CocoaWebResource"]; 
  28.     [httpServer setupBuiltInDocroot]; 
  29.     httpServer.fileResourceDelegate = self; 
  30.   
  31.     [super viewDidLoad]; 
  32.   
  33.   
  34. - (void)didReceiveMemoryWarning { 
  35.     [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview 
  36.     // Release anything that's not essential, such as cached data 
  37.   
  38.   
  39. - (void)dealloc { 
  40.     httpServer.fileResourceDelegate = nil; 
  41.     [httpServer release]; 
  42.     [fileList release]; 
  43.     [super dealloc]; 
  44.   
  45. #pragma mark actions 
  46. - (IBAction)toggleService:(id)sender 
  47.     NSError *error; 
  48.     if ([(UISwitch*)sender isOn]) 
  49.     { 
  50.         BOOL serverIsRunning = [httpServer start:&error]; 
  51.         if(!serverIsRunning) 
  52.         { 
  53.             NSLog(@"Error starting HTTP Server: %@", error); 
  54.         }        
  55.         [urlLabel setText:[NSString stringWithFormat:@"http://%@:%d", [httpServer hostName], [httpServer port]]]; 
  56.     } 
  57.     else 
  58.     { 
  59.         [httpServer stop]; 
  60.         [urlLabel setText:@""]; 
  61.     } 
  62.   
  63. #pragma mark WebFileResourceDelegate 浏览器上传文件后的回调方法 
  64. // number of the files 
  65. - (NSInteger)numberOfFiles 
  66.     return [fileList count]; 
  67.   
  68. // the file name by the index 
  69. - (NSString*)fileNameAtIndex:(NSInteger)index 
  70.   
  71.     return [fileList objectAtIndex:index]; 
  72.   
  73. // provide full file path by given file name 
  74. - (NSString*)filePathForFileName:(NSString*)filename 
  75.     NSString* docDir = [NSString stringWithFormat:@"%@/Documents", NSHomeDirectory()]; 
  76.   
  77.     return [NSString stringWithFormat:@"%@/%@", docDir, filename]; 
  78.   
  79. // handle newly uploaded file. After uploading, the file is stored in 
  80. // the temparory directory, you need to implement this method to move 
  81. // it to proper location and update the file list. 
  82. - (void)newFileDidUpload:(NSString*)name inTempPath:(NSString*)tmpPath 
  83.     if (name == nil || tmpPath == nil) 
  84.         return
  85.     NSString* docDir = [NSString stringWithFormat:@"%@/Documents", NSHomeDirectory()]; 
  86.     NSString *path = [NSString stringWithFormat:@"%@/%@", docDir, name]; 
  87.     NSFileManager *fm = [NSFileManager defaultManager]; 
  88.     NSError *error; 
  89.   
  90.     if (![fm moveItemAtPath:tmpPath toPath:path error:&error]) 
  91.     { 
  92.         NSLog(@"can not move %@ to %@ because: %@", tmpPath, path, error ); 
  93.     } 
  94.           
  95.     [self loadFileList]; 
  96.       
  97.   
  98. // implement this method to delete requested file and update the file list 
  99. - (void)fileShouldDelete:(NSString*)fileName 
  100.   
  101.     NSString *path = [self filePathForFileName:fileName]; 
  102.     NSFileManager *fm = [NSFileManager defaultManager]; 
  103.     NSError *error; 
  104.     if(![fm removeItemAtPath:path error:&error]) 
  105.     { 
  106.         NSLog(@"%@ can not be removed because:%@", path, error); 
  107.     } 
  108.     [self loadFileList]; 
  109.   
  110. @end 

下载地址:http://down.51cto.com/data/2103329

责任编辑:倪明 来源: devstore
相关推荐

2010-03-24 15:31:34

2016-02-22 09:34:24

WiFi光纤传输速度

2015-11-19 14:46:40

2011-08-01 13:05:27

路由器WiFi无线

2012-06-28 20:34:59

极速WiFi涡流电波WiFi

2012-06-14 10:56:14

WiFi7Gbps无线

2021-10-08 14:43:24

WiFi 5WiFi 6远程

2013-06-24 10:40:01

千兆WiFi802.11ac无线网络

2015-08-13 17:29:35

WiFi

2019-09-20 09:22:00

蓝牙WiFiZigBee

2021-06-23 09:03:19

iPhone WiFi bug漏洞

2015-06-11 13:07:31

WIFIWIFI安全风险示意图

2022-08-11 15:44:30

WiFi 6WiFi 7物联网

2012-06-25 09:38:33

全球最快手机快牙

2009-02-18 09:42:58

TCPISO传输

2017-02-07 16:20:39

光纤无线通信

2015-11-04 10:25:14

WiFi黑科技感知

2021-10-28 11:48:17

WiFi网络网络安全WiFi嗅探

2017-02-27 20:00:08

16wifi公共WIFI无线网络

2010-03-26 10:19:34

无线应用方案Nos-W
点赞
收藏

51CTO技术栈公众号