Installing SwiftDiscord for iOS
Please note that you should follow the instructions in this document rather than in the main README if you intend to build for iOS.
Prerequisites
- SwiftDiscord 6
- Xcode 9.0
- iOS SDK 11.0
- Homebrew
I have absolutely no idea if this will continue to work with future or previous versions of SwiftDiscord/Xcode/iOS. Your mileage may vary.
Instructions
You may be tempted to skip some steps or to not follow them in order. If you’d like to get this working at any point within the next few hours, it is highly recommended you do not do that. If you do somehow get this to work without completing a certain step, please let me know so I can update the instructions.
- Install dependencies:
brew tap vapor/tap && brew install ctls && brew install opus && brew install libsodium - Make your project folder:
mkdir SwiftDiscord-iOS && cd SwiftDiscord - Initialize a new Swift package:
swift package init --type executable Modify your
Package.swiftso you have SwiftDiscord as a dependency:// swift-tools-version:3.1 import PackageDescription let package = Package( // you can replace name (below) with a string of your choosing, the xcode project that is generated will have this name name: "SwiftDiscord-iOS", dependencies: [ .Package(url: "https://github.com/nuclearace/SwiftDiscord", majorVersion: 6) ] )Download SwiftDiscord:
swift package updateBuild SwiftDiscord:
swift build -Xlinker -L/usr/local/lib -Xlinker -lopus -Xcc -I/usr/local/includeNow comes the hardest part:
mkdir libsManually compile & build the following projects:
I hope you like reading READMEs.
Move the resulting compiled libraries (.a files) into your new
libsdirectory. You should have the following files in there:libcrypto.alibopus.alibsodium.alibssl.a
To be honest, I’m not expecting anyone to make it past the last step, so if by some miracle you actually managed to fill
libswith the required libs, give yourself a pat on the back. Go make coffee or something.Generate an Xcode project and open it:
swift package generate-xcodeproj && open SwiftDiscord-iOSIn the Build Settings for the
SwiftDiscordtarget:- Add the following to
Valid Architectures:arm64armv7armv7s - Add your
libsfolder to theLibrary Search Paths:$(SRCROOT)/libs- You may need to switch from
BasictoAllBuild Settings in order to see this key.
- You may need to switch from
- Also add
/usr/local/libto yourLibrary Search Paths, after your ownlibsfolder. - Add
/usr/local/includeto yourHeader Search Paths.
- Add the following to
In the Build Settings for the
DiscordOpustarget:- Repeat steps 2, 3, and 4 from the previous target, but for this target’s settings of course.
In the Build Settings for the
URItarget:- Add your
libsfolder to theLibrary Search Paths:$(SRCROOT)/libs - Make sure
$(PROJECT_TEMP_DIR)/SymlinkLibs/is still included in the search path and that it is above thelibspath you just added. - Copy the
Library Search Paths(just select the row and Cmd+C).
- Add your
In the Build Settings for the
Crypto,CHTTP,HTTP,TLS, andWebSocketstargets:- Overwrite the
Library Search Pathswith the paths stored on your clipboard (Cmd+V).
- Overwrite the
Create a new Single View Application target (I’ll call it
TestiOSbut you do you)In the
Build Settingsfor theTestiOStarget, copy and paste theHeader Search Pathsfrom theSwiftDiscordtarget
* Not just the one we added - grab the ones that were there before too
In the Build Phases for the
TestiOStarget:- Add
SwiftDiscordto Target Dependencies - Add
libcrypto.a,libopus.a,libsodium.a, andlibssl.aunder Link Binary with Libraries. - Create a new Copy Files Build Phase (there’s a + button at the upper left side [under the General tab] that lets you do this).
- Set its destination to
Frameworks - Add everything in the Products folder (should all be .frameworks) except for
TestiOS.app
- Set its destination to
- Add
Replace your
ViewController.swiftcontents with the following code to make sure everything works correctly:import UIKit import SwiftDiscord class ViewController: UIViewController, DiscordClientDelegate { var client: DiscordClient! override func viewDidLoad() { super.viewDidLoad() self.client = DiscordClient(token: "some valid token here", delegate: self, configuration: [.log(.info)]) client.connect() } func client(_ client: DiscordClient, didConnect connected: Bool) { print("Bot connected!") } func client(_ client: DiscordClient, didCreateMessage message: DiscordMessage) { if message.content == "$mycommand" { message.channel?.send("I got your command") } } func client(_ client: DiscordClient, needsDataSourceForEngine engine: DiscordVoiceEngine) throws -> DiscordVoiceDataSource { return try DiscordBufferedVoiceDataSource( opusEncoder: DiscordOpusEncoder(bitrate: 128_000, sampleRate: 48_000, channels: 2) ) } }Build!
- If you have hundreds of warnings, you may want to run a Product > Clean and restart Xcode.
Run!
You did it. Somehow. Congratulations!
Thanks
I’d like to thank the lead developer (nuclearace) for providing guidance throughout the whole journey; without whom I would have undoubtedly never been able to get this working.
Cheers,
Troubleshooting
If you experience issues and you are positive you followed the instructions exactly (no, really, if you miss one step you’ll probably get 400+ warnings and several build errors), I recommend you file an issue first. You can also talk to me on Twitter.
Happy Swifting! (is that a thing? I don’t know. It is now).
Installing SwiftDiscord for iOS Reference