SwiftSoup is a Swift port of the popular Java library jsoup for parsing HTML. There are three main ways to add SwiftSoup to your Swift project: Swift Package Manager (recommended), CocoaPods, or Carthage.
Swift Package Manager (Recommended)
Swift Package Manager is the modern, Apple-recommended way to manage dependencies in Swift projects.
Using Xcode Interface
- Open your project in Xcode
- Go to File → Add Package Dependencies...
- Enter the SwiftSoup repository URL:
https://github.com/scinfu/SwiftSoup.git
- Select the version rule (usually "Up to Next Major Version")
- Click Add Package
- Select your target and click Add Package
Using Package.swift
For Swift packages, add SwiftSoup to your Package.swift
file:
// swift-tools-version:5.5
import PackageDescription
let package = Package(
name: "YourProject",
platforms: [
.iOS(.v12),
.macOS(.v10_15),
.tvOS(.v12),
.watchOS(.v5)
],
dependencies: [
.package(url: "https://github.com/scinfu/SwiftSoup.git", from: "2.6.0")
],
targets: [
.target(
name: "YourProject",
dependencies: ["SwiftSoup"]
),
.testTarget(
name: "YourProjectTests",
dependencies: ["YourProject", "SwiftSoup"]
)
]
)
Then run:
swift package update
CocoaPods
Installation
- Install CocoaPods if you haven't already:
sudo gem install cocoapods
- Navigate to your project directory and initialize CocoaPods:
cd YourProjectDirectory
pod init
- Open the generated
Podfile
and add SwiftSoup:
platform :ios, '12.0'
use_frameworks!
target 'YourProjectName' do
pod 'SwiftSoup', '~> 2.6'
end
- Install the dependency:
pod install
- Important: Always open the
.xcworkspace
file (not.xcodeproj
) in Xcode after runningpod install
.
Carthage
Installation
- Install Carthage using Homebrew:
brew install carthage
Create a
Cartfile
in your project root:github "scinfu/SwiftSoup" ~> 2.6
Build the framework:
carthage update --platform iOS --use-xcframeworks
- In Xcode, go to your target's General tab
- Drag
SwiftSoup.xcframework
fromCarthage/Build/
to Frameworks, Libraries, and Embedded Content
Verification and Basic Usage
After installation, import SwiftSoup in your Swift files:
import SwiftSoup
Test the installation with a simple example:
import SwiftSoup
func testSwiftSoup() {
let html = "<html><head><title>Test</title></head><body><p>Hello World</p></body></html>"
do {
let doc: Document = try SwiftSoup.parse(html)
let title = try doc.title()
let paragraph = try doc.select("p").first()?.text()
print("Title: \(title)") // Output: Title: Test
print("Paragraph: \(paragraph)") // Output: Paragraph: Hello World
} catch {
print("Error: \(error)")
}
}
Platform Support
SwiftSoup supports: - iOS 12.0+ - macOS 10.15+ - tvOS 12.0+ - watchOS 5.0+ - Linux (Swift 5.5+)
Choosing the Right Method
- Swift Package Manager: Best for new projects and iOS 13+/macOS 10.15+ targets
- CocoaPods: Good for projects already using CocoaPods or needing iOS 11 support
- Carthage: Suitable for projects preferring decentralized dependency management
After successful installation, you can start parsing HTML, extracting data, and manipulating DOM elements using SwiftSoup's comprehensive API.