Perform action as soon as camera opens ios - ios7

I am using ZBAR scanner for scanning bar codes. But it takes quite some time before the camera opens. In the meantime, i want to have a loading animation once the button is pressed and stop the animation after the camera is open. Can anyone plz tell how to detect as soon as the camera is open event handler in ios.

I'm not sure if this is what you are looking for, but I think you can use KVO (Key-value observing) to have event-handler mechanism.
There is this connected property that is KVO-compliant that might tell you when the camera opens (I am not sure though). Here's the documentation:
connected
Indicates whether the device is currently connected.
(read-only)
#property(nonatomic, readonly, getter=isConnected) BOOL connected
Discussion The value of this property indicates whether the device
represented by the receiver is connected and available for use as a
capture device. When the value of this property becomes NO for a given
instance, however, it will not become YES again. If the same physical
device again becomes available to the system, it will be represented
using a new instance of AVCaptureDevice.
You can observe the value of this property using Key-value observing
to be notified when a device is no longer available.
Then you can try to implement observer for this connected property by following the link below. Basically what you need to do is something like:
Event Handler
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if( [keyPath isEqualToString:#"connected"] ){
BOOL isConnected = [ [change objectForKey:NSKeyValueChangeNewKey] isEqualToNumber:[NSNumber numberWithInt:1] ];
if(isConnected){
//remove loading icon..
} else {
//show loading icon..
}
}
}
Event Registration
- (void)viewWillAppear:(BOOL)animated{
AVCaptureDevice *camDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
int flags = NSKeyValueObservingOptionNew;
[camDevice addObserver:self forKeyPath:#"connected" options:flags context:nil];
(...)
}
Here's the documentation that might help you:
KVO Programming Guide
NSKeyValueObserving Protocol
Hope this helps you! :)

Related

how to turn off the Camera Light while taking snap programmatically?

I'm developing one app in which i want to capture an image at run time.
It has to work for both MAC PRO & Mac mini (If web cam is connected). When Camera takes picture small light will come at the time of taking snap, that should not come. Its not regarding the flash.
code snippet:
- (void)captureOutput:(QTCaptureOutput *)captureOutput didOutputVideoFrame:(CVImageBufferRef)videoFrame withSampleBuffer:(QTSampleBuffer *)sampleBuffer fromConnection:(QTCaptureConnection *)connection
{
// If we already have an image we should use that instead
if ( currentImage ) return;
// Retain the videoFrame so it won't disappear
// don't forget to release!
CVBufferRetain(videoFrame);
// The Apple docs state that this action must be synchronized
// as this method will be run on another thread
#synchronized (self) {
currentImage = videoFrame;
}
// As stated above, this method will be called on another thread, so
// we perform the selector that handles the image on the main thread
[self performSelectorOnMainThread:#selector(saveImage) withObject:nil waitUntilDone:NO];
}
To capture picture i'm using the above method. Thanks in advance
According to this article, several older MacBooks' camera LEDs could be circumvented by tying into the circuit board logic directly.
To my knowledge, there is no longer this security loophole, nor is there an API available in Objective-C to disable the camera LED.

Getting the UITextField that's associated with my observer

I've got an observer on my UItextFields. It basically looks to see if the "enabled" property has changed.
If the enabled status changes, I want a method called fade to run. Fade requires a TextField to be passed to it.
How do I use the textField associated with the Observer that sends the message?
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change: (NSDictionary *)change context:(void *) context;
{
int new = [change objectForKey:NSKeyValueChangeNewKey];
int old = [change objectForKey:NSKeyValueChangeOldKey];
if (new != old)
{
[self fadeEnable:"requires txtField"];
}
}
Thanks
As shown in the Docs,
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol/Reference/Reference.html, the parameter 'Object' in the method call encapsulates your textfield.
you just have to cast it and youre done.
To cast it, use:
UITextField *tf = (UITextField *)object;
Edit to address your follow-up Question:
In The case of a Gesture recognizer, you want to get the view that you added the recognizer to. The 'sender' being the recognizer itself, sender.view will get you the associated view.
None of this will ever change, because recognizers only work with VIEWS.
Now, with an observer, you do not necessarily want a view. You want an OBJECT. Thats why 'object' is of type 'id', so you can add observers to objects of arbitrary types. The downside is, of course, that you need a cast. If you dont know what type to cast to, the
-isKindOfClass:
method might be useful.
Oh, and none of this is about interface-builder versus doing stuff in code.
If you find this confusing, i suggest you go study the docs on gestureRecognizers and Key-Value-Observing. If you have specific questions, let me know, or just ask an new question ;)

