dismiss UIActionSheet when App goes in background on iOS4 - iphone

In the app I am working on, I have action sheets and alert views which I would like dismissed when app enters the inactive/ background state.
I am using the UIApplicationWillResignActiveNotification instead of the UIApplicationDidEnterBackgroundNotification as I want the code to be compatible with iOS3.2.
-(void)applicationWillResignActive:(Notification *)notification{
if (self.actionSheet && self.actionSheet.visible){
NSLog(#" actionSheet is Visible");
[self.actionSheet dismissWithClickedButtonIndex:0 animated:NO];
}
}
Testing this in simulator (iphone 3.2, iOS4), with the actionSheet visible, I press the home button, but I do not get the "actionSheet is Visible" message. Yet when I re-open the app and dismiss it again with home button, I get the "actionSheet is Visible" message.
This suggests that the first time the actionSheet's visible property is not being set. Could there be a delay in the property being set? In fact I put a message in the method that displays the actionSheet
[self.actionSheet showInView:self.parentViewController.tabBarController.view];
if (self.actionSheet.Visible) NsLog(#" action Sheet visible");
even here I do not get the message. Where/ when is the visible property set? Am I doing something fundamentally wrong in trying to dismiss the actionSheet? I have seen similar very good and detailed solutions on dismissing alertViews in SO.... but they don't seem to cover this issue. Any help will be much appreciated.

Why would you even need to check if it's visible? In fact, why would you even need to check it against nil? You could just put [self.actionSheet dismissWithClickedButtonIndex:0 animated:NO];, and it should work fine, as if the action sheet exists you will dismiss it, and if it doesn't, you will will just call the method on nil, which does nothing.

Related

Removing UIAlertView from keyWindow Error

I am removing UIAlertView from UIWindow by using this code : -
UIAlertView *alert= (UIAlertView*)[[[[UIApplication sharedApplication] keyWindow]subviews]objectAtIndex:0];
[alert removeFromSuperview];
After removing alertview from key Window my Whole screen turns faded like suspended while alertview shown. Please tell me how can i make my all screen normal.Thanks in advance.
Don't remove the alert like that. Even if you get it to work now it could easily break with any future update to iOS.
Instead, when you create and show the alert, keep a reference to it. Then, when you need to, call dismissWithClickedButtonIndex:animated:.

Keyboard shows then immediately hides itself when showing MFMessageComposeViewController

I am building a PhoneGap app using Cordova 2.2 for IOS. I am experiencing some bizarre behavior when calling out to a native obj-c plugin I have written to show the MFMessageComposeViewController.
The setup is very simple - I have a tap event attached to a UI element, that when pressed, will make a call to my PhoneGap plugin, pass with it a number and a text message, then show the MFMessageComposeViewController with the parameters pre-populated.
My javascript looks like this:
$(document).bind('deviceready', function(){
$(".theButton").tap(function(){
cordova.exec(function(){}, function() {}, "PhoneGapSms", "SendSms", [db.getItem("profile_sms"), db.getItem("profile_emergency")]);
});
});
And my obj-c code looks like this:
- (void)SendSms:(CDVInvokedUrlCommand*)command
{
CDVInvokedUrlCommand* myCommand = command;
MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
NSString* body = [command.arguments objectAtIndex:0];
NSString* toRecipientsString = [command.arguments objectAtIndex:1];
if(body != nil)
picker.body = body;
if(toRecipientsString != nil)
[picker setRecipients:[ toRecipientsString componentsSeparatedByString:#","]];
picker.messageComposeDelegate = self;
[self.viewController presentModalViewController:picker animated:YES];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
[picker release];
}
So, all in all, very simple stuff.
My Problem is this:
When my iPhone is plugged into my Mac and the app is run from XCode, the Message Composer overlay appears great, with my values pre-populated. Image below demonstrating the SMS interface appears fine while plugged into XCode:
When my iPhone is unplugged from my Mac, and the app is run from the spring board, the Overlay slides up, the keyboard begins to slide up, then immediately slides down - making it impossible to type or send the message. This is what it looks like when not attached to the Mac/Xcode - the keyboard begins to slide up then immediately slides down (~ < 1 sec) leaving the following interface:
I can't for the life of me figure out what would cause the keyboard to hide when not running from XCode, but work perfectly well when it is.
Is there any way to 'force' the keyboard to display, or possibly put the whole modalviewcontroller as first responder in some form or fashion?
Any suggestions are appreciated!
Edit:
The keyboard WILL appear again if you click in the contact area
You must add MessageUI.framework to your Xcode project and include a
#import <MessageUI/MessageUI.h> in your header file.
try this code may be its helpful to you..
[self presentModalViewController:picker animated:YES];
//[self becomeFirstResponder];//try picker also instead of self
Also Refer this bellow tutorial and also check demo..
new-tutorial-developing-and-architecting-a-phonegap-application
SMSComposer
i hope this help you...
I encountered these symptoms with a Sencha Touch 2.2 and Cordova 2.6.0 setup (specifically, iOS 6+ devices).
The issue was with the web framework stealing focus away from the native SMS Composer modal, typically occurring after the first SMS Composer modal had been successfully displayed and closed.
A event.preventDefault() and event.stopPropagation() call once the event had been fired (or event.stopEvent() in Sencha land) resolved this.
Hope this helps,
-James
First: The most likely culprit, without seeing your code, is that your parent (presenting) view controller may have an action in its viewWillDisappear or viewDidDisappear that is affecting who has the "first" responder. These methods get called when presenting a view controller modally. It may be behaving differently on the simulator than the device due to timing - often really close timing conditions are different on the ARM device and the i386 processor.
Second: Does anywhere in your app register for the UIKeyboardWillShowNotification or the UIKeyboardDidShowNotification? If so, put breakpoints in the methods that are called as a result - it's possible some other controller in your view hierarchy is interfering with this one.
To answer your question...
Is there any way to 'force' the keyboard to display, or possibly put the whole modalviewcontroller as first responder in some form or fashion?
No to both. The only way to make the keyboard display is to call the becomeFirstResponder method of the input view. Since Apple doesn't expose the text view, you cannot send it messages. Setting the modalViewController as first responder would be setting the wrong object.
I found the same type of issue But Not Sure it will Solve your problem or not Just Have a Look on the Following Links :
1. http://community.phonegap.com/nitobi/topics/keyboard_is_not_triggerd_in_my_ios_build
2. https://github.com/phonegap/build/issues/31
If its not Solving your issue then you can Download the Sample code Here.
Link: https://github.com/phonegap/phonegap-plugins/tree/master/iPhone/SMSComposer

Weird keyboard behavior in my iOS app

Ok, so here's how my keyboard behaves:
There's a button which calls a method to send a textmessage, that works fine. If the user now sends the text everything's fine.
Now if (s)he taps cancel it switches back to my view (as it's supposed to do), but the keyboard won't show up. I already tried
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
[self dismissModalViewControllerAnimated:YES];
[inputView becomeFirstResponder];
}
since it didn't work I tried:
-(void)viewDidAppear:(BOOL)animated{
[inputView becomeFirstResponder];
}
but that won't work either. So how can I get my keyboard to show up?
EDIT: inputText is an UITextView.
This might be related to sending YES in dismissModalViewControllerAnimated:. When animations are involved, statements generally don't remain synchronous. So, your call to [inputView becomeFirstResponder] must be executing before the modal dialog has been dismissed, resulting in an inconsistent state. This is the reason why the inputView does not take focus.
HTH,
Akshay

iPhone app entering back into foreground shifts the view back to its origins and keyboard is still visible

I'm currently working on an iPhone 4 app with a registration view. The users can focus into a UITextField and I have code that will shift the view upwards to prevent the keyboard from covering up the textfield. But if the app is backgrounded and brought back into the foreground again, the keyboard is still up, the textfield is still in focus, but the view is now shifted back down in its original state. This covers up the textfield.
What's going on? How do I either make the view stay put or hide the keyboard when the app is brought back into the foreground?
UPDATE:
-Any changes for this on the new iOS5?
you could try doing something in applicationDidEnterBackground in your app delegate like
NSLog(#"%#", [self.viewController.YOURTEXTFIELD isFirstResponder]);
if ([self.viewController.YOURTEXTFIELD isFirstResponder]) {
[self.viewController.YOURTEXTFIELD resignFirstResponder];
}
the "isFirstResponder" checks to see if the keyboard is currently being used in this view and returns YES if it is and NO if it isn't.
The NSLog is there just so you know what is getting passed into the if statement.
I've next solution:
In AppDelegate
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// 1. get access to ViewController which is on top
// In my case, I have navigation controller in root
UIViewController* current_controller = [self.rootNavController.viewControllers lastObject];
// 2. loop all uitextfield.
for (UITextField* o_txt in [current_controller.view subviews]) {
[o_txt resignFirstResponder];
}
}
Look's like "hot fix" )

iPhone: taking a picture programmatically

I'm trying to use the UIImagePickerController interface from OS 3.1, with the cameraOverlayView and takePicture, but I've clearly failed to understand how this works, and so I'm not getting the behaviour I want.
What I want to do is open the camera and take a picture automatically without having to having the user interact with the picker or edit the image. So I subclass UIImagePickerController (similar to the example in http://github.com/pmark/Helpful-iPhone-Utilities/tree/master/BTL%20Utilities/) and turn off all of the controls:
- (void)displayModalWithController:(UIViewController*)controller animated:(BOOL)animated {
self.sourceType = UIImagePickerControllerSourceTypeCamera;
self.showsCameraControls = NO;
self.navigationBarHidden = YES;
self.toolbarHidden = YES;
// Setting the overlay view up programmatically.
ipView = [[ImagePickerView alloc] init];
self.cameraOverlayView = ipView;
[controller presentModalViewController:self animated:NO];
}
In the overlayView, I've managed to force the takePicture method of UIImagePickerController to fire (I know this, because I can NSLog it, and I hear the sound of the camera taking a picture). The overlayView shows up just fine. However, the delegate method didFinishPickingMediaWithInfo: never gets called, and imagePickerControllerDidCancel doesn't get called either.
So, how do I either get the delegate methods to get called, or save the picture by overriding the takePicture method? (I have no idea how to capture the picture data here, and Google seems to have failed me). I can't help feeling that I've failed to understand how the guts of UIImagePickerController works, but the docs aren't overly helpful:
e.g.:
"You can provide a custom overlay view to display a custom picture-taking interface and you can initiate the taking of pictures from your code. Your custom overlay view can be displayed in addition to, or instead of, the default controls provided by the image picker interface."
or from showCameraControls:
"If you set this property to NO and provide your own custom controls, you can take multiple pictures before dismissing the image picker interface." - How do I dismiss the picker interface?
Note: the delegate is set properly in IB, so that's not the problem.
Thanks for any help you can provide!
I've found that you just have to wait "long enough" before calling takePicture, or it just silently fails. I don't have a good answer for how to determine the minimum value of "long enough" that will always work, but if you set a timer and wait five or ten seconds you should be okay. It would be nice if it returned some kind of an "I'm not ready to take a picture yet, sorry" error either directly from takePicture or through the delegate, but as far as I know it doesn't.
As an update to my own question: It turns out that I was trying to use takePicture too early. When I moved the action to a button on the overlay and sent takePicture from that button (once the picker was presented modally), the delegate methods fired as they should. I don't know if what I wanted is achievable - taking the image without having to press that button, automatically - but if it is, it will probably have to be done by sending takePicture sometime after I was trying to use it.
-(void)imageMethod:(id)sender{
imagePickerController = [[UIImagePickerController alloc]init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePopover=[[UIPopoverController alloc]initWithContentViewController:imagePickerController];
[imagePopover presentPopoverFromRect:importButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
}