0

Ready to make a fool out of myself:

I have this skeleton app, that has the PushWoosh notification classes in place. It works fine. I'm able to send a push message to my app.

For this to work, in my AppDelegate, there is a method called

- (void) onPushAccepted:(PushNotificationManager *)pushManager withNotification:(NSDictionary *)pushNotification

that allows me to fire off stuff when a notification is accepted.

Meanwhile, in my ViewController, I have a method like this:

-(void)loadURL{
    NSLog(@"testing");
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:TOPIC]]];
    [webView reload];
}

This one works fine when called from the ViewController itself.

However, when I try to call this method from within the 'onPushAccepted' method in the appDelegate, the webView does not show the desired URL, although, as indicated by the logging, the method IS called.

I guess this shows that I'm lacking some fundamental understanding of the working of this all.

Therefore, I would be satisfied with some some strings that would make this work, but I would be really happy with an explanation on the why and how behind it.

I tried putting the onPushAccepted: in the ViewController, but that didn't work at all, although I included the necessary "PushNotificationManager.h" in the ViewController.m.

I'm confused, and need your help.

I think your answer will get me close to getting the basics.

Thanks ahead!

shader
  • 2,121
  • 1
  • 13
  • 20
Sjakelien
  • 2,255
  • 3
  • 25
  • 43
  • Put a breakpoint in the method and check the value of `webview` in the variable inspector. It's likely that `webview` is `nil` – Hugo Tunius Jan 03 '14 at 22:06
  • OK, and if so, what should I do? – Sjakelien Jan 03 '14 at 22:08
  • How do you call `-(void)loadURL` from AppDelegate? – Hugo Tunius Jan 03 '14 at 22:12
  • ViewController * vc = [[ViewController alloc]init]; [vc loadURL]; – Sjakelien Jan 03 '14 at 22:14
  • You cannot just create an instance of your `ViewController` with `init`. Is the `ViewController` loaded from a nib or storyboard? Or are you creating it with code. In any case you are going to want to call `- (void)loadURL` on the instance of `ViewController` that is displayed on the screen therefore creating a new one on the fly will not work. – Hugo Tunius Jan 03 '14 at 22:17
  • My viewController is part of the storyboard. The [init] part doesn't make sense to me either. However, as said, the NSLog(@"testing") shows up in my console. Somehow, the webView is not responding. – Sjakelien Jan 03 '14 at 22:20
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/44454/discussion-between-sjakelien-and-hugo-tunius) – Sjakelien Jan 03 '14 at 22:21

1 Answers1

0

In this specific case Sjakelien was using a Single View Application. The confusion was that the attempt to instantiate the ViewController in AppDelegate by doing the following ViewController * vc = [[ViewController alloc]init]; [vc loadURL]; did not work.

In this case the solution is the get the ViewController that is displayed on screen by using

- (void) onPushAccepted:(PushNotificationManager *)pushManager withNotification:(NSDictionary *)pushNotification {
  ViewController *vc = (ViewController*)self.window.rootViewController;
  [vc loadURL];
}

Applications with a different setup such as a UINavigationController need to take different actions.

A few choices:

  • popToRootViewController and instantiate a new instance of ViewController and push it onto the navigation stack

  • Push a new instance of ViewController onto the navigation stack without using popToRootViewController

  • Present the ViewController in a modal
  • Modify the model of the application
Charlie
  • 11,380
  • 19
  • 83
  • 138
Hugo Tunius
  • 2,869
  • 24
  • 32
  • Thank you, Hugo. For those people that have the same pathetic level of programming skills: this all means, that, rather than creating a new View, you need to crawl all the way up to the existing View to send your message. Next step for me: pay more attention to the existence of the rootViewController, and the whole paradigm surrounding that phenomenon. I'm deeply embarrassed. – Sjakelien Jan 03 '14 at 23:10