How to hide implementation of Cocoapods | Swift
Written a great cocoapod, but want to hide its source code from the users? You are in the right place. No further delays, let’s get started.
Open up Xcode and choose `framework` from iOS tab.
Next, give it a good name , in my case its ‘HiddenFramework’. I am saving my framework project on desktop.
Now, I am adding a dummy file and inside that file, I am writing some dummy code.
Now, after you are done with your code implementation, choose any device , either from simulator or your physical device. Remember, if you choose simulator, your framework will only work on simulator and in case you choose a physical device, your framework will only work on physical device. (For framework to work on both physical device and simulator, I’ll cover that area in upcoming article.) I have chosen a device from my simulator. After that, build your framework. Upon successful build, inside “Project Navigator”, look for “Products” folder, expand it and inside that folder, you’ll see your framework on top naming ‘HiddenFramework.framework’.
Right click on that framework file and click ‘Show in finder’. With that finder window opened up, also open up your project home folder which was on Desktop and drag your framework folder in to the project home folder. I am also placing a license file into that folder. You can get license file of your own and put into that folder. Your project home folder now should look like this :
Great! So for the next step, we will zip our “license” and “HiddeFramework.framwork” files and we will name our zipped file “HiddenFramework.zip”. The way we zip a file/s is that we select the files we want to zip, and then upon right click, we choose “Compress”. Upon compressing, a zipped folder with name “Archive.zip” will be made, which you can of course rename.
You are doing awesome so far! Next, we want to upload that zipped file to our server. Now don’t worry if you don’t know where to host it. For starting purpose, we can upload our file on DropBox. So open up your dropbox and upload the file there.
Right click on this file, get the shareable link, and save it up somewhere as we gonna need it in a minute. With the link we have, we can not directly download the file, rather if we paste the url and hit enter, we are directed to dropbox page where we have to download the file manually. We want to generate a link which will download the file directly once we hit enter. For that go to the link below, paste your file link that you saved earlier, paste it and get the direct download link. https://syncwithtech.blogspot.com/p/direct-download-link-generator.html
We will need that link in our podspec file, which we will be creating in a minute.
Open up your terminal and type the following command:
pod spec create HiddenFramework
This will first navigate you to your project folder and then it will create a podspec file. Open up the file in text editor.
open -a textEdit HiddenFramework.podspec
Inside podspec file, you’ll see a lot of comments and instructions. Its a mess! Just follow the image below and type out all the info accordingly.
For s.source , we have to put the direct download url, that we have generated for our file.
The next step is to validate that podspec file, and for that go to terminal, while in the same directory as of this podspec file, type the following command :
pod spec lint
Well, if everything goes nice and according to plan, we will see a green coloured awesome message informing us that our framework has passed validation.
Now, there are chances that instead of that, you receive a message in red, stating that our framework didn’t passed validation. The error will be clearly mentioned in the terminal. Most common errors are if your summary is not meaningful, or description is shorter than summary, or any other syntax error. But the error which I faced was ‘The `vendored_frameworks` pattern did not match any file’ , which had something to do with the ‘s.source’ link which I was putting in and it had haunted me for days. Well in that case, make sure you follow the above instructions carefully, like copying the source link correctly, managing the directories of the folders and podspec accordingly, naming of the framework etc.
We are almost there! Now to push and publish our framework podspec on CocoaPods repo, we need to register our email with cocoapod. If you are already registered, your are good to go, or else, inside terminal , enter the following command :
pod trunk register email@example.com 'Full Name'
Go to your email and verify by clicking on the link sent to you by cocoapod.
For the next step where you want to push your podspec file, first go to GitHub, and create a new repository named ‘HiddenFramework’. Now through terminal, initialise your git repo locally, add podspec file, commit it locally with a message.
git add HiddenFramework.podspec
git commit -m "First commit"
To set up your remote repo, copy the following line from your github repo which you’ve just created.
git remote add origin https://github.com/abubakar-9012/HiddenFramework.git
After the remote origin is set, push your code.
git push -u origin master
You should tag your every new release. As this is our first release, we will tag it with 0.0.1. Remember, this should be the same as our s.version in podspec file. Go to the GitHub repo, click on tags which is beside the name of your branch, click on `Create a new release` and give 0.0.1 as Tag version and any suitable title.
Click publish release, and you are good to go!
Now that we are all setup, its time to publish our podspec to cocoapod. Run the following command in terminal :
pod trunk push HiddenFramework.podspec
If everything goes well and nice, you’ll get a congratulations message
Congrats! You have done it. Get yourself a treat!
Now you can check and verify if your framework is working properly, create a new project, initialise a pod file via terminal , and add your framework to podfile. After that, install the pod. If you get an error like so :
Don’t worry! Just run another command.
pod repo update
And then try installing pod again. This may take several attempts and you may face the same error again and again even after running the update command. So just be patient and wait for 4 to 5 min and then run the update command again and after that install the pod. You should be good to go!
Voila!! That was a long journey but finally we did it. Great work. Now you can see how your code implementation is hidden. Only your podspec file is showing up in git hub repo, and everything else is hidden. One last thing, your pod will be on cocoapods.org after after some time, it can be hours or even a day, so be patient.
Feel free to comment if you are facing any problem while following this article!