Passing variables inside Appdelegate - objective-c

When I use a variable inside AppDelegate and then I use it inside a function the variable doesn't change its value!
Any help? I use objective c
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// Show alert for push notifications recevied while the
// app is running
NSString *message = [[userInfo objectForKey:#"aps"]
objectForKey:#"alert"];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#""
message:message
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
How can I get message outside of this function?

You can make a class variable :
NSString *mesage;//You can use this variable anywhere in AppDelegate
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// Show alert for push notifications recevied while the
// app is running
message = [[userInfo objectForKey:#"aps"]
objectForKey:#"alert"];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#""
message:message
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];

Related

UIAlertView On Button Click

Yes, I've already Googled this question and I've used the exact code, but that's still not working.
Here is my current Objective-C code:
- (IBAction)btnTemp:(id)sender
{
if (_deepSwitch.on == TRUE)
{
[self TempCleaner];
_progress.progress += 1;
}
UIAlertView *cleaned = [[UIAlertView alloc] initWithTitle:#"Done!" message:#"Your device is now clean. Restarting SpringBoard." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[cleaned show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == [alertView cancelButtonIndex]) {
NSLog(#"clicked");
}
}
Why isn't this working? I've tried buttonIndex 0, and cancel button!
Set the alert view's delegate to self
UIAlertView *cleaned = [[UIAlertView alloc] initWithTitle:#"Done!" message:#"Your device is now clean. Restarting SpringBoard." delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
Also, make sure that the class from which you are presenting the alert view declares the UIAlertViewDelegate protocol
//YourClass.h
#interface YourClass : SuperClass <UIAlertViewDelegate>

UIAlertView alertView.cancelButtonIndex = -1; Not Working

I am trying to create an AlertView which does not have a cancel button.
This is the code I have tried:
alertView.cancelButtonIndex = -1;
It does not seem to work with iOS 7.
Any ideas?
Thanks,
1) With using this code pressing "Do something button" can do some action but will not lead to alertView hiding
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Title" message:#"The message." delegate:self cancelButtonTitle:nil otherButtonTitles: #"Do something", nil] show];
#pragma mark - UIAlertViewDelegate
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView {
return NO;
}
2) With using this code there will be no buttons on the alertView(I think this is what you actually need)
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Title" message:#"The message." delegate:self cancelButtonTitle:nil otherButtonTitles:nil] show];

MFMailComposeViewController won't dismiss on iOS 6

In iOS 6 the presented MFMailComposeViewController will not dismiss if user attempts to send second email...
Everything works perfectly the first go around and email is sent. However, if email option is selected again the MFMailComposeViewController will not dismiss on cancel.
Here is how I implemented it:
- (IBAction)buttonEmailClick:(id)sender {
if (![MFMailComposeViewController canSendMail]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Can't send" message:#"This device is unable to send emails." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
return;
}
NSDictionary *contactsInfo = [self contactsInfoFromPlistNamed:kConfigPlistName];
[mailComposeViewController setToRecipients:[NSArray arrayWithObject:[contactsInfo objectForKey:#"email"]]];
//[mailComposeViewController setSubject:kEmailDefaultSubject];
//[mailComposeViewController setMessageBody:text isHTML:NO];
[self presentModalViewController:mailComposeViewController animated:YES];
}
and then this:
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
UIAlertView *alert = nil;
if (result == MFMailComposeResultSent) {
alert = [[UIAlertView alloc] initWithTitle:#"Sent" message:#"Your email was sent." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
}
else if (result == MFMailComposeResultFailed) {
alert = [[UIAlertView alloc] initWithTitle:#"Failed" message:#"An error occured and your email was not sent." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
}
[alert show];
[self dismissModalViewControllerAnimated:YES];
}
It works fine in iOS 5 but not in iOS 6. I have tried replacing with non deprecated methods for iOS 6, but it doesn't work.
Have you tried creating a fresh MFMailComposeViewController each time they go to send an email? I'm not sure if you should be reusing it.
You can try this:
MFMailComposeViewController * composer = [[MFMailComposeViewController alloc] init];
composer.delegate = self;
-(void)mailComposeController:didFinishWithResult:error: should be called if you assign that class to the delegate

clickedButtonAtIndex: not working

Hi guys i have this i IBAction linked to a button:
- (IBAction)showCurl:(id)sender {
alert1 = [[UIAlertView alloc]initWithTitle:#"Loading" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[alert1 show];
}
and a clickedButtonIndex to auto run but somehow it doesn't load SecondViewController:
#pragma mark UIAlertView
- (void)alertView:(UIAlertView *)alert1 clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 0){
SecondViewController *sampleView = [[SecondController alloc] init];
[sampleView setModalTransitionStyle:UIModalTransitionStylePartialCurl];
[self presentModalViewController:sampleView animated:YES];
}
else{
// Cancel prompt
}
}
Am i missing something here?
If you don’t give the alert view some button titles, there won’t be any buttons to tap on and that delegate method won’t get called.
- (IBAction)showCurl:(id)sender {
alert1 = [[UIAlertView alloc] initWithTitle:#"Loading" message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert1 show];
}
Your code doesn't show a button
alert1 = [[UIAlertView alloc]initWithTitle:#"Loading" message:nil delegate:self cancelButtonTitle:**nil** otherButtonTitles:**nil**];
You pass nil as cancelButtonTitle AND nil as otherbuttonTitles, you should at least have one button title set.

Using an NSString as UIAlertView message:

I can't figure out why this won't work, I've tried it 100 ways. The AlertView shows up with a blank message. Here's my code:
eventChoiceNow = [[UIAlertView alloc] initWithTitle:#"Hurry Up!" message:timeTillRest delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
timeTillRest is an NSString. and just before calling the alertview an NSLog(#"%#",timeTillRest); displays the string without trouble.
Why would you use an alertview as an instance variable? There's no need for that. It's just as easy as this:
UIAlertView *eventChoiceNow = [[UIAlertView alloc] initWithTitle:#"Hurry Up!" message:timeTillRest delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[eventChoiceNow show];
[eventChoiceNow release];
This should work just fine. Testing using this code:
NSString *timeTillRest = #"Testing";
UIAlertView *eventChoiceNow = [[UIAlertView alloc] initWithTitle:#"Hurry Up!" message:timeTillRest delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[eventChoiceNow show];
And it works fine. Also testing using:
#import "ViewController.h"
#interface ViewController ()
#property(nonatomic,strong) NSString *timeTillRest;
#end
#implementation ViewController
#synthesize timeTillRest;
- (void)viewDidLoad
{
[super viewDidLoad];
timeTillRest = #"Testing";
UIAlertView *eventChoiceNow = [[UIAlertView alloc] initWithTitle:#"Hurry Up!" message:timeTillRest delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[eventChoiceNow show];
// Do any additional setup after loading the view, typically from a nib.
}
And that works flawlessly too. Make sure you aren't setting that property to nil anywhere.