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

Topic marketing method for May Day!

Long time no see. In this episode of 5 Minutes to...

Exploring the PHP kernel: PHP's FastCGI

CGI stands for Common Gateway Interface, which al...

Are shared bikes doomed to fail?

The closure of shared bikes has not slowed down pe...

When Jing Haipeng met the Swiss astronaut, he told a "tearful" story

Why can't Chinese astronaut Jing Haipeng cry ...

Paid gameplay for acquiring new customers

Acquiring new customers is an eternal mission for...

Channel Operation | If I give you 10 million, how would you spend it?

I believe that those who are involved in promotio...

What kind of shrimp do you like best?

Cat Killing Warning Please bring your own wifi an...

What do you think of the suddenly disappearing stars and twinkling stars?

A strange star flickering in the center of the Mi...

How will 5G change our lives? 1G of data may only cost a few cents

Chinanews.com Beijing, July 6 (Reporter Wu Tao) R...

6 creative tips for WeChat Moments ads in the catering industry

The spring is beautiful and suitable for travelin...