|
1 | 1 | import Flutter
|
2 | 2 | import UIKit
|
3 | 3 |
|
4 |
| -//https://flutterdevs.com/blog/creating-a-flutter-plugin-for-android-and-ios-image-gallery/ |
5 |
| - |
| 4 | +//============================================================================== |
6 | 5 | public class SwiftProximityStreamHandler : NSObject,FlutterStreamHandler
|
7 | 6 | {
|
| 7 | + let notiCenter = NotificationCenter.default |
| 8 | + let device = UIDevice.current |
| 9 | + var observer: NSObjectProtocol? |
| 10 | + |
8 | 11 | public func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
|
9 |
| - let device = UIDevice.current |
10 | 12 | device.isProximityMonitoringEnabled = true
|
11 |
| - //NotificationCenter.default.addObserver(forName: <#T##NSNotification.Name?#>, object: <#T##Any?#>, queue: <#T##OperationQueue?#>, using: <#T##(Notification) -> Void#>) |
12 |
| - NotificationCenter.default.addObserver(forName: UIDevice.proximityStateDidChangeNotification, object: device, queue: nil) |
13 |
| - { /* [weak self] */ (notification) in |
14 |
| - //guard let strongSelf = self else { return } |
15 |
| - if let device = notification.object as? UIDevice { |
16 |
| - print("from swift ...") |
17 |
| - // Int32 type 인 경우 dart 에서 수신하는 것은 4 byte: this is dart code --> [0, 0, 0, 0] |
18 |
| - // --> dart 에서 처리시 list 로! 즉, return new ProximityEvent(event.cast<int>()[0]); |
19 |
| - // Int8 type 인 경우 dart 에서 수신하는 것은 1 byte: this is dart code --> [0] |
20 |
| - // --> 이경우에는 dart 코드에서는 리스트로 처리 안해도 됨 ! |
21 |
| - // return new ProximityEvent(event.cast<int>()); |
22 |
| - let onoff:Int8 = device.proximityState ? 0 : 1 |
23 |
| - print(onoff) |
24 |
| - var data = Data() |
25 |
| - withUnsafeBytes(of: onoff) {buffer in data.append(buffer.bindMemory(to: Int8.self))} |
26 |
| - events(data) |
27 |
| - } |
28 |
| - } |
29 |
| - //////////////// |
30 |
| - /* |
31 |
| - NotificationCenter.default.addObserver(self, selector: #selector(proximityStateDidChange), |
32 |
| - name: UIDevice.proximityStateDidChangeNotification, |
33 |
| - object: device) |
34 |
| - */ |
| 13 | + |
| 14 | + observer = notiCenter.addObserver(forName: UIDevice.proximityStateDidChangeNotification, |
| 15 | + object: device, |
| 16 | + queue: nil, |
| 17 | + using : { (notification) in |
| 18 | + if let device = notification.object as? UIDevice { |
| 19 | + let onoff:Int8 = device.proximityState ? 1 : 0 |
| 20 | + var data = Data() |
| 21 | + withUnsafeBytes(of: onoff) { |
| 22 | + buffer in data.append(buffer.bindMemory(to: Int8.self))} |
| 23 | + |
| 24 | + events(data) |
| 25 | + } |
| 26 | + }) |
35 | 27 | return nil
|
36 | 28 | }
|
37 | 29 |
|
38 | 30 | public func onCancel(withArguments arguments: Any?) -> FlutterError? {
|
39 |
| - //TODO |
| 31 | + notiCenter.removeObserver(self, name: UIDevice.proximityStateDidChangeNotification, object: nil) |
| 32 | + device.isProximityMonitoringEnabled = false |
| 33 | + |
40 | 34 | return nil
|
41 | 35 | }
|
42 |
| - /* |
43 |
| - @objc func proximityStateDidChange(notification: Notification) |
44 |
| - { |
45 |
| - if let device = notification.object as? UIDevice { |
46 |
| - print("from swift ...") |
47 |
| - print( device) |
48 |
| - } |
49 |
| - } |
50 |
| - */ |
51 | 36 | }
|
52 | 37 |
|
| 38 | +//============================================================================== |
53 | 39 | public class SwiftProximitySensorPlugin: NSObject, FlutterPlugin
|
54 | 40 | {
|
55 |
| - //static var observer:NotificationCenter = NotificationCenter.default |
56 | 41 | static var stream_handler:SwiftProximityStreamHandler = SwiftProximityStreamHandler()
|
57 | 42 | static var channel:FlutterEventChannel = FlutterEventChannel()
|
58 | 43 |
|
59 |
| - public static func register(with registrar: FlutterPluginRegistrar) |
60 |
| - { |
61 |
| - //---------------------------- FlutterEventChannel |
| 44 | + public static func register(with registrar: FlutterPluginRegistrar) { |
62 | 45 | let channel = FlutterEventChannel.init(name: "proximity_sensor", binaryMessenger: registrar.messenger())
|
63 | 46 | channel.setStreamHandler(stream_handler)
|
64 |
| - //---------------------------- |
65 |
| - //auto generated code ...! |
66 |
| - /* |
67 |
| - let channel = FlutterMethodChannel(name: "proximity_sensor", binaryMessenger: registrar.messenger()) |
68 |
| - let instance = SwiftProximitySensorPlugin() |
69 |
| - registrar.addMethodCallDelegate(instance, channel: channel) |
70 |
| - */ |
71 |
| - } |
72 |
| - |
73 |
| - /* |
74 |
| - public func detachFromEngine(for registrar: FlutterPluginRegistrar) { |
75 |
| - let device = UIDevice.current |
76 |
| - device.isProximityMonitoringEnabled = false |
77 |
| - NotificationCenter.default.removeObserver(observer) |
78 |
| - } |
79 |
| - */ |
80 |
| - ////////////////////////////////////////////////////////////////////////////// |
81 |
| - // - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result; |
82 |
| - // Called if this plugin has been registered to receive `FlutterMethodCall`s. |
83 |
| - // @param call The method call command object. |
84 |
| - // @param result A callback for submitting the result of the call. |
85 |
| - public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) { |
86 |
| - NSLog(call.method) //"listen" |
87 |
| - if call.method == "listen" { |
88 |
| - } |
89 |
| - //result("iOS " + UIDevice.current.systemVersion) |
90 |
| - } |
| 47 | + } |
91 | 48 | }
|
92 | 49 |
|
0 commit comments