diff --git a/example/README.md b/example/README.md index efc954f..e728298 100644 --- a/example/README.md +++ b/example/README.md @@ -2,15 +2,81 @@ Demonstrates how to use the mparticle_flutter_sdk plugin. -## Getting Started +## Prerequisites -This project is a starting point for a Flutter application. +- Flutter SDK (stable channel) +- Xcode 16.4 or later (for iOS development) +- CocoaPods +- iOS 13.0+ (minimum deployment target) -A few resources to get you started if this is your first Flutter project: +## Setup + +### 1. Install Flutter + +If Flutter is not installed, follow these steps: + +```bash +# Clone Flutter SDK to your home directory +cd ~ +git clone https://github.com/flutter/flutter.git -b stable + +# Add Flutter to your PATH in ~/.zshrc +echo 'export PATH="$HOME/flutter/bin:$PATH"' >> ~/.zshrc +source ~/.zshrc + +# Verify installation +flutter doctor +``` + +### 2. Install Dependencies + +```bash +# From the example directory +cd example + +# Get Flutter packages +flutter pub get + +# Precache iOS artifacts (first time only) +flutter precache --ios +``` + +### 3. Install iOS Dependencies + +```bash +# Install CocoaPods dependencies +cd ios +pod install +cd .. +``` + +**Note:** The Podfile has been configured with iOS 13.0 as the minimum deployment target to ensure compatibility with Flutter and mParticle dependencies. + +## Running the App + +### iOS Simulator + +```bash +# List available devices +flutter devices + +# Run on iOS simulator +flutter run -d + +# Or simply +flutter run +``` + +### iOS Device + +```bash +# Connect your iOS device and ensure it's in Developer Mode +flutter run -d +``` + +## Resources - [Lab: Write your first Flutter app](https://flutter.dev/docs/get-started/codelab) - [Cookbook: Useful Flutter samples](https://flutter.dev/docs/cookbook) - -For help getting started with Flutter, view our -[online documentation](https://flutter.dev/docs), which offers tutorials, -samples, guidance on mobile development, and a full API reference. +- [Flutter Documentation](https://flutter.dev/docs) +- [mParticle Documentation](https://docs.mparticle.com/) diff --git a/example/ios/Flutter/AppFrameworkInfo.plist b/example/ios/Flutter/AppFrameworkInfo.plist index 7c56964..1dc6cf7 100644 --- a/example/ios/Flutter/AppFrameworkInfo.plist +++ b/example/ios/Flutter/AppFrameworkInfo.plist @@ -21,6 +21,6 @@ CFBundleVersion 1.0 MinimumOSVersion - 12.0 + 13.0 diff --git a/example/ios/Podfile b/example/ios/Podfile index c4372c6..cfc8818 100644 --- a/example/ios/Podfile +++ b/example/ios/Podfile @@ -1,5 +1,5 @@ # Uncomment this line to define a global platform for your project -platform :ios, '12.0' +platform :ios, '13.0' # CocoaPods analytics sends network stats synchronously affecting flutter build latency. ENV['COCOAPODS_DISABLE_STATS'] = 'true' diff --git a/example/ios/Runner.xcodeproj/project.pbxproj b/example/ios/Runner.xcodeproj/project.pbxproj index 18a9d31..ea15ef0 100644 --- a/example/ios/Runner.xcodeproj/project.pbxproj +++ b/example/ios/Runner.xcodeproj/project.pbxproj @@ -343,7 +343,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 12.0; + IPHONEOS_DEPLOYMENT_TARGET = 13.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; SUPPORTED_PLATFORMS = iphoneos; @@ -421,7 +421,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 12.0; + IPHONEOS_DEPLOYMENT_TARGET = 13.0; MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; @@ -471,7 +471,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 12.0; + IPHONEOS_DEPLOYMENT_TARGET = 13.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; SUPPORTED_PLATFORMS = iphoneos; diff --git a/example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme b/example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme index 5e31d3d..9c12df5 100644 --- a/example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme +++ b/example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme @@ -26,6 +26,7 @@ buildConfiguration = "Debug" selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" + customLLDBInitFile = "$(SRCROOT)/Flutter/ephemeral/flutter_lldbinit" shouldUseLaunchSchemeArgsEnv = "YES"> diff --git a/example/ios/Runner/AppDelegate.swift b/example/ios/Runner/AppDelegate.swift index 5d6fa75..083daf3 100644 --- a/example/ios/Runner/AppDelegate.swift +++ b/example/ios/Runner/AppDelegate.swift @@ -2,18 +2,66 @@ import UIKit import Flutter import mParticle_Apple_SDK -@UIApplicationMain -@objc class AppDelegate: FlutterAppDelegate { +@main +@objc class AppDelegate: FlutterAppDelegate, FlutterStreamHandler { + private var eventSink: FlutterEventSink? + + var isInitialized = false + override func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { GeneratedPluginRegistrant.register(with: self) + + // Set up EventChannel for mParticle initialization notifications + let controller = window?.rootViewController as! FlutterViewController + let initializationEventChannel = FlutterEventChannel( + name: "com.example.mparticle_initialization", + binaryMessenger: controller.binaryMessenger + ) + initializationEventChannel.setStreamHandler(self) + + // Subscribe to mParticle initialization notification + NotificationCenter.default.addObserver( + self, + selector: #selector(handleMParticleInitialized), + name: NSNotification.Name(rawValue: "mParticleDidFinishInitializing"), + object: nil + ) - let options = MParticleOptions(key: "api-key", secret: "secret") + let options = MParticleOptions(key: "", secret: "") options.logLevel = MPILogLevel.verbose MParticle.sharedInstance().start(with: options) return super.application(application, didFinishLaunchingWithOptions: launchOptions) } + + // MARK: - FlutterStreamHandler + + func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? { + self.eventSink = events + if isInitialized { + events(["initialized": true]) + } + return nil + } + + func onCancel(withArguments arguments: Any?) -> FlutterError? { + self.eventSink = nil + return nil + } + + // MARK: - mParticle Initialization + + @objc private func handleMParticleInitialized(notification: Notification) { + isInitialized = true + if let eventSink = self.eventSink { + eventSink(["initialized": true]) + } + } + + deinit { + NotificationCenter.default.removeObserver(self) + } } diff --git a/example/lib/main.dart b/example/lib/main.dart index 1403f85..aa4bfeb 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,6 +1,8 @@ import 'dart:async'; +import 'dart:io'; import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:mparticle_flutter_sdk/mparticle_flutter_sdk.dart'; import 'package:mparticle_flutter_sdk/events/event_type.dart'; import 'package:mparticle_flutter_sdk/events/commerce_event.dart'; @@ -35,6 +37,11 @@ final myController = TextEditingController(); class _MyAppState extends State { bool _isInitialized = false; + StreamSubscription? _initializationSubscription; + + // EventChannel for receiving mParticle initialization notifications + static const EventChannel _initializationEventChannel = + EventChannel('com.example.mparticle_initialization'); TextButton buildButton(text, onPressedFunction) { return TextButton( @@ -60,15 +67,38 @@ class _MyAppState extends State { initMparticle(); } + @override + void dispose() { + _initializationSubscription?.cancel(); + super.dispose(); + } + MparticleFlutterSdk? mpInstance; // Platform messages are asynchronous, so we initialize in an async method. Future initMparticle() async { - mpInstance = await MparticleFlutterSdk.getInstance(); - if (mpInstance != null) { - setState(() { - _isInitialized = true; + if (Platform.isIOS) { + // iOS: Subscribe to mParticle initialization events from native code + _initializationSubscription = _initializationEventChannel + .receiveBroadcastStream() + .listen((event) async { + if (event is Map && event['initialized'] == true) { + print("mParticle initialized from native layer"); + mpInstance = await MparticleFlutterSdk.getInstance(); + setState(() { + _isInitialized = true; + }); + } }); + } else { + // Android and other platforms: Use original initialization + mpInstance = await MparticleFlutterSdk.getInstance(); + + if (mpInstance != null) { + setState(() { + _isInitialized = true; + }); + } } } diff --git a/example/pubspec.lock b/example/pubspec.lock index bfd29a1..09117a0 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -21,26 +21,26 @@ packages: dependency: transitive description: name: characters - sha256: "04a925763edad70e8443c99234dc3328f442e811f1d8fd1a72f1c8ad0f69a605" + sha256: f71061c654a3380576a52b451dd5532377954cf9dbd272a78fc8479606670803 url: "https://pub.dev" source: hosted - version: "1.3.0" + version: "1.4.0" clock: dependency: transitive description: name: clock - sha256: cb6d7f03e1de671e34607e909a7213e31d7752be4fb66a86d29fe1eb14bfb5cf + sha256: fddb70d9b5277016c77a80201021d40a2247104d9f4aa7bab7157b7e3f05b84b url: "https://pub.dev" source: hosted - version: "1.1.1" + version: "1.1.2" collection: dependency: transitive description: name: collection - sha256: a1ace0a119f20aabc852d165077c036cd864315bd99b7eaa10a60100341941bf + sha256: "2f5709ae4d3d59dd8f7cd309b4e023046b57d8a6c82130785d2b0e5868084e76" url: "https://pub.dev" source: hosted - version: "1.19.0" + version: "1.19.1" cupertino_icons: dependency: "direct main" description: @@ -53,10 +53,10 @@ packages: dependency: transitive description: name: fake_async - sha256: "511392330127add0b769b75a987850d136345d9227c6b94c96a04cf4a391bf78" + sha256: "5368f224a74523e8d2e7399ea1638b37aecfca824a3cc4dfdf77bf1fa905ac44" url: "https://pub.dev" source: hosted - version: "1.3.1" + version: "1.3.3" file: dependency: transitive description: @@ -99,34 +99,34 @@ packages: dependency: transitive description: name: leak_tracker - sha256: "7bb2830ebd849694d1ec25bf1f44582d6ac531a57a365a803a6034ff751d2d06" + sha256: "33e2e26bdd85a0112ec15400c8cbffea70d0f9c3407491f672a2fad47915e2de" url: "https://pub.dev" source: hosted - version: "10.0.7" + version: "11.0.2" leak_tracker_flutter_testing: dependency: transitive description: name: leak_tracker_flutter_testing - sha256: "9491a714cca3667b60b5c420da8217e6de0d1ba7a5ec322fab01758f6998f379" + sha256: "1dbc140bb5a23c75ea9c4811222756104fbcd1a27173f0c34ca01e16bea473c1" url: "https://pub.dev" source: hosted - version: "3.0.8" + version: "3.0.10" leak_tracker_testing: dependency: transitive description: name: leak_tracker_testing - sha256: "6ba465d5d76e67ddf503e1161d1f4a6bc42306f9d66ca1e8f079a47290fb06d3" + sha256: "8d5a2d49f4a66b49744b23b018848400d23e54caf9463f4eb20df3eb8acb2eb1" url: "https://pub.dev" source: hosted - version: "3.0.1" + version: "3.0.2" matcher: dependency: transitive description: name: matcher - sha256: d2323aa2060500f906aa31a895b4030b6da3ebdcc5619d14ce1aada65cd161cb + sha256: dc58c723c3c24bf8d3e2d3ad3f2f9d7bd9cf43ec6feaa64181775e60190153f2 url: "https://pub.dev" source: hosted - version: "0.12.16+1" + version: "0.12.17" material_color_utilities: dependency: transitive description: @@ -139,10 +139,10 @@ packages: dependency: transitive description: name: meta - sha256: bdb68674043280c3428e9ec998512fb681678676b3c54e773629ffe74419f8c7 + sha256: "23f08335362185a5ea2ad3a4e597f1375e78bce8a040df5c600c8d3552ef2394" url: "https://pub.dev" source: hosted - version: "1.15.0" + version: "1.17.0" mparticle_flutter_sdk: dependency: "direct main" description: @@ -154,10 +154,10 @@ packages: dependency: transitive description: name: path - sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af" + sha256: "75cca69d1490965be98c73ceaea117e8a04dd21217b37b292c9ddbec0d955bc5" url: "https://pub.dev" source: hosted - version: "1.9.0" + version: "1.9.1" platform: dependency: transitive description: @@ -191,18 +191,18 @@ packages: dependency: transitive description: name: stack_trace - sha256: "9f47fd3630d76be3ab26f0ee06d213679aa425996925ff3feffdec504931c377" + sha256: "8b27215b45d22309b5cddda1aa2b19bdfec9df0e765f2de506401c071d38d1b1" url: "https://pub.dev" source: hosted - version: "1.12.0" + version: "1.12.1" stream_channel: dependency: transitive description: name: stream_channel - sha256: ba2aa5d8cc609d96bbb2899c28934f9e1af5cddbd60a827822ea467161eb54e7 + sha256: "969e04c80b8bcdf826f8f16579c7b14d780458bd97f56d107d3950fdbeef059d" url: "https://pub.dev" source: hosted - version: "2.1.2" + version: "2.1.4" string_scanner: dependency: transitive description: @@ -231,18 +231,18 @@ packages: dependency: transitive description: name: test_api - sha256: "664d3a9a64782fcdeb83ce9c6b39e78fd2971d4e37827b9b06c3aa1edc5e760c" + sha256: ab2726c1a94d3176a45960b6234466ec367179b87dd74f1611adb1f3b5fb9d55 url: "https://pub.dev" source: hosted - version: "0.7.3" + version: "0.7.7" vector_math: dependency: transitive description: name: vector_math - sha256: "80b3257d1492ce4d091729e3a67a60407d227c27241d6927be0130c98e741803" + sha256: d530bd74fea330e6e364cda7a85019c434070188383e1cd8d9777ee586914c5b url: "https://pub.dev" source: hosted - version: "2.1.4" + version: "2.2.0" vm_service: dependency: transitive description: @@ -260,5 +260,5 @@ packages: source: hosted version: "3.0.4" sdks: - dart: ">=3.4.0 <4.0.0" + dart: ">=3.8.0-0 <4.0.0" flutter: ">=3.18.0-18.0.pre.54"