I while ago I did a small experiment to run Caddy on my iPhone. I currently have no time to do something with it, and actually build a useful Caddy iOS app, but I wanted to do a quick writeup about how you can achieve this. So others can maybe do something with Caddy on iOS, because compiling a Go project for iOS is really easy!
Installing Go Mobile
You first need to install Xcode and the Xcode command line tools.
Then you can simply install Go Mobile with the following commands:
$ go get golang.org/x/mobile/cmd/gomobile
$ gomobile init # it might take a few minutes
More information can be found in the official docs.
Building an iOS framework bundle from Caddy
Now you have to create a framework bundle of the Caddy project, which you can then use in your iOS application:
$ gomobile bind -target=ios github.com/mholt/caddy/caddy/caddymain
This will generate a framework called Caddymain.framework
.
Building an iOS app with the framework
Now you can just drag and drop the compiled framework bundle to the Xcode project of the iOS app.
Check “Copy items if needed” if you want to include a copy of the framework bundle within the Xcode repo. Otherwise, modifying the Go package source code and rerunning gomobile bind ...
will update the Caddymain.framework
in the app.
If everything went well, your Xcode project overview will look like this:
Once this is done, you can import the Caddymain
framework in your Swift files and call the functions from the Caddy package and use them to start Caddy.
I put everything in my ViewController
:
import UIKit
import Caddymain
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Don't forget to compile with ENABLE_BITCODE = 'No'
// Startup Caddy...
CaddymainRun()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Now you can hit run
, and you’ll be running Caddy on your iPhone!
Note: since Go mobile doesn’t compile frameworks with bitcode enabled, you have to turn bitcode off in the project settings in Xcode: Go to the Build Settings tab. Type bitcode in the search field and change the Enable Bitcode setting to No.
Conclusion
Running Go code on an iOS device is easy!
So don’t hesitate to do some useful stuff with it yourself!