Project Setup
Install dependencies
CocoaPods
To install dependencies into your Xcode project using CocoaPods, specify it in your Podfile:
target 'TargetName' do
# Uncomment the next line if you're using Swift
# use_frameworks!
pod 'SwarmCloudKit', '~>3.3'
end
Then, run the following command:
$ pod install
Carthage
Add the dependencies to the Cartfile:
binary "https://web3lab.b-cdn.net/apple/SwarmCloudKit.json" ~> 3.2.5
binary "https://web3lab.b-cdn.net/apple/Datachannel.json" ~> 1.0.2113
Then run:
carthage update --use-xcframeworks --cache-builds
This will build each one or download a pre-compiled XCFramework.
On your application targets’ General settings tab, in the Frameworks, Libraries, and Embedded Content section, drag and drop each XCFramework you want to use from the ***Carthage/Build*** folder.
Swift Package Manager
Once you have your Swift package set up, adding Alamofire as a dependency is as easy as adding it to the dependencies value of your Package.swift.
dependencies: [
.package(url: "https://github.com/swarm-cloud/SwarmCloudKit.git", branch: "main")
]
Manually
Download Pre-compiled XCFramework
Copy the framework
Unzip files, then drag and drop SwarmCloudKit.xcframework and datachannel_wrapper.xcframework into your Xcode project on your application targets’ General settings tab, in the Frameworks, Libraries, and Embedded Content section.
Integration
Include
Import SwarmCloudKit in AppDelegate.m:
- swift
- objective-c
import SwarmCloudKit
#import <SwarmCloudKit/SwarmCloudKit-Swift.h>
Initialize P2pEngine
Initialize P2pEngine in AppDelegate.m:
- swift
- objective-c
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let config = P2pConfig(
trackerZone: .Europe // Set HongKong or USA if you changed zone
)
P2pEngine.setup(token: YOUR_TOKEN, config: config) // replace with your own token
return true
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
P2pConfig* config = [P2pConfig defaultConfiguration];
config.trackerZone = TrackerZoneEurope; // Set HongKong or USA if you changed zone
[P2pEngine setupWithToken:YOUR_TOKEN config:config]; // replace with your own token
return YES;
}
Where YOUR_TOKEN is your Customer ID. Please replace it by your own token obtained from console, click here for more information.
Usage
When initializing an AVPlayer (or any other video player) instance, before passing it a URL, pass that URL through P2P Engine:
- swift
- objective-c
let parsedUrl = P2pEngine.shared.parseStreamUrl("https://your_stream.m3u8")
_player = AVPlayer.init(url: URL(string: parsedUrl)!)
NSString *parsedUrl = [[P2pEngine sharedInstance] parseStreamUrl:@"https://your_stream.m3u8"];
_player = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:parsedUrl]];
That’s it! SDK should now be integrated into your app.
For macOS development:
Xcode enables sandboxing by default on new applications. Ensure your application's entitlements allows incoming and outgoing connections by marking the respective checkboxes in the Network section.
Demo
A completed example can be found here
Troubleshooting Steps when P2P doesn't work
Click here