diff --git a/.gitignore b/.gitignore index e35be3a7..9ebf1980 100644 --- a/.gitignore +++ b/.gitignore @@ -61,3 +61,11 @@ example/lib/firebase_options.dart example/macos/Runner/GoogleService-Info.plist example/firebase.json + +# Swift Package Manager (plugin packages only - an app's Package.resolved should be committed) +ios/twilio_voice/.build/ +ios/twilio_voice/.swiftpm/ +ios/twilio_voice/Package.resolved +macos/twilio_voice/.build/ +macos/twilio_voice/.swiftpm/ +macos/twilio_voice/Package.resolved diff --git a/CHANGELOG.md b/CHANGELOG.md index 35ad773b..df81da8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## Next Release +* feat: [iOS, macOS] Swift Package Manager support added, enable it with `flutter config --enable-swift-package-manager`. _Note: projects using CocoaPods will continue to use CocoaPods, and SPM is only used for new projects or those that have opted in._ * feat: added call quality events for all platforms (see `CallQualityEvent` and `TwilioVoicePlatform.instance.call.qualityWarnings`, and [Twilio Call Quality Metrics](https://www.twilio.com/docs/voice/voice-insights/api/call/details-sdk-call-quality-events#error-and-warning-events)) * Updated Example * Updated docs diff --git a/README.md b/README.md index 8021a62b..3c835251 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,36 @@ Please follow Twilio's quickstart setup for each platform, you don't need to wri but it will help you understand the basic functionality of setting up your server, registering your iOS app for VOIP, etc. +### Swift Package Manager (iOS & macOS) + +This plugin ships both a Swift package and a podspec, so it works with either integration. If your +app uses CocoaPods there is nothing to do - it keeps working exactly as before. + +To use Swift Package Manager instead, enable it once for your Flutter install: + +```bash +flutter config --enable-swift-package-manager +``` + +Flutter then resolves this plugin through SPM, and the iOS Twilio Voice SDK is pulled from +[twilio/twilio-voice-ios](https://github.com/twilio/twilio-voice-ios) (6.13.6+) instead of the +`TwilioVoice` pod. `TwilioVoice.framework` is embedded into your app automatically; no manual +"Frameworks, Libraries and Embedded Content" step is needed. + +Two things to be aware of: + +- **macOS apps must set their deployment target to 11.0 or later.** CocoaPods tolerates an app + target below a pod's minimum, but SwiftPM does not, and the build fails with: + + > error: The package product 'twilio-voice' requires minimum platform version 11.0 for the macOS + > platform, but this target supports 10.15 + + Set `macOS Deployment Target` to `11.0` on the Runner target in Xcode (Flutter's own template + still defaults to 10.15). iOS already requires 13.0, which matches Flutter's template default. + +- Enabling SPM is a per-machine Flutter setting, not a per-project one. Plugins that do not yet + support SPM continue to be resolved through CocoaPods alongside it, so mixed projects are fine. + ### iOS Setup To customize the icon displayed on a CallKit call, Open XCode and add a png icon named ' diff --git a/ios/Classes/TwilioVoicePlugin.h b/ios/Classes/TwilioVoicePlugin.h deleted file mode 100644 index ed05e301..00000000 --- a/ios/Classes/TwilioVoicePlugin.h +++ /dev/null @@ -1,4 +0,0 @@ -#import - -@interface TwilioVoicePlugin : NSObject -@end diff --git a/ios/Classes/TwilioVoicePlugin.m b/ios/Classes/TwilioVoicePlugin.m deleted file mode 100644 index 3a9abfdc..00000000 --- a/ios/Classes/TwilioVoicePlugin.m +++ /dev/null @@ -1,15 +0,0 @@ -#import "TwilioVoicePlugin.h" -#if __has_include() -#import -#else -// Support project import fallback if the generated compatibility header -// is not copied when this plugin is created as a library. -// https://forums.swift.org/t/swift-static-libraries-dont-copy-generated-objective-c-header/19816 -#import "twilio_voice-Swift.h" -#endif - -@implementation TwilioVoicePlugin -+ (void)registerWithRegistrar:(NSObject*)registrar { - [SwiftTwilioVoicePlugin registerWithRegistrar:registrar]; -} -@end diff --git a/ios/twilio_voice.podspec b/ios/twilio_voice.podspec index 9bb8c1d3..bbbeb56e 100644 --- a/ios/twilio_voice.podspec +++ b/ios/twilio_voice.podspec @@ -13,7 +13,7 @@ Pod::Spec.new do |s| s.license = { :file => '../LICENSE' } s.author = { 'Charles Dyason' => 'charles@earthbase.io' } s.source = { :path => '.' } - s.source_files = 'Classes/**/*' + s.source_files = 'twilio_voice/Sources/twilio_voice/**/*.swift' s.dependency 'Flutter' s.dependency 'TwilioVoice','~> 6.13.6' s.platform = :ios, '13.0' diff --git a/ios/twilio_voice/Package.swift b/ios/twilio_voice/Package.swift new file mode 100644 index 00000000..5151cef4 --- /dev/null +++ b/ios/twilio_voice/Package.swift @@ -0,0 +1,29 @@ +// swift-tools-version: 5.9 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "twilio_voice", + platforms: [ + .iOS("13.0") + ], + products: [ + .library(name: "twilio-voice", targets: ["twilio_voice"]) + ], + dependencies: [ + // Mirrors the `TwilioVoice` CocoaPods dependency in `ios/twilio_voice.podspec`. Keep the two + // version constraints in step so an app gets the same SDK whichever integration it uses. + .package(url: "https://github.com/twilio/twilio-voice-ios", from: "6.13.6") + ], + targets: [ + .target( + name: "twilio_voice", + dependencies: [ + // The dynamic variant, matching what the CocoaPods integration vendors. Twilio also + // publishes `TwilioVoice-static`, which additionally requires SystemConfiguration. + .product(name: "TwilioVoice", package: "twilio-voice-ios") + ] + ) + ] +) diff --git a/ios/Classes/SwiftTwilioVoicePlugin.swift b/ios/twilio_voice/Sources/twilio_voice/SwiftTwilioVoicePlugin.swift similarity index 99% rename from ios/Classes/SwiftTwilioVoicePlugin.swift rename to ios/twilio_voice/Sources/twilio_voice/SwiftTwilioVoicePlugin.swift index 7f74bd5d..175cabec 100644 --- a/ios/Classes/SwiftTwilioVoicePlugin.swift +++ b/ios/twilio_voice/Sources/twilio_voice/SwiftTwilioVoicePlugin.swift @@ -6,6 +6,14 @@ import TwilioVoice import CallKit import UserNotifications +/// Exposed to Objective-C as `TwilioVoicePlugin`, which is the class name Flutter's generated +/// plugin registrant calls into (`pluginClass` in `pubspec.yaml`). +/// +/// This used to be a separate Objective-C shim (`TwilioVoicePlugin.h/.m`) that forwarded to this +/// class. Swift Package Manager does not support mixed-language targets, so the shim was removed +/// and its Objective-C name adopted here instead - keeping the registrant, and any manual +/// `[TwilioVoicePlugin registerWithRegistrar:]` call, working unchanged. +@objc(TwilioVoicePlugin) public class SwiftTwilioVoicePlugin: NSObject, FlutterPlugin, FlutterStreamHandler, PKPushRegistryDelegate, NotificationDelegate, CallDelegate, AVAudioPlayerDelegate, CXProviderDelegate { let callObserver = CXCallObserver() diff --git a/macos/twilio_voice.podspec b/macos/twilio_voice.podspec index f3bca7a8..dcfb7d58 100644 --- a/macos/twilio_voice.podspec +++ b/macos/twilio_voice.podspec @@ -14,11 +14,11 @@ A new Flutter plugin project. s.author = { 'Your Company' => 'email@example.com' } s.source = { :path => '.' } - s.source_files = 'Classes/**/*' + s.source_files = 'twilio_voice/Sources/twilio_voice/**/*.swift' s.dependency 'FlutterMacOS' s.platform = :osx, '11.0' s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES' } s.swift_version = '5.0' - s.resources = "Resources/*" + s.resources = 'twilio_voice/Sources/twilio_voice/Resources/*' end diff --git a/macos/twilio_voice/Package.swift b/macos/twilio_voice/Package.swift new file mode 100644 index 00000000..54b9bc9c --- /dev/null +++ b/macos/twilio_voice/Package.swift @@ -0,0 +1,26 @@ +// swift-tools-version: 5.9 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "twilio_voice", + platforms: [ + .macOS("11.0") + ], + products: [ + .library(name: "twilio-voice", targets: ["twilio_voice"]) + ], + dependencies: [], + targets: [ + .target( + name: "twilio_voice", + dependencies: [], + resources: [ + // `index.html` is the page hosted by the plugin's WKWebView. `.process` flattens it into + // the generated resource bundle, so it is looked up via `Bundle.module` - see TVWebView. + .process("Resources") + ] + ) + ] +) diff --git a/macos/Classes/Constants/Constants.swift b/macos/twilio_voice/Sources/twilio_voice/Constants/Constants.swift similarity index 100% rename from macos/Classes/Constants/Constants.swift rename to macos/twilio_voice/Sources/twilio_voice/Constants/Constants.swift diff --git a/macos/Classes/Constants/FlutterErrorCodes.swift b/macos/twilio_voice/Sources/twilio_voice/Constants/FlutterErrorCodes.swift similarity index 100% rename from macos/Classes/Constants/FlutterErrorCodes.swift rename to macos/twilio_voice/Sources/twilio_voice/Constants/FlutterErrorCodes.swift diff --git a/macos/Classes/JsInterop/Call/TVCall.swift b/macos/twilio_voice/Sources/twilio_voice/JsInterop/Call/TVCall.swift similarity index 100% rename from macos/Classes/JsInterop/Call/TVCall.swift rename to macos/twilio_voice/Sources/twilio_voice/JsInterop/Call/TVCall.swift diff --git a/macos/Classes/JsInterop/Call/TVCallDelegate.swift b/macos/twilio_voice/Sources/twilio_voice/JsInterop/Call/TVCallDelegate.swift similarity index 100% rename from macos/Classes/JsInterop/Call/TVCallDelegate.swift rename to macos/twilio_voice/Sources/twilio_voice/JsInterop/Call/TVCallDelegate.swift diff --git a/macos/Classes/JsInterop/Call/TVCallParams.swift b/macos/twilio_voice/Sources/twilio_voice/JsInterop/Call/TVCallParams.swift similarity index 100% rename from macos/Classes/JsInterop/Call/TVCallParams.swift rename to macos/twilio_voice/Sources/twilio_voice/JsInterop/Call/TVCallParams.swift diff --git a/macos/Classes/JsInterop/Call/Types/CallDirection.swift b/macos/twilio_voice/Sources/twilio_voice/JsInterop/Call/Types/CallDirection.swift similarity index 100% rename from macos/Classes/JsInterop/Call/Types/CallDirection.swift rename to macos/twilio_voice/Sources/twilio_voice/JsInterop/Call/Types/CallDirection.swift diff --git a/macos/Classes/JsInterop/Call/Types/TVCallEvent.swift b/macos/twilio_voice/Sources/twilio_voice/JsInterop/Call/Types/TVCallEvent.swift similarity index 100% rename from macos/Classes/JsInterop/Call/Types/TVCallEvent.swift rename to macos/twilio_voice/Sources/twilio_voice/JsInterop/Call/Types/TVCallEvent.swift diff --git a/macos/Classes/JsInterop/Call/Types/TVCallStatus.swift b/macos/twilio_voice/Sources/twilio_voice/JsInterop/Call/Types/TVCallStatus.swift similarity index 100% rename from macos/Classes/JsInterop/Call/Types/TVCallStatus.swift rename to macos/twilio_voice/Sources/twilio_voice/JsInterop/Call/Types/TVCallStatus.swift diff --git a/macos/Classes/JsInterop/Core/JSLoader.swift b/macos/twilio_voice/Sources/twilio_voice/JsInterop/Core/JSLoader.swift similarity index 100% rename from macos/Classes/JsInterop/Core/JSLoader.swift rename to macos/twilio_voice/Sources/twilio_voice/JsInterop/Core/JSLoader.swift diff --git a/macos/Classes/JsInterop/Device/TVDevice.swift b/macos/twilio_voice/Sources/twilio_voice/JsInterop/Device/TVDevice.swift similarity index 100% rename from macos/Classes/JsInterop/Device/TVDevice.swift rename to macos/twilio_voice/Sources/twilio_voice/JsInterop/Device/TVDevice.swift diff --git a/macos/Classes/JsInterop/Device/TVDeviceConnectOptions.swift b/macos/twilio_voice/Sources/twilio_voice/JsInterop/Device/TVDeviceConnectOptions.swift similarity index 100% rename from macos/Classes/JsInterop/Device/TVDeviceConnectOptions.swift rename to macos/twilio_voice/Sources/twilio_voice/JsInterop/Device/TVDeviceConnectOptions.swift diff --git a/macos/Classes/JsInterop/Device/TVDeviceDelegate.swift b/macos/twilio_voice/Sources/twilio_voice/JsInterop/Device/TVDeviceDelegate.swift similarity index 100% rename from macos/Classes/JsInterop/Device/TVDeviceDelegate.swift rename to macos/twilio_voice/Sources/twilio_voice/JsInterop/Device/TVDeviceDelegate.swift diff --git a/macos/Classes/JsInterop/Device/TVDeviceInitOptions.swift b/macos/twilio_voice/Sources/twilio_voice/JsInterop/Device/TVDeviceInitOptions.swift similarity index 100% rename from macos/Classes/JsInterop/Device/TVDeviceInitOptions.swift rename to macos/twilio_voice/Sources/twilio_voice/JsInterop/Device/TVDeviceInitOptions.swift diff --git a/macos/Classes/JsInterop/Device/Types/TVDeviceEvent.swift b/macos/twilio_voice/Sources/twilio_voice/JsInterop/Device/Types/TVDeviceEvent.swift similarity index 100% rename from macos/Classes/JsInterop/Device/Types/TVDeviceEvent.swift rename to macos/twilio_voice/Sources/twilio_voice/JsInterop/Device/Types/TVDeviceEvent.swift diff --git a/macos/Classes/JsInterop/Device/Types/TVError.swift b/macos/twilio_voice/Sources/twilio_voice/JsInterop/Device/Types/TVError.swift similarity index 100% rename from macos/Classes/JsInterop/Device/Types/TVError.swift rename to macos/twilio_voice/Sources/twilio_voice/JsInterop/Device/Types/TVError.swift diff --git a/macos/Resources/index.html b/macos/twilio_voice/Sources/twilio_voice/Resources/index.html similarity index 100% rename from macos/Resources/index.html rename to macos/twilio_voice/Sources/twilio_voice/Resources/index.html diff --git a/macos/Classes/TVWebview/ScriptHandlers/LoggingMessageHandler.swift b/macos/twilio_voice/Sources/twilio_voice/TVWebview/ScriptHandlers/LoggingMessageHandler.swift similarity index 100% rename from macos/Classes/TVWebview/ScriptHandlers/LoggingMessageHandler.swift rename to macos/twilio_voice/Sources/twilio_voice/TVWebview/ScriptHandlers/LoggingMessageHandler.swift diff --git a/macos/Classes/TVWebview/TVWebView.swift b/macos/twilio_voice/Sources/twilio_voice/TVWebview/TVWebView.swift similarity index 93% rename from macos/Classes/TVWebview/TVWebView.swift rename to macos/twilio_voice/Sources/twilio_voice/TVWebview/TVWebView.swift index d55dfe39..584c7ecd 100644 --- a/macos/Classes/TVWebview/TVWebView.swift +++ b/macos/twilio_voice/Sources/twilio_voice/TVWebview/TVWebView.swift @@ -14,8 +14,7 @@ public class TVWebView: WKWebView, WKUIDelegate { overrideLogging() injectTwilioVoiceSDK() - let bundle = Bundle(for: TwilioVoicePlugin.self) - if let url = bundle.url(forResource: "Resources/index", withExtension: "html") { + if let url = TVWebView.indexPageURL() { loadFileURL(url, allowingReadAccessTo: url.deletingLastPathComponent()) } else { NSLog(""" @@ -28,6 +27,20 @@ public class TVWebView: WKWebView, WKUIDelegate { } } + /// Locates `index.html`, the page this webview hosts. + /// + /// Where it lives depends on how the plugin was integrated. Under Swift Package Manager the + /// file is declared as a package resource and lands in the package's generated resource + /// bundle, reachable only through `Bundle.module`. Under CocoaPods it is copied into the + /// plugin framework's own `Resources` directory instead. + private static func indexPageURL() -> URL? { + #if SWIFT_PACKAGE + return Bundle.module.url(forResource: "index", withExtension: "html") + #else + return Bundle(for: TwilioVoicePlugin.self).url(forResource: "Resources/index", withExtension: "html") + #endif + } + /// Asset path of the bundled Twilio Voice JS SDK, as declared in the plugin's `pubspec.yaml`. private static let sdkAsset = "assets/twilio.min.js" private static let sdkPackage = "twilio_voice" diff --git a/macos/Classes/TwilioVoiceChannelMethods.swift b/macos/twilio_voice/Sources/twilio_voice/TwilioVoiceChannelMethods.swift similarity index 100% rename from macos/Classes/TwilioVoiceChannelMethods.swift rename to macos/twilio_voice/Sources/twilio_voice/TwilioVoiceChannelMethods.swift diff --git a/macos/Classes/TwilioVoicePlugin.swift b/macos/twilio_voice/Sources/twilio_voice/TwilioVoicePlugin.swift similarity index 100% rename from macos/Classes/TwilioVoicePlugin.swift rename to macos/twilio_voice/Sources/twilio_voice/TwilioVoicePlugin.swift diff --git a/macos/Classes/Types/Disposable.swift b/macos/twilio_voice/Sources/twilio_voice/Types/Disposable.swift similarity index 100% rename from macos/Classes/Types/Disposable.swift rename to macos/twilio_voice/Sources/twilio_voice/Types/Disposable.swift diff --git a/macos/Classes/Types/JSONArgumentSerializer.swift b/macos/twilio_voice/Sources/twilio_voice/Types/JSONArgumentSerializer.swift similarity index 100% rename from macos/Classes/Types/JSONArgumentSerializer.swift rename to macos/twilio_voice/Sources/twilio_voice/Types/JSONArgumentSerializer.swift diff --git a/macos/Classes/Types/JSObject.swift b/macos/twilio_voice/Sources/twilio_voice/Types/JSObject.swift similarity index 100% rename from macos/Classes/Types/JSObject.swift rename to macos/twilio_voice/Sources/twilio_voice/Types/JSObject.swift diff --git a/macos/Classes/Types/OnCompletionHandler.swift b/macos/twilio_voice/Sources/twilio_voice/Types/OnCompletionHandler.swift similarity index 100% rename from macos/Classes/Types/OnCompletionHandler.swift rename to macos/twilio_voice/Sources/twilio_voice/Types/OnCompletionHandler.swift diff --git a/macos/Classes/Types/TVScriptMessage.swift b/macos/twilio_voice/Sources/twilio_voice/Types/TVScriptMessage.swift similarity index 100% rename from macos/Classes/Types/TVScriptMessage.swift rename to macos/twilio_voice/Sources/twilio_voice/Types/TVScriptMessage.swift