UIAlertView changes to landscape when App is locked in portrait only in iOS8 - iphone

I have an app and its orientation is locked as portrait mode, but when I have a UIAlertView and turn the iPhone to landscape, the alert changes the orientation and crops the alert's overlay. Only in iOS8.
Anyone had this error?

Yes I did get this issue. I have developed my code in iOS7 but had to do compatible with iOS 8 also. So I came across This link. I got my solution as follows:
When you are working with iOS7 UIAlertView works fine but if you are working with iOS8 you have to use UIAlertController. Use this kind of code:
if(SYSTEM_VERSION_LESS_THAN(#"8.0"))
{
alert = [[UIAlertView alloc] initWithTitle:#"Warning" message:#"Invalid Coupon." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
else
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Warning" message:#"Invalid Coupon." preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"OK", #"OK action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
}];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
}
Hope this will help you.

Related

Keyboard will appeared automatically in ios 8.3 while displaying alertview or alertcontroller

I have updated Xcode 6.3 and ios8.3 check my code. then it gives me weird result.
here is first screen of my demo app. here is one textfield. when I type somethin in textfield keyboard open.
after typing completed. I have clicked on show alert button. I have displayed alert and output will be following.
After click on cancel.
I have displayed another alert then weird result keyboard should not open but when click on cancel button. display another alert and keyboard will appear automatically.
here is next screen output
following is the code
- (IBAction)MethodShowAlert:(id)sender
{
[tmptxtField resignFirstResponder];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Check Alert textField" message:#"keyboard should not be open" delegate:self cancelButtonTitle:#"cancel" otherButtonTitles:nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
[self showCustomAlertWithTitle:nil];
}
-(void)showCustomAlertWithTitle:(NSString *)title{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Now Check" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
[alertView show]
}
In my case i tried hiding keyboard, before showing alert, so it will not save keyboard in memory to present it again after dismissing it self.
for that you just need to dismiss keyboard which will take default animation time to hide, only then you should present alert view then it will not save that keyboard.
you must put around .6 second gap in hiding keyboard and presenting alert
[YOUR_TEXT resignFirstResponder];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
_alertVw = [[UIAlertView alloc] initWithTitle:#"" message:#"message." delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[_alertVw show];
});
Yep, it's strange.
But since iOS 8, I suggest to use the UIAlertController instead of UIAlertView.
Replace your code with this one:
- (IBAction)MethodShowAlert:(id)sender
{
[tmptxtField resignFirstResponder];
UIAlertController * alertController = [UIAlertController alertControllerWithTitle:#"Check Alert textField"
message:#"keyboard should not be open"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:#"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[self showCustomAlertWithTitle:#"Now Check"];
}];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
}
-(void)showCustomAlertWithTitle:(NSString *)title{
UIAlertController * alertController = [UIAlertController alertControllerWithTitle:title
message:nil
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alertController animated:YES completion:nil];
}
The keyboard will not show after the click on the button.
This was a change in behaviour introduced in iOS 8.3. Try downloading the iOS 8.2 simulator and you will see the old behaviour.
The result of my analysis was the following:
When an alert is shown, it saves the currently showing keyboard.
When an alert has completed the dismiss animation, it restores the previously saved keyboard.
So in -[id<UIAlertViewDelegate> alertView:clickedButtonAtIndex:], you are between those states. So what happens with two Alerts that are shown at the same time:
Show Alert1. Save visible keyboard. Hide keyboard.
User taps on alert.
Show Alert2. Save that there is no keyboard.
Alert1 completes dismiss animation. Restore saved keyboard. Keyboard is visible.
User taps on alert.
Alert2 is dismissed. Restore that there is no keyboard. Hide keyboard.
My recommendation is to use a UIAlertViewDelegate method that is called after the dismiss animation completes and show the next alert then.
Replace your alert method from below method.
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:messege title message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
}];
[alertVC addAction:cancelAction];
[[[[[UIApplication sharedApplication] windows] objectAtIndex:0] rootViewController] presentViewController:alertVC animated:YES completion:^{
}];
I also tried hiding the keyboard directly before presenting the alert.
What worked for me was calling [myTextField endEditing:YES]; instead of [myTextField resignFirstReponder];
This let the keyboard stay hidden, even after the alert was dismissed again.
The code looks like this:
[myTextField endEditing:YES];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"myTitle"
message:nil
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"NO"
style:UIAlertActionStyleCancel
handler:nil];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:#"YES"
style:UIAlertActionStyleDefault
handler:nil];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];

