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

APP promotion and operation planning, a new way of event operation!

Suppose a scenario where people are shopping offl...

Cauliflower, broccoli, and kale are actually "brothers"

After eating them for so many years, I finally re...

An analysis of how to play private domain traffic across platforms!

Private domain traffic is not exclusive to the We...

The Mystery of Gigantopithecus and Humans in Lingnan's Prehistoric History

Lingnan entered the Cenozoic Paleocene, and life ...

What brand advantages are needed to use low-price strategies for marketing?

Many people believe that if the product has the s...

If you see a millipede on the road, should you drive it away?

Review expert: Ancient Mingdi Lian (He Lin) Zhihu...