user2966615
user2966615

Reputation: 383

Open specific tab and launch a specific url in uiwebview when receive a push notification

i am developing an app that will receive Push notifications and when user hits view on notification, app launches and there are 3 uitabs in it and indexed as 0,1 and 2. My required tab, when notification received is second one whose index is 1. And when tab 1 openes it will detect that if this tab is open in response of a push notification then UIWebview will launch a different URL. Otherwise it will act as default. I am posting all my code please help me with this. Thanks in advance :)

Here is my didReceiveRemotenotification method, I am creating and saving a integer as a flag to make decision. - AppDelegate.m

(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    int flg = 1;
    [[NSUserDefaults standardUserDefaults] setInteger:flg forKey:@"flagvlu"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];
}

then it loads app and open first view which is Main.m - Main.m

(void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushNotificationReceived) name:@"pushNotification" object:nil];
}
-(void)pushNotificationReceived{
    [self.tabBarController setSelectedIndex:1];
}

and it passes app to next tab(Required.m) which is my required tab. in which i have a uiwebview.

Required.m

- (void)viewDidLoad
{
  [self doYourStuff];
  [super viewDidLoad];
}
-(void)doYourStuff{
  int flg = [[NSUserDefaults standardUserDefaults] integerForKey:@"flagvlu"];
   if (flg == 1) {
     [webPage reload];
     [webPage loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]];
     flg = 0;
     [[NSUserDefaults standardUserDefaults] setInteger:flg forKey:@"flagvlu"];
     [[NSUserDefaults standardUserDefaults] synchronize];
   }
   else if(flg == 0){
     [webPage reload];
     [webPage loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.yahoo.com"]]];
   }
}

*Now the the problem is that app receives a notification and when user taps on view, it launches app and opens required tab perfectly but not launching desired URL in webview. :( please help i am new in objective c development.

Upvotes: 0

Views: 895

Answers (2)

sergio
sergio

Reputation: 69027

One way of doing what you want is calling doYourStuff in viewWillAppear instead of viewDidLoad.

viewDidLoad is called on view creation, typically when the view is added as a subview to another view.

viewWillAppear is called when the view becomes visible.

So, viewDidLoad is called when you populate your tab bar; viewWillAppear is called when you make that view visible (i.e., by tapping on its tab bar icon).

A better approach to viewWillAppear would be, IMO, using the tab bar delegate method:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {

   if (<viewController is Required.m>)
       [(Required.m*)viewController doYourStyff];
}

which is called exactly once each time you tap on a tab bar item.

Upvotes: 1

Toseef Khilji
Toseef Khilji

Reputation: 17409

Try This without using NSNotificationCenter.

- (void)application:(UIApplication *)application  didReceiveRemoteNotification:(NSDictionary *)userInfo
{


        int flg = 1;
    [[NSUserDefaults standardUserDefaults] setInteger:flg forKey:@"flagvlu"];
    [[NSUserDefaults standardUserDefaults] synchronize];

   UITabBarController *tabb = (UITabBarController *)self.window.rootViewController;
   tabb.selectedIndex = 1;

}

now right doYourStuff in viewwillappear method.

Upvotes: 0

Related Questions