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.swift
so 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 update
Build SwiftDiscord:
swift build -Xlinker -L/usr/local/lib -Xlinker -lopus -Xcc -I/usr/local/include
Now comes the hardest part:
mkdir libs
Manually compile & build the following projects:
I hope you like reading READMEs.
Move the resulting compiled libraries (.a files) into your new
libs
directory. You should have the following files in there:libcrypto.a
libopus.a
libsodium.a
libssl.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
libs
with 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-iOS
In the Build Settings for the
SwiftDiscord
target:- Add the following to
Valid Architectures
:arm64
armv7
armv7s
- Add your
libs
folder to theLibrary Search Paths
:$(SRCROOT)/libs
- You may need to switch from
Basic
toAll
Build Settings in order to see this key.
- You may need to switch from
- Also add
/usr/local/lib
to yourLibrary Search Paths
, after your ownlibs
folder. - Add
/usr/local/include
to yourHeader Search Paths
.
- Add the following to
In the Build Settings for the
DiscordOpus
target:- Repeat steps 2, 3, and 4 from the previous target, but for this target’s settings of course.
In the Build Settings for the
URI
target:- Add your
libs
folder to theLibrary Search Paths
:$(SRCROOT)/libs
- Make sure
$(PROJECT_TEMP_DIR)/SymlinkLibs/
is still included in the search path and that it is above thelibs
path 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
, andWebSockets
targets:- Overwrite the
Library Search Paths
with the paths stored on your clipboard (Cmd+V).
- Overwrite the
Create a new Single View Application target (I’ll call it
TestiOS
but you do you)In the
Build Settings
for theTestiOS
target, copy and paste theHeader Search Paths
from theSwiftDiscord
target
* Not just the one we added - grab the ones that were there before too
In the Build Phases for the
TestiOS
target:- Add
SwiftDiscord
to Target Dependencies - Add
libcrypto.a
,libopus.a
,libsodium.a
, andlibssl.a
under 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.swift
contents 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).