Skip to content

Commit

Permalink
Check if torch is available in iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
svprdga committed Nov 30, 2021
1 parent 113faff commit d26fef8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/torch_light/ios"

SPEC CHECKSUMS:
Flutter: 434fef37c0980e73bb6479ef766c45957d4b510c
Flutter: 50d75fe2f02b26cc09d224853bb45737f8b3214a
torch_light: 682062fa12102172fa38b6b14c106d93b060f83e

PODFILE CHECKSUM: aafe91acc616949ddb318b77800a7f51bffa2a4c

COCOAPODS: 1.10.1
COCOAPODS: 1.11.2
15 changes: 15 additions & 0 deletions ios/Classes/SwiftTorchLightPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ public class SwiftTorchLightPlugin: NSObject, FlutterPlugin {

// Constants

let nativeEventIsTorchAvailable = "torch_available"
let errorIsTorchAvailable = "torch_available_error"

let nativeEventEnableTorch = "enable_torch"
let errorEnableTorchExistentUser = "enable_torch_error_existent_user"
let errorEnableTorch = "enable_torch_error"
Expand All @@ -26,6 +29,9 @@ public class SwiftTorchLightPlugin: NSObject, FlutterPlugin {

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch(call.method) {
case self.nativeEventIsTorchAvailable:
isTorchAvailable(result: result)
return
case self.nativeEventEnableTorch:
enableTorch(result: result)
return
Expand All @@ -39,6 +45,15 @@ public class SwiftTorchLightPlugin: NSObject, FlutterPlugin {

// Private methods

private func isTorchAvailable(result: FlutterResult) {
guard let device = AVCaptureDevice.default(for: .video) else {
result(FlutterError(code: self.errorIsTorchAvailable, message: "Could not determine if the device has a torch, please make sure that you are doing this on a real device.", details: nil))
return
}

result(device.hasTorch)
}

private func enableTorch(result: FlutterResult) {
guard let device = AVCaptureDevice.default(for: .video) else {
result(FlutterError(code: self.errorEnableTorch, message: "Could not enable torch, please make sure that you are doing this on a real device", details: nil))
Expand Down

0 comments on commit d26fef8

Please sign in to comment.