WIFI transmission

WIFI transmission

Source code introduction: Similar to the WIFI transmission function of Duokan and iReader.

Source code effect:

Source code snippet:

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

Download address: http://download..com/data/2103329

<<:  Avatar caching strategy recommended by experts

>>:  Imitation of Meituan city selection interface

Recommend

How to guide users to create content from 0 to 1?

1. What did Lukou Community do from 0 to 1? Lukou...

24-hour emergency response: Beijing's flu cases increased by 16.69% year-on-year

Hot News TOP NEWS Beijing CDC: Influenza cases in...

Advanced Operations: How to use 4P theory to achieve growth?

As we all know, operations are divided into produ...

4 marketing details to make your APP stand out

Since I represented "Zhimeng Mobile" to...

The undercurrent of climate change is hidden in this wave of atmosphere

Wei Ke We live at the bottom of the Earth's a...

Using mobile phones on airplanes will have a clear conclusion in 2016

At present, every time passengers take a flight i...

[Tutorial] How did a 16-year-old hacker install Windows 95 on a smartwatch?

Corbin Davenport, a 16-year-old hacker from Georg...

Five reasons why Apple could develop a professional camera product

Although we don't know what industry Apple wa...

Mr. Xin and Mr. Ling's "Healthy Meta-Universe"

This is the 3538th article of Da Yi Xiao Hu Mr. X...