2

The notification I receive contains an url to open in a UIWebView. But I cannot access the UIWebview from my AppDelegate (this is where is receive the notification).

-(void) application:(UIApplication *)application didReceiveRemoteNotification(NSDictionary* ) userInfo{
      // handle notification (this works)
      //HERE I want to call my UIWebView and load the url I got.
}
tkanzakic
  • 5,499
  • 16
  • 34
  • 41

3 Answers3

1

One way to do this is to post a notification that your UIWebView receives. Check out the NSNotificationCenter Class Reference or this example on SO.

Community
  • 1
  • 1
tilo
  • 14,009
  • 6
  • 68
  • 85
1

In my app I have used the following, this is when the UIWebView is already on the screen:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    NSString *urlString = [userInfo objectForKey:@"url"];
    if( [self.window.rootViewController isKindOfClass:[UINavigationController class]] ){
        UINavigationController *currentNavigationController = (UINavigationController*)self.window.rootViewController;
        if( [currentNavigationController.visibleViewController isKindOfClass:[NHCallVC class]] ){
            SomeViewController *currentViewController = (SomeViewController*)currentNavigationController.visibleViewController;
            [currentViewController.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
        }
    }
}

The structure with the UINavigationController seems complex, but this was needed for the way I set up my app. This can be different in your app.

The idea is to get the viewcontroller that is opened and load a URL on the UIWebView. The code is assuming the UIViewController with the UIWebView is currently open. The code should be altered if you want to navigate to the correct UIViewController before opening the url in the UIWebView.

Roland Keesom
  • 8,180
  • 5
  • 45
  • 52
0

Place an observer to the view where UIWebView resides such as:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recievedNotification:) name:@"ReceivedNotification" object:nil];

Write the appropriate code within recievedNotification function which changes the UIWebView's target url.

And post a notification in didReceiveRemoteNotification function in APP Delegate such as:

    [[NSNotificationCenter defaultCenter] postNotificationName:@"ReceivedNotification" object:nil];

Good luck.

Can Leloğlu
  • 271
  • 1
  • 4
  • 11