I'd like to add flashlight functionality to my app in Swift. How can I go about doing that?
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- SwiftUI: UIImage (QRCode) does not load after call
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- Using if let syntax in switch statement
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Enum with associated value conforming to CaseItera
- Swift - hide pickerView after value selected
- Is there a Github markdown language identifier for
I've updated @Lyndsey Scott's great answer for Swift 2.0
Solution For Swift 4 With Condition torch is available or not
The Solution is Combination of @Joshua Dance And @Lance
Update #1: (
torchActive
isn't returning the expected value; perhaps because it's been modified)Update #2: For Swift 2.0
To toggle the flash from on to off (not just "on" as in mad pig's answer), you can use the following method:
I used nested do-catch blocks to implement Awesomeness's suggestion from the comments. This way, even if
try device.setTorchModeOnWithLevel(1.0)
fails, the device is properly unlocked for configuration.Update #3: For Swift 4:
(I edited the code a bit to my personal taste)
Original answer:
To toggle the flash from on to off (not just "on" as in mad pig's answer), you can use the following method: