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

Spicy kiwi? More weird flavors

Produced by: Science Popularization China Produce...

Do you understand the “blood” of the earth?

Do you understand the “blood” of the earth? Quest...

How can we operate a Baidu bidding account well?

For many people who do Baidu bidding promotion, h...

Why are Internet giants aggressively attacking the film industry?

In 2014, Internet companies entered the film indu...

How does Watsons conduct user operations?

The Internet has been developing for more than 10...

Car brand marketing strategy!

As early as May of this year, Lao Zhao continued ...

Why is altitude sickness fatal?

Why do people need oxygen? If oxygen inhalation i...

Lucheng SEO training: Several common black hat methods

In the SEO industry, the only thing that does not...

6 marketing breakthroughs for billion-dollar brands

In the trillion-dollar new consumer market, why i...