diff --git a/platform_channels/ios/.gitignore b/platform_channels/ios/.gitignore index e96ef602b..277275782 100644 --- a/platform_channels/ios/.gitignore +++ b/platform_channels/ios/.gitignore @@ -21,6 +21,7 @@ Flutter/Generated.xcconfig Flutter/app.flx Flutter/app.zip Flutter/flutter_assets/ +Flutter/.last_build_id Flutter/flutter_export_environment.sh ServiceDefinitions.json Runner/GeneratedPluginRegistrant.* diff --git a/platform_channels/ios/Flutter/.last_build_id b/platform_channels/ios/Flutter/.last_build_id deleted file mode 100644 index a23b6bae7..000000000 --- a/platform_channels/ios/Flutter/.last_build_id +++ /dev/null @@ -1 +0,0 @@ -dad366ac47fa5a1d30c8533697937d44 \ No newline at end of file diff --git a/platform_channels/ios/Runner.xcodeproj/project.pbxproj b/platform_channels/ios/Runner.xcodeproj/project.pbxproj index dccd2beb9..8bbe666ca 100644 --- a/platform_channels/ios/Runner.xcodeproj/project.pbxproj +++ b/platform_channels/ios/Runner.xcodeproj/project.pbxproj @@ -3,11 +3,12 @@ archiveVersion = 1; classes = { }; - objectVersion = 46; + objectVersion = 50; objects = { /* Begin PBXBuildFile section */ 1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */ = {isa = PBXBuildFile; fileRef = 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */; }; + 1C7DB94C2561121200101D94 /* AccelerometerStreamHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C7DB94B2561121200101D94 /* AccelerometerStreamHandler.swift */; }; 3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; }; 74858FAF1ED2DC5600515810 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74858FAE1ED2DC5600515810 /* AppDelegate.swift */; }; 97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; }; @@ -31,6 +32,7 @@ /* Begin PBXFileReference section */ 1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeneratedPluginRegistrant.h; sourceTree = ""; }; 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratedPluginRegistrant.m; sourceTree = ""; }; + 1C7DB94B2561121200101D94 /* AccelerometerStreamHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccelerometerStreamHandler.swift; sourceTree = ""; }; 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = ""; }; 74858FAD1ED2DC5600515810 /* Runner-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Runner-Bridging-Header.h"; sourceTree = ""; }; 74858FAE1ED2DC5600515810 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; @@ -95,6 +97,7 @@ 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */, 74858FAE1ED2DC5600515810 /* AppDelegate.swift */, 74858FAD1ED2DC5600515810 /* Runner-Bridging-Header.h */, + 1C7DB94B2561121200101D94 /* AccelerometerStreamHandler.swift */, ); path = Runner; sourceTree = ""; @@ -214,6 +217,7 @@ files = ( 74858FAF1ED2DC5600515810 /* AppDelegate.swift in Sources */, 1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */, + 1C7DB94C2561121200101D94 /* AccelerometerStreamHandler.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -302,7 +306,10 @@ "$(PROJECT_DIR)/Flutter", ); INFOPLIST_FILE = Runner/Info.plist; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); LIBRARY_SEARCH_PATHS = ( "$(inherited)", "$(PROJECT_DIR)/Flutter", @@ -415,7 +422,8 @@ MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; SUPPORTED_PLATFORMS = iphoneos; - SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OPTIMIZATION_LEVEL = "-O"; TARGETED_DEVICE_FAMILY = "1,2"; VALIDATE_PRODUCT = YES; }; @@ -434,7 +442,10 @@ "$(PROJECT_DIR)/Flutter", ); INFOPLIST_FILE = Runner/Info.plist; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); LIBRARY_SEARCH_PATHS = ( "$(inherited)", "$(PROJECT_DIR)/Flutter", @@ -461,7 +472,10 @@ "$(PROJECT_DIR)/Flutter", ); INFOPLIST_FILE = Runner/Info.plist; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); LIBRARY_SEARCH_PATHS = ( "$(inherited)", "$(PROJECT_DIR)/Flutter", diff --git a/platform_channels/ios/Runner/AccelerometerStreamHandler.swift b/platform_channels/ios/Runner/AccelerometerStreamHandler.swift new file mode 100644 index 000000000..dd8a5bd29 --- /dev/null +++ b/platform_channels/ios/Runner/AccelerometerStreamHandler.swift @@ -0,0 +1,38 @@ +// Copyright 2020 The Flutter team. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import CoreMotion + +class AccelerometerStreamHandler: NSObject, FlutterStreamHandler { + + var motionManger: CMMotionManager; + + override init() { + motionManger = CMMotionManager() + } + + func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? { + + if !motionManger.isAccelerometerAvailable { + events(FlutterError(code: "SENSOR_UNAVAILABLE", message: "Accelerometer is not available", details: nil)) + } + + motionManger.accelerometerUpdateInterval = 0.1 + + motionManger.startAccelerometerUpdates(to: OperationQueue.main) {(data, error) in + guard let accelerationData = data?.acceleration else { + events(FlutterError(code: "DATA_UNAVAILABLE", message: "Cannot get accelerometer data", details: nil )) + return + } + + events([accelerationData.x, accelerationData.y, accelerationData.z]) + } + + return nil + } + + func onCancel(withArguments arguments: Any?) -> FlutterError? { + return nil + } +} diff --git a/platform_channels/ios/Runner/AppDelegate.swift b/platform_channels/ios/Runner/AppDelegate.swift index 0837cb9f7..71b430d64 100644 --- a/platform_channels/ios/Runner/AppDelegate.swift +++ b/platform_channels/ios/Runner/AppDelegate.swift @@ -1,3 +1,7 @@ +// Copyright 2020 The Flutter team. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + import UIKit import Flutter @@ -39,6 +43,8 @@ import Flutter } } + FlutterEventChannel(name: "eventChannelDemo", binaryMessenger: flutterViewController.binaryMessenger).setStreamHandler(AccelerometerStreamHandler()) + GeneratedPluginRegistrant.register(with: self) return super.application(application, didFinishLaunchingWithOptions: launchOptions) } diff --git a/platform_channels/lib/src/event_channel_demo.dart b/platform_channels/lib/src/event_channel_demo.dart index 355f0fba1..1489d5476 100644 --- a/platform_channels/lib/src/event_channel_demo.dart +++ b/platform_channels/lib/src/event_channel_demo.dart @@ -3,6 +3,7 @@ // found in the LICENSE file. import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:platform_channels/src/accelerometer_event_channel.dart'; /// Demonstrates how to use [EventChannel] to listen continuous values @@ -25,7 +26,7 @@ class EventChannelDemo extends StatelessWidget { stream: Accelerometer.readings, builder: (context, snapshot) { if (snapshot.hasError) { - return Text(snapshot.error.toString()); + return Text((snapshot.error as PlatformException).message); } else if (snapshot.hasData) { return Column( mainAxisAlignment: MainAxisAlignment.center,