Social sharing url of current webview iPhone. ( Xcode)

so I have my app setup with share action button that shares the current URL to Twitter or Facebook however when I click the Facebook button it shows a Twiiter share sheet. The (Tiwtter option works fine)
After I click the FACEBOOK option, when testing on iPhone the standard twiiter share sheet appears.
- (IBAction)social:(id)sender {
UIActionSheet *share = [[UIActionSheet alloc] initWithTitle:#"Pass on the news!" delegate:self cancelButtonTitle:#"OK" destructiveButtonTitle:nil otherButtonTitles:#"Post to Twitter", #"Post to Facebook", nil];
//You must show the action sheet for the user to see it.
[share showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
//Each button title we gave to our action sheet is given a tag starting with 0.
if (actionSheet.tag == 0) {
//Check Twitter accessibility and at least one account is setup.
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
SLComposeViewController *tweetSheet =[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet addURL:self.newsItemWebView.request.URL]; //This is setting the initial text for our share card.
[tweetSheet setInitialText:#"Check out this article I found using the 'Pass' iPhone app:, "];
//Brings up the little share card with the test we have pre defind.
[self presentViewController:tweetSheet animated:YES completion:nil];
} else {
//This alreat tells the user that they can't use built in socal interegration and why they can't.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Sorry" message:#"You can't send a tweet right now, make sure you have at least one Twitter account setup and your device is using iOS6 or above!." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
} else if (actionSheet.tag == 1) {
//Check Facebook accessibility and at least one account is setup.
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *facebookSheet =[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[facebookSheet addURL:self.newsItemWebView.request.URL];
//This is setting the initial text for our share card.
[facebookSheet setInitialText:#"Check out this article I found using the 'Pass' iPhone app:"];
//Brings up the little share card with the test we have pre defind.
[self presentViewController:facebookSheet animated:YES completion:nil];
} else {
//This alreat tells the user that they can't use built in socal interegration and why they can't.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Sorry" message:#"You can't post a Facebook post right now, make sure you have at least one Facebook account setup and your device is using iOS6 or above!." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
}
}
You are using actionSheet.tag which will be the same for the entiere actionsheet.
If you want to know which button is pressed you should use buttonIndex in your if statements.
Even better is to use UIActivityViewController.
NSArray *items = #[self.newsItemWebView.request.URL];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
activityViewController.completionHandler = ^(NSString *activityType, BOOL completed) {
// do any thing you want when the user finishes the share activity.
// Like present a message that that activity has completed or not.
};
[self presentViewController:activityViewController animated:YES completion:nil];

TWTweetComposeViewController not dismissing on iPad simulator

In my app I have an action sheet and one of its buttons opens the TWTweetComposeViewController modally. On iPhone simulator the cancel button on the tweet composer works fine and dismisses the view. However, on iPad simulator the cancel button does not work and the tweet composer view remains on the screen. It is even weirder because after pressing the cancel button, the keyboard retracts and the underlying views become active. It behaves as if the view has been dismissed but its is still there.
The code I used when the user pressed the action button is:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:#"Open in Safari"]){
[[UIApplication sharedApplication] openURL:[self.webView.request URL]];
}else if ([buttonTitle isEqualToString:#"Twitter"]){
if ([TWTweetComposeViewController canSendTweet]){
TWTweetComposeViewController *tweetSheet = [[TWTweetComposeViewController alloc] init];
[tweetSheet addURL:[self.webView.request URL]];
tweetSheet.completionHandler = ^(TWTweetComposeViewControllerResult result){
if (result == TWTweetComposeViewControllerResultCancelled){
[self dismissModalViewControllerAnimated:YES];
}
};
[self presentModalViewController:tweetSheet animated:YES];
}else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Twitter error" message:#"You can't send a tweet right now, make sure your device has an internet connection and you have at least one Twitter account setup" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
}
}
Do you have any idea on how to solve this problem or is it a bug of the simulator?
P.S.: My app is a tabbar app and this code is called from one of of the view controller of the tab bar.
I'm having this same problem on the actual device. It turns out this is a bug in Apple's SDK for TWTweetComposeViewController.
See the bug report here on OpenRadar: http://openradar.appspot.com/radar?id=1484405.
When a completionHandler block is added to
TWTweetComposeViewController, the completion handler needs to call
-[UIViewController dismissModalViewControllerAnimated:], even though the view for the tweet composer dismisses itself with its cancel or
send buttons. Failure to do so causes touch events to not reach the
view that spawned the tweet composer.
Just thought I'd add how I'm doing things, even though this is not correctly following memory guidelines, it's a workaround:
[compose setCompletionHandler:^(TWTweetComposeViewControllerResult result){
dispatch_async(dispatch_get_main_queue(), ^{
if(self.delegate != nil)
{
if (result == TWTweetComposeViewControllerResultDone)
{
[self.delegate twitterOperation:TETwitterOperationTweet
completedSuccessfully:YES
withResponseString:#"Tweet Successful"];
}
else if(result == TWTweetComposeViewControllerResultCancelled)
{
[self.delegate twitterOperation:TETwitterOperationTweet
completedSuccessfully:NO
withResponseString:#"Tweet Cancelled"];
}
}
// Dismiss per Apple's Twitter example
[self.shownInViewController dismissViewControllerAnimated:YES
completion:nil];
// Yuck. But it's necessary.
[compose release];
});

TWTweetComposeViewController freezes my app completely on alloc-init

I've got an iPhone app that uses the Twitter framework in iOS 5. I've noticed that when I try to tweet something, the app completely freezes on the "alloc init" line of code.
Here's my code. Anybody see something wrong with it?
-(void)tweet {
NSLog(#"Tweeting");
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
if (stringToShare.length < 140) {
TWTweetComposeViewController *tweetController = [[TWTweetComposeViewController alloc] init];
[tweetController setInitialText:stringToShare];
[self presentModalViewController:tweetController animated:YES];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Easy there, Hemingway." message:[NSString stringWithFormat:#"Your note has %d characters, which exceeds Twitter's limit of 140.", stringToShare.length]delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
}
UPDATE: This method is called from a modal view controller. Could that be the issue?
UPDATE 2: Here's the line of code that's used to detect if the user can tweet:
if ([TWTweetComposeViewController canSendTweet]) {
[availableActions addObject:#"Tweet"];
}
I know that the issue is not constant --- it seems to occur on my phone, but not my co-founder's.

UIAlertView not showing message text

I am bringing up an UIAlertView that works fine in portrait layout, but when in landscape mode - the message doesn't appear.
It is a standard UIAlertView, with three buttons.
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"cancel" otherButtonTitles:#"one", #"two", nil];
I have tried moving the buttons down (according to the height of the message label) and resizing the alert according to the relocated buttons, but the message still doesn't appear, despite there being plenty of room for display.
Setting the UILabel background to some color for debugging shows that it just isn't displayed..
EDIT:
The UILabel is there - It's just not being displayed.
In the willPresentAlertView method, I can see the UILabel in the NSAlertView's subviews.
It appears to be a bug with the layout code for UIAlertView. After fiddling a bit in the debugger I managed to get this workaround:
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"cancel" otherButtonTitles:#"one", #"two", nil];
[alert show];
// for some reason we have alpha 0 for 3 or 4 buttons
[[[alert subviews] objectAtIndex:2] setAlpha:1];
// also, for 3 buttons the height goes to 10 -> proof of concept 'fix'
[[[alert subviews] objectAtIndex:2] setFrame:CGRectMake(12, 45, 260, 24)];
[alert release];
This is just a proof of concept. A real workaroung should iterate ober the subviews and fix only labels that have either height to small or alpha==0
Probably you missed:
[alert show];
You can directly use uialertview and create object of it. Then pass title and message and button and also other button.....And call click button method.
//Example
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Title" message:#"The message."
delegate:self cancelButtonTitle:#"button 1" otherButtonTitles:#"button", nil];
[alert show];
[alert relaese];
//Then use this method
-(void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
// the user clicked one of the ok/cancel buttons
if(buttonIndex==0)
{
NSLog(#"Ok");
}
else
{
NSLog(#"cancel");
}
}