WebView did finish launching does not work

what is wrong with this method?
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
[activityIndicator stopAnimation:self];
}
I want to stop the Circular Progress Indicator (activityIndicator). But there is something wrong with - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame. I am coding for mac osx and not for iOS. I heard something from Delegates, what does that mean?
Check to be sure that your UIWebView has set its delegate. Setting a delegate is basically telling the program who you want to handle events (like taps, gestures, or, in this case, the loading of a webView). Thus when an event is fired, it will inform the delegate and the delegate can process it. Maybe if you post more of your code it would help, but I would check your declaration of the UIWebView in question. Be sure that after you allocate it and initialize it, you set its delegate to self (assuming that this method is in the same class), like so:
UIWebView *myWebView = [[UIWebView alloc] init];
[myWebView setDelegate:self];
If you have not set the delegate, it is firing off events and no one is receiving them to process them. The method you are using is waiting for the specific event sent by any webView. When it is sent an event message it passes, as a parameter, the webView that triggered. In any case, put in a log statement to be sure you are entering the method. That will tell you if it is receiving the event messages.
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
NSLog(#"Did finish loading...");
[activityIndicator stopAnimation:self];
}
NOTE: This is as per iOS experience, but should work for Mac OS as well. Let me know what your log result is, if the method is getting called or not.

Disabling dictation within the app [duplicate]

Ours is a health care app. We have a HIPAA-compliant speech recognizer in the app through which all the dictation can take place. The hospitals don't want physicians to accidentally start speaking to the Nuance Dragon server which is not HIPAA-compliant. So, I was looking for ways I could supress the dictation key on the keyboard.
I tried putting a fake button on the Dictation button on the key pad, but on the iPad the split dock concept keeps moving the microphone all over the screen. This does not sound like a reasonable solution. Are there any experts out there who could help me?
OKAY, finally got it! The trick is to observe UITextInputMode change notifications, and then to gather the identifier of the changed mode (Code seems to avoid the direct use of Private API, though seems to require a little knowledge of private API in general), and when the mode changes to dictation, resignFirstResponder (which will cancel the voice dictation). YAY! Here is some code:
Somewhere in your app delegate (at least that's where I put it)
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(inputModeDidChange:) name:#"UITextInputCurrentInputModeDidChangeNotification"
object:nil];
And then you can
UIView *resignFirstResponder(UIView *theView)
{
if([theView isFirstResponder])
{
[theView resignFirstResponder];
return theView;
}
for(UIView *subview in theView.subviews)
{
UIView *result = resignFirstResponder(subview);
if(result) return result;
}
return nil;
}
- (void)inputModeDidChange:(NSNotification *)notification
{
// Allows us to block dictation
UITextInputMode *inputMode = [UITextInputMode currentInputMode];
NSString *modeIdentifier = [inputMode respondsToSelector:#selector(identifier)] ? (NSString *)[inputMode performSelector:#selector(identifier)] : nil;
if([modeIdentifier isEqualToString:#"dictation"])
{
[UIView setAnimationsEnabled:NO];
UIView *resigned = resignFirstResponder(window);
[resigned becomeFirstResponder];
[UIView setAnimationsEnabled:YES];
UIAlertView *denyAlert = [[[UIAlertView alloc] initWithTitle:#"Denied" message:nil delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil] autorelease];
[denyAlert show];
}
}
you can create your own keyboard and set the inputView for the text fields that will accept this dictation. then when they press any keys they will get your keyboard, therefore you dont have to override the keys on the standard keyboard, you will be able to customize the entire thing.
self.myButton.inputView = self.customKeyboardView;
here is an example of an extremely custom keyboard
http://blog.carbonfive.com/2012/03/12/customizing-the-ios-keyboard/
Ray also has a teriffic tutorial on custom keyboards.
http://www.raywenderlich.com/1063/ipad-for-iphone-developers-101-custom-input-view-tutorial
I hope that helps.
I had the same issue and the only way i found that hides the dictation button is changing the keyboard type.
For me changing it to email type seemed to be reasonable:
textField.keyboardType = UIKeyboardTypeEmailAddress;
You could make a subclass of UITextField/UITextView that overrides insertDictationResult: to not insert anything.
This won't prevent the information being sent, but you could then display an alert informing them of the breech.
This is a Swift 4 solution based on #BadPirate's hack. It will trigger the initial bell sound stating that dictation started, but the dictation layout will never appear on the keyboard.
This will not hide the dictation button from your keyboard: for that the only option seems to be to use an email layout with UIKeyboardType.emailAddress.
In viewDidLoad of the view controller owning the UITextField for which you want to disable dictation:
// Track if the keyboard mode changed to discard dictation
NotificationCenter.default.addObserver(self,
selector: #selector(keyboardModeChanged),
name: UITextInputMode.currentInputModeDidChangeNotification,
object: nil)
Then the custom callback:
#objc func keyboardModeChanged(notification: Notification) {
// Could use `Selector("identifier")` instead for idSelector but
// it would trigger a warning advising to use #selector instead
let idSelector = #selector(getter: UILayoutGuide.identifier)
// Check if the text input mode is dictation
guard
let textField = yourTextField as? UITextField
let mode = textField.textInputMode,
mode.responds(to: idSelector),
let id = mode.perform(idSelector)?.takeUnretainedValue() as? String,
id.contains("dictation") else {
return
}
// If the keyboard is in dictation mode, hide
// then show the keyboard without animations
// to display the initial generic keyboard
UIView.setAnimationsEnabled(false)
textField.resignFirstResponder()
textField.becomeFirstResponder()
UIView.setAnimationsEnabled(true)
// Do additional update here to inform your
// user that dictation is disabled
}

Detecting an NSColorWell's changed selection

I have been looking at how to use an NSColorWell in my app, but there doesn't seem to be much documentation on it.
Is there any way to embed a colour picker (such as the NSColorWell/NSColorPanel) directly into my view? When clicking the well, it always presents a new colour picking window. Can this not be embedded somehow?
I have a custom NSButton class to which I am passing the colour from my NSColorWell. To do this, I am having to make the user pick a colour, then click a button to send this colour to my custom class. Is there a way of simply detecting when a new colour is selected directly from the color picker?
For problem 1, no, that's the system behaviour for an NSColorWell. Don't like it? make your own.
Problem 2 has two possible solutions.
Method 1: Connect the action from the color well to your object in IB and read the color of the color well via an outlet from your class. Any color change in the well will send a message to the selector of your choice.
Method 2: Add an object of your own as an observer to the color property
[colorwell addObserver:self forKeyPath:#"color" options:0 context:NULL];
then implement
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
Any color change will trigger that method.
Be sure to detach from the color well in dealloc or another breakdown method:
[colorwell removeObserver:self forKeyPath:#"color"]
There's a way to do it with bindings as well, but these two are fine.
For the color panel problem, I was also looking for such a thing. The closes I could find was this one on CocoaControls. Unfortunately, this only works on 10.7+ (as it uses NSPopover) and it looks like it uses a private API.
For swift 2 I use this code:
#IBOutlet weak var colorSelector: NSColorWell!
override func viewDidAppear() {
super.viewDidAppear()
self.colorSelector.addObserver(self, forKeyPath: "color", options: .New, context: nil)
}
override func viewDidDisappear(){
super.viewDidDisappear()
self.colorSelector.removeObserver(self, forKeyPath:"color")
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
print("keyPath=>\(keyPath)")
if (keyPath! == "color") {
print("object=>\(self.colorSelector.color)")
}
}