How to handle APNS Message by clicking "View" button when the app is not in active? - iphone

Am new to use APNS in iPhone app. I have used and received the message in all scenarios. When the user clicks the "View" button from the APNS alert i should do the actions in the app and navigate the user to 3rd tab.
But when the app is in In Active (The app not in use) i can't trigger the actions in the app. I have used the below code in "didFinishLaunch" but not used.
NSString *params=[[launchOptions objectForKey:#"UIApplicationLaunchOptionsRemoteNotificationKey"] objectForKey:#"aps"];
NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
NSLog(#"remoteNotif : %#", remoteNotif);
if ([params length] > 0 )
{
UIAlertView *messageReceivedAlert = [[UIAlertView alloc] initWithTitle:#"My APP" message:#"New Message Recevied From APNS in IN-Active Stage" delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[messageReceivedAlert show];
[messageReceivedAlert release];
}
It always returns (null).
Can anyone please help me to do this? Thanks in advance.

Use this :
- (void) application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
And you can check for inactive by using
if([UIApplication sharedApplication].applicationState == UIApplicationStateInactive)

Related

Check if Notify Alert is Displayed - Bluetooth Notify On Disconnect

My iPhone app connects to a BLE Peripheral using this line of code:
[manager connectPeripheral:per.peripheral options:[NSDictionary dictionaryWithObject [NSNumber numberWithBool:YES]
forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]];
I enable the option to notify the user upon the peripheral disconnecting which will only happen when the app isn't being used (i.e. the iPhone is locked or the app is in the background)
When the peripheral disconnects, it calls a function in which I display an alert to the user telling them it disconnected:
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
//Show alert to the user
NSString *str = [[NSString alloc] initWithFormat:#"Peripheral disconnected. Try connecting again. Error: %#", error.localizedDescription];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Peripheral Disconnected"
message:str delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[self.navigationController popToRootViewControllerAnimated:NO];
}
The problem that I have is that when the iPhone is locked and the peripheral disconnects, both alerts pop up and it makes the user click "OK" on both alerts. How can I check to see if the "NotifyOnDisconnect" alert is being displayed to cancel showing the other alert?
You could avoid using CBConnectPeripheralOptionNotifyOnDisconnectionKey altogether and in your -centralManager:didDisconnectPeripheral: implementation, check if the app is in the background and present a UILocalNotification if it is in the background or your existing UIAlertView if the app is in the foreground. This way only one alert will be presented.

objective-c if NSURL fails display UIAlertView

I have this NSURL like so:
NSURL *url = [NSURL URLWithString:#"http://jamessuske.com/isthedomeopen/isthedomeopenGetData.php"];
and it works fine when I am connected to the internet. But if I am not (or if the user has no signal) I would want an UIAlertView to appear. I tried the following code below (with and without the if statement) but my app crashes.
if(!url){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Test Message"
message:#"This is a test"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
What Am I doing wrong?
What I mean by crashing, I mean my app opens and does not get the data from the URL. I do not see any errors, I just get brought to a page with this highlighter:
Thread 6
com.app.root.default-priority
- 0 ucol_getVersion
Next to it is a window with this hightlighted
0x17bbd32: movl 204(%ecx), %edx Thread 6: EXC_BAD_ACCESS (code=2 address=0xcc)
What you are doing only checks if the object url is a NULL pointer or not. If you want to check if a user has internet connection, I would recommend trying the Reachability classes, which can be found easily online. This one is very useful for me.
it is recommended to check the Internet availability if you are developing the app with internet connection requirement by the Apple and they are also providing the .h and .m files namely Reachability.h and Reachability.m so please download that files and import to your projects and there is a parameter named "internetConnectionStatus" and by that values use the below alert
if (internetConnectionStatus == NotReachable)
{
UIAlertView *responseAlert = [[UIAlertView alloc] initWithTitle:#"Network Error"
message:#"This application requires network connectivity."
delegate:self
cancelButtonTitle:#"Exit"
otherButtonTitles:#"Continue", nil];
[responseAlert show];
}
else
{ //do other stuff here
}
you can find the reachability files Here and you can use any Reachability files from github
ThnQ

iPhone to retrieve gmail account details in app from setting

I am making an application where I want to send an email using smtp server.
It's working fine but if user does not provide the correct gmail account details such as (email id and password) email is not send.
What I want to do is I want to access user gmail account details from setting tab of the iPhone into my app?
Is it possible to do retrieve the gmail account detail into the app?
Please help me out with this.
Following is my code:
-(IBAction)sendemail
{
SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init];
//testMsg.fromEmail = #"Lexi mobile";//nimit51parekh#gmail.com
testMsg.fromEmail = soundOn;
NSLog(#"str_Uname=%#",testMsg.fromEmail);
//str_info = [str_info stringByReplacingOccurrencesOfString:#"," withString:#""];
testMsg.toEmail = [string_emailinfo objectAtIndex:0];
NSLog(#"autoemail=%#",testMsg.toEmail);
testMsg.relayHost = #"smtp.gmail.com";
testMsg.requiresAuth = YES;
testMsg.login = soundOn;
NSLog(#"autoelogin=%#",testMsg.login);
testMsg.pass = vibrateOn;
NSLog(#"autopass=%#",testMsg.pass);
testMsg.subject = #"Photo Sharing";
testMsg.wantsSecure = YES;
NSURL *url = [[NSURL alloc]initWithString:#"http://itunes.apple.com/us/app/umix-radio/id467041430?mt=8"];
NSString *msg2 = [NSString stringWithFormat:#"<html><b>DOWNLOAD NOW</b>&nbsp&nbsp</html>",url];
NSString *sendmsg=[[NSString alloc]initWithFormat:#"%#",msg2];
NSLog(#"automsg=%#",sendmsg);
testMsg.delegate = self;
NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:#"text/plain",kSKPSMTPPartContentTypeKey,
sendmsg,kSKPSMTPPartMessageKey,#"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];
testMsg.parts = [NSArray arrayWithObjects:plainPart,nil];
[testMsg send];
// [self DeleteRowAfterSending];
//[self performSelector:#selector(DeleteRowAfterSending) withObject:nil afterDelay:5.0];
}
-(void)messageSent:(SKPSMTPMessage *)message
{
[message release];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Success" message:#"Your email is sent successfully" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
NSLog(#"delegate - message sent");
}
-(void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error
{
[message release];
// open an alert with just an OK button
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Fail" message:#"Message sending failure" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
NSLog(#"delegate - error(%d): %#", [error code], [error localizedDescription]);
}
What i want to do is i want to access user gamil account details from
setting tab of the iPhone into my app? is it possible to do retrive
the gamil account detail into the app?
No, and with good reason: that functionality would allow any developer access to anyone's e-mail account.
That would be a massive security hole.
Much like it is with Location Services, you're just going to have to ask the user to enter their e-mail address and password. And hope they want to trust your app with that kind of personal information. (Assuming Apple approves your app at all.)
Let the user give your app authorization from Google. check the oauth autorization with google api at their developers site?
https://developers.google.com/accounts/docs/OAuth2
As usual the user signs in with his account through google api and returns back to you an access_token to get information back from his account including his email which you need.

fire an alert when a UILocalNotification is called

I have a UILocalNotification and I want to fire an alert when the app recovers from background to foreground. I can create the alert in didReceiveLocalNotification, but this method can only be called when the app is active. Now I want to check if a notification was fired while the app is in background and then fire an alert when the app recovers.
this is my current method
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
application.applicationIconBadgeNumber = 0;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSLog(#"prefs %#",[prefs stringForKey:#"kTimerNotificationUserDef"]);
if([prefs stringForKey:#"kTimerNotificationUserDef"] != nil)
{
[prefs setObject:nil forKey:#"kTimerNotificationUserDef"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Timer Alert" message:[prefs stringForKey:#"kTimerNotificationUserDef"] delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}
the NSUserDefaults is set when I initialize the notification, however, this alert is called even though the notification did not arrive yet. So, Im assuming maybe I can do something like:
if([prefs stringForKey:#"kTimerNotificationUserDef"] != nil && didFireNotifFromBackground)
any suggestions? thanks!
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification {
notification.applicationIconBadgeNumber = 0;
NSString *reminderText = [notification.userInfo objectForKey:kRemindMeNotificationDataKey];
[viewController showReminder:reminderText];
}
show the alertview where class u want to show
- (void)showReminder:(NSString *)text
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Reminder" message:text delegate:nil cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
[alertView release];
}
It is in your settings. Settings ->Notification->Your application in right side-> Choose your alert style.
Then you get alert directly while firing local notification

UIAlertView, once the user opens the app. Shown once

I want a UIAlertView to show, once the user opens the app. It'll ask them for their email address. But i want it to only show once. So when the user re opens the app the uialertview shouldnt pop up. And the UIAlertView will contain 2 buttons within that. 'Dismiss' & 'Yes' .. the dismiss button will continue with the app. But the 'Yes' will take them to another view'.
Thanks :)
Edit:
- (void)viewDidLoad {
[super viewDidLoad];
if (![#"1" isEqualToString:[[NSUserDefaults standardUserDefaults] objectForKey:#"alert"]]) {
[[NSUserDefaults standardUserDefaults] setValue:#"1" forKey:#"alert"];
[[NSUserDefaults standardUserDefaults] synchronize];
UIAlertView *prompt = [[UIAlertView alloc] initWithTitle:#"Enter your email"
message:#"\n\n\n"
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Enter", nil];
textField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 50.0, 260.0, 25.0)];
[textField setBackgroundColor:[UIColor whiteColor]];
[textField setPlaceholder:#"enter email here"];
[prompt addSubview:textField];
[prompt show];
[prompt release];
//[textField becomeFirstResponder];
}
}
Okay this is the code at the moment, im stuck on how the email is going to be sent to be once the user presses enter.
You can use NSUserDefaults to save a boolean between sessions.
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html
// save
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:true] forKey:#"hasRunBefore"];
// load
[[NSUserDefaults standardUserDefaults] objectForKey:#"hasRunBefore"] boolValue];
You could store the email address in NSUserDefaults (I guess you are already doing it, if you ask for it only the very first time your app starts); each time the app starts, you check if the email address is there (NSUserDefaults); if not, you display the UIAlertView.
First, need to make yourself the delegate for the UIAlertView so you know when the user has pressed Cancel or Enter. So this:
UIAlertView *prompt = [[UIAlertView alloc] initWithTitle:#"Enter your email"
message:#"\n\n\n"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Enter", nil];
The delegate callback is this method:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
Once that's called, you can retrieve the email address from the textField instance you created and added to the alert view.