I get the notification But my app is not running and i open the app through notification so i can't skip ViewController.
That is my code.
AppDelegate.m
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive)
{
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]];
if([[NSUserDefaults standardUserDefaults] valueForKey:#"username"] && [[NSUserDefaults standardUserDefaults] valueForKey:#"password"] && [[NSUserDefaults standardUserDefaults] valueForKey:#"companyid"])
{
HomeViewController *hm= [storyBoard instantiateViewControllerWithIdentifier:#"HomeView"];
self.window.rootViewController = hm;
}
else
{
NSLog(#"LoginView");
}
}
else
{
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]];
if([[NSUserDefaults standardUserDefaults] valueForKey:#"username"] && [[NSUserDefaults standardUserDefaults] valueForKey:#"password"] && [[NSUserDefaults standardUserDefaults] valueForKey:#"companyid"])
{
HomeViewController *hm= [storyBoard instantiateViewControllerWithIdentifier:#"HomeView"];
self.window.rootViewController = hm;
}
else
{
NSLog(#"LoginView");
}
}
}
Related
i'm try to never show user login view if he logged in
i do this in viewWillAppear
-(void)viewWillAppear:(BOOL)animated
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"userName"]) {
NSDictionary *d = [defaults objectForKey:#"currentUser"];
UserProfile *userData = [[UserProfile alloc]initWithDictionary:d];
[[NetworkModel sharedManager] setCurrentUser:hh];
[self loginResults];
}
self.navigationController.navigationBarHidden = YES;
}
and loginResults function
-(void)childrenReceived:(NSNotification *) notification
{
[SVProgressHUD dismiss];
NSDictionary *userInfo = notification.userInfo;
NSArray *allKids = [userInfo objectForKey:#"children"];
RootViewController *sideBarRoot= [self.storyboard instantiateViewControllerWithIdentifier:#"root"];
sideBarRoot.children = allKids;
[self dismissViewControllerAnimated:YES completion:nil];
[self presentViewController:sideBarRoot animated:YES completion:nil];
}
check the below coding
#interface AppDelegate : UIResponder <UIApplicationDelegate>
-(void) didFinishLogin
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
CCKFNavDrawer*homeNavController = (CCKFNavDrawer*)[mainStoryboard instantiateViewControllerWithIdentifier:#"NavigationLoginID"];
[self.window makeKeyAndVisible];
self.window.rootViewController = homeNavController;
-(void)didFinishLogout
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UINavigationController *homeNavController = (UINavigationController*)[mainStoryboard instantiateViewControllerWithIdentifier:#"LoginNavigationID"];
LoginVC *objloginVC = (LoginVC*)[homeNavController topViewController];
objloginVC.delegate=self;
[self.window makeKeyAndVisible];
self.window.rootViewController = homeNavController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
if( [[[NSUserDefaults standardUserDefaults]objectforkey:#"loginKey"]isequaltostring:#""])
[self didFinishLogout];
else
[self didFinishLogin];
I have an iOS application in which I am using the push notification to notify the user when the correct answer is posted. If the application is open and if I click on the notification it goes to the specified controller. But if the application is closed and if I receive the notification it does not goes to the specified controller.
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(#"User Info %#",userInfo);
NSString* alertValue = [[userInfo valueForKey:#"aps"] valueForKey:#"badge"];
NSLog(#"my message-- %#",alertValue);
int badgeValue= [alertValue intValue];
// NSNumber *identifierString = [[[userInfo valueForKey:#"aps"]valueForKey:#"details"]valueForKey:#"identifire"];
// NSLog(#"identifierString %#",identifierString);
NSString *alertMessage = [[userInfo valueForKey:#"aps"] valueForKey:#"alert"];
NSLog(#"ALertMessage %#",alertMessage);
if ([alertMessage isEqualToString:#"New answer added."]) {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:badgeValue];
// [[NSUserDefaults standardUserDefaults] setObject:[[[[userInfo valueForKey:#"aps"] valueForKey:#"details"] objectAtIndex:0]valueForKey:#"question"]forKey:#"notificationQuestion"];
// [[NSUserDefaults standardUserDefaults] setObject:[[[[userInfo valueForKey:#"aps"] valueForKey:#"details"] objectAtIndex:0]valueForKey:#"user_image"]forKey:#"notificationImage"];
// NSLog(#"Image %#",[[NSUserDefaults standardUserDefaults]valueForKey:#"notificationQuestion"]);
NSLog(#"User Information %#",userInfo);
NSLog(#"User Details %#",[[userInfo valueForKey:#"aps"] valueForKey:#"details"]);
// NSLog(#"User Details 1%#",[[[userInfo valueForKey:#"aps"] valueForKey:#"details"] objectAtIndex:0]);
// NSLog(#"User Details 1%#",[[[[userInfo valueForKey:#"aps"] valueForKey:#"details"] objectAtIndex:0]valueForKey:#"question"]);
pushDictonary = [[userInfo valueForKey:#"aps"] valueForKey:#"details"];
}
//NSArray *pushDetails = [[userInfo valueForKey:#"aps"] valueForKey:#"details"];
// NSLog(#"Push Details %#",pushDetails);
if (application.applicationState == UIApplicationStateActive ) {
// UILocalNotification *localNotification = [[UILocalNotification alloc] init];
// localNotification.userInfo = userInfo;
// localNotification.soundName = UILocalNotificationDefaultSoundName;
// localNotification.alertBody = [[userInfo valueForKey:#"aps"] valueForKey:#"alert"];
// localNotification.fireDate = [NSDate date];
if ([alertMessage isEqualToString:#"New answer added."])
{
[self createNotificationViewwithUserDictionary:userInfo];
}
}
else if (application.applicationState == UIApplicationStateBackground)
{
NSLog(#"YES");
}
else if (application.applicationState == UIApplicationStateInactive)
{
NSLog(#"YES");
if ([alertMessage isEqualToString:#"New answer added."])
[self pushdetailsViewController];
}
For going to the controller :
-(void)pushdetailsViewController
{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
QPYourQuestionController *controller = (QPYourQuestionController*)[mainStoryboard instantiateViewControllerWithIdentifier: #"yourQuestion"];
[[QPCommonClass initializeUserDefaults]setObject:[pushDictonary valueForKey:#"question_id"] forKey:#"currentquestionID"];
NSLog(#"Dictionary %#",pushDictonary);
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
[navigationController pushViewController:controller animated:YES];
}
For the situation when application is closed the remote notification delegate
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
is not called
In this case the call goes to the following method:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Hence you can do this to take the user to the VC you want
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// ...
if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {
// Do whatever you want
}
}
add this code to your app delegate file
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
//you will get notification data in userInfo dict
UIStoryboard * mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
notificationViewController * notificationVC = [mainStoryboard instantiateViewControllerWithIdentifier: #"notificationViewController"];//set your controller here
[(UINavigationController *)self.window.rootViewController pushViewController:notificationVC animated:YES];
completionHandler(UIBackgroundFetchResultNewData);
}
Here the code I have under the viewController.m
This code will run when a user selects a viewController
-(void)switchViews {
UIStoryboard *mainStory = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [mainStory instantiateViewControllerWithIdentifier:schoolName];
[self presentModalViewController:vc animated:YES];
NSUserDefaults *defaultViewController = [NSUserDefaults standardUserDefaults];
[defaultViewController setObject:nil forKey:#"save"];
[defaultViewController synchronize];
}
This code will run on the second time the app is launched
-(void)loadNewView {
UIStoryboard *mainStory = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
NSUserDefaults *newUserDefault = [NSUserDefaults standardUserDefaults];
NSString *newVC = [NSString stringWithFormat:#"save", schoolName];
UIViewController *newViewController = [mainStory instantiateViewControllerWithIdentifier:newVC];
[self presentModalViewController:newViewController animated:YES];
}
Keeping in mind that schoolNameis a string
How can I run [self loadNewView] under the viewDidLoad but running it on the second time the app is launched?
You should do something like
id value = [[NSUserDefaults standardUserDefaults] objectForKey:#"save"];
if (value) {
UIViewController *vc = [mainStory instantiateViewControllerWithIdentifier:value];
[self presentModalViewController:vc animated:YES];
} else {
// first time logic
}
in your AppDelegate or wherever you have your navigation logic. You should not have that logic in viewDidLoad.
-(void)viewDidLoad {
[super viewDidLoad];
NSString *launchCount = #"LaunchCount";
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSInteger count;
if([userDefaults objectForKey:launchCount]) {
count = [userDefaults integerForKey:launchCount];
}
else {
count = 0;
}
count++; //increment the launch count
[userDefaults setObject:[NSNumber numberWithInt:count] forKey:launchCount];
[userDefaults synchronize];
if([userDefaults integerForKey:launchCount] >= 2) {
// Do your thang
}
}
on ViewDidLoad function do something like this
if (![[NSUserDefaults standardUserDefaults] boolForKey:#"HasLaunchedFirst"])
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"HasLauncheFirst"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self switchViews];
}
else
{
[[NSUserDefaults standardUserDefaults] setBool:No forKey:#"HasLauncheFirst"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self loadNewView];
}
I am new to iPhone developing , My app is based on video conferencing, I have a task where an app should launch from browser using URL schemes. I have done it but problem is when app launched from browser it should load particular view controller. I am using Storyboard. Here is the code which I tried.
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
if (!url) { return NO;
}
NSString *URLString = [url absoluteString];
[[NSUserDefaults standardUserDefaults] setObject:URLString forKey:#"url"];
[[NSUserDefaults standardUserDefaults] synchronize];
URLParser *parser = [[URLParser alloc] initWithURLString:URLString];
username = [parser valueForVariable:#"USERNAME"];
NSLog(#"%#", username); //b
sessid = [parser valueForVariable:#"SESSION_ID"];
NSLog(#"%#", sessid); //(null)
tokenid = [parser valueForVariable:#"token"];
NSLog(#"%#", tokenid); //yes
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
ViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"VideoController"];
return YES;
}
This is how I do it..
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
//get parameters
[self goResetPassword:dict];
}
return YES;
}
- (void) goResetPassword:(NSDictionary*) dict{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UINavigationController *root = [[UINavigationController alloc]initWithRootViewController:[storyboard instantiateViewControllerWithIdentifier:#"resetPasswordView"]];
self.window.rootViewController= root;
ResetPasswordViewController *vc = (ResetPasswordViewController*)[[root viewControllers] objectAtIndex:0];
[vc loadData:dict];
}
hope it helps... GL HF
According to your comment your initial ViewController is NavigationController. This is how you can push the required ViewController.
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
// same code
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
// Get instance of initial Viewcontroller from storyboard
UINavigationController *navController = [storyboard instantiateInitialViewController
];
// Get instance of desired viewcontroller
ViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"VideoController"];
// Push ViewController on to NavigationController
[navController pushViewController:viewController animated:NO];
return YES;
}
Check It
UIStoryboard *board = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
UINavigationController *nav = [[UINavigationController alloc] init];
[nav setNavigationBarHidden:NO];
IntroVC *initailView = [board instantiateViewControllerWithIdentifier:#"Intro"];
[nav pushViewController:initailView animated:YES];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
UIApplication *sharedApplication = [UIApplication sharedApplication];
[sharedApplication unregisterForRemoteNotifications];
I'm trying to create a "initial setup page" that is shown if the app is launched for the first time on the device.
I made it like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"HasLaunchedOnce"])
{
NSLog(#"not first launch");
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
else
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"HasLaunchedOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(#"first launch");
}
}
Now I want to create a view controller and push to this view controller if it's the first time the app is launched.
What do I need to do?
Create a new ViewController. Import the header in appDelegate.h file also create a instance variable of that class with name initialViewController.
Change your else condition like:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"HasLaunchedOnce"])
{
NSLog(#"not first launch");
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
}
else
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"HasLaunchedOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
self.initialViewController = [[InitialViewController alloc] initWithNibName:#"InitialViewController" bundle:nil];
self.window.rootViewController = self.InitialViewController;
NSLog(#"first launch");
}
[self.window makeKeyAndVisible];
return YES;
}