1. Getting Started
Using the THEOplayer iOS SDK is possible through the following steps:
- In the target's General tab, in the Embedded Binaries click (+) and select the THEOplayerSDK.framework
- Import the framework in your view controller
#import <THEOplayerSDK/THEOplayerSDK-Swift.h>
- Declare a player variable and initialize it
THEOplayer *player = [[THEOplayer alloc] initWithConfiguration:nil];
- Position and size your player
[player setBounds:CGRectMake(0, 0, 320, 180)]; CGSize screenBounds = UIScreen.mainScreen.bounds; [player setCenter:CGPointMake(screenBounds.width/2, screenBounds.height/2)];
- Add the player to your controller's view hierarchy
[player addAsSubviewOf:controller.view];
- Set a source to the player
[TypedSource *typedSource = [[TypedSource alloc] initWithSrc:@"https://www.examples.com/index.m3u8" type:@"application/x-mpegurl"];
SourceDescription *source = [[SourceDescription alloc] initWithSources:typedSource ads:NULL textTracks:NULL poster:NULL analytics:NULL metadata:NULL];
player.source = source;
2. Interoperability Swift/Objective-C
Since THEOplayer iOS SDK is a Swift framework, it is possible to integrate it into an Objective-C project. To be accessible and usable in Objective-C a Swift class must be a descendant of an Objective-C class. Except for Swift-only features such as Generics, Non-Int Enumerations, Structures, etc. That's why the complete THEOplayer iOS SDK API is not accessible and usable directly in Objective-C.
However, you can still use the complete THEOplayer API in your Objective-C project through Swift code thanks to the the mix and match feature. From Apple documentation: "Using mix and match, you can implement part of your app’s functionality using the latest Swift features and seamlessly incorporate it back into your existing Objective-C codebase."
Listen to events in an Objective-C app
Adding event listeners is one of the features that are not available in Objective-C code. Here's an example of how you can listen to an event in your Objective-C app through Swift code.
- Create a Swift file that contains a Swift class with a THEOplayer property. This class should be available in Objective-C, so it should extend an Objective-C class, here NSObject.
@objc class SwiftCode : NSObject { var player : THEOplayer init(player : THEOplayer) { self.player = THEOplayer() super.init() } }
- Create a method to add some event listeners.
func addEventListeners() { _ = player.addEventListener(type: PlayerEventTypes.PLAY, listener: handleEvent) _ = player.addEventListener(type: PlayerEventTypes.PAUSE, listener: handleEvent) } func handleEvent (event : CurrentTimeEvent) { // Do something with the event print("Received \(event.type) event") }
- Import the Xcode-generated header file for your Swift code into any Objective-C .m file you want to use your Swift code from.
#import "YourProductName-Swift.h"
- Finally, in your Objective-C code, instanciate the Swift class and its addEventListeners method.
var swiftCode = [[SwiftCode alloc] init]; [self.swiftCode addEventListeners];
You can learn more about Interopability and Swift type compatibility on the Apple documentation.