Customize GCDWebServer default download webpage with method swizzling
Currently I am developing a simple voice memo iOS app. One basic feature is to let users download their recorded memos from any browser. One way to do this is to set up a http server running inside the app to provide a file download feature.
I googled some solutions and found that GCDWebServer was a very popular lightweight http server library for iOS apps. Although it only has an Objective C version, it can be easily integrated into a Swift project via CocoaPods. It is also very easy to set up a http server via GCDWebUploader using just a few lines of code:
You can get the address information from the IDE debug window after the server is started. Then when you access this address via any browser, you will see a web page that looks like this:
Could the solution really be this easy? It seemed to be a piece of cake. I was initially pretty satisfied but then I realized there were several problems. The default web page provides basic file management features but they were too many for my app. What I need is only to allow users to download files without changing anything else. Because I save voice memo file information in a database, if users can upload files, create folders, or delete or rename files, it will make them inconsistent with the information in the database and mess up the app’s functioning.
I checked the source code and found GCDWebUploader did have some hook methods to prevent it:
If you set No/false to return the values of all of these hook methods, it will display a warning when users try to do some editing:
Although this was a workable solution, the webpage displayed was not very visually appealing.
For my voice memo app, simplicity is paramount. I want to provide users with a neat webpage and hide any unnecessary buttons. Since I do not want to bother to modify the source code for the time being, if I can modify the web page content before it returns to requests from browsers instead, this should work too. I checked the source code again and found the html content for the web page is returned by initial method initWithHTML:(NSString*)html in GCDWebServerDataResponse.m
So if we can modify the html content here, then it will be possible to resolve this issue. This is where method swizzling comes in.
You may have used this black magic many times since the Objective C era. The way to use method swizzling changes a little bit in Swift, but you can still use it. Check out this StackOverflow link to read about the different ways to use method swizzling in Swift 3.0 and Swift 4.0:
How can I implement method swizzling in Swift 3.0 ? I've read nshipster article about it, but in this code's chunk…stackoverflow.com
What you need to do next is just add the GCDWebServerDataResponse extension and put your method swizzling inside. Below is my version:
One thing you should notice is that when you create the method (swizzled_init(html:)) to replace the original method (init(html:)), you should let it return to Self since the original method is an initialized method, otherwise the magic will not work.
Then we can start the http server and check our download page again. It now appears like this:
Much neater, right? Now I can return to drinking my tea and coding in peace without worrying about any unexpected user modifications.
Since you can replace the html content, you can even change the entire webpage into your own implementation if you want. But you have to understand the source code well enough and make sure that your own page works as expected.
This solution comes with its pros and cons.
Pros: It is non-invasive and easy to implement and very flexible.
Cons: It relies on the inside logic of the CGDWebServer. If the logic is modified in any future version, then you have to modify your code too. You also have to do some extra work to parse html.
Maybe the ultimate way to solve the problem is to modify the source code of GCDWebSever and create a pull request on Github and hope the author approves the modification, but that is another story and may not solve your problem in time.