0

I have a UIWebView in my ViewController that I want to update when the UIApplicationDelegate is passed a URL, like

myApp://?changeWebView=newstuff

I am building the new URL fine based on the data I am passing in but I can't figure out how to update my UIWebView

I have this in my AppDelegate.m:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    NSLog(@"Calling Application Bundle ID: %@", sourceApplication);
    NSLog(@"URL scheme:%@", [url scheme]);
    NSLog(@"URL query: %@", [url query]);
    NSLog(@"URL hash: %@", [url fragment]);

    NSURL *newURL = [NSURL URLWithString:@"URL constructed with pieces of passed URL data"];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:newURL];


 //now I want to update the UIWebview defined in my ViewController.h

    return YES;
}
sunbabaphu
  • 1,473
  • 1
  • 10
  • 15
murphy52
  • 3
  • 1

2 Answers2

2

You can load the request in webView by using loadRequest

[self.myWebView loadRequest:requestObj]; put this line in your code

You can post notifications to let know your view controller that you can load webview.

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    NSLog(@"Calling Application Bundle ID: %@", sourceApplication);
    NSLog(@"URL scheme:%@", [url scheme]);
    NSLog(@"URL query: %@", [url query]);
    NSLog(@"URL hash: %@", [url fragment]);

    NSURL *newURL = [NSURL URLWithString:@"URL constructed with pieces of passed URL data"];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:newURL];

      //Post Notification
      [[NSNotificationCenter defaultCenter] postNotificationName:@"loadRequest"
                                  object:nil
                                  userInfo:@{@"requestObj":requestObj}];

    return YES;
}

Now in add notification observer in your viewController

-(void)viewDidLoad{

        //Add observer for notification
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveEvent:) name:@"loadRequest" object:nil];
}

Write this method to load request in webView in your ViewController

- (void)receiveEvent:(NSNotification *)notification {
    // handle event
        NSURLRequest *requestObj = notification.userInfo[@"requestObj"];
       [self.myWebView loadRequest:requestObj];         
}
codester
  • 36,891
  • 10
  • 74
  • 72
  • This won't work. He receives the notification in his app delegate but the web view is in the view controller. – AdamPro13 Jul 17 '14 at 21:08
0

For starters, since you're doing this in the app delegate, you don't know that the view controller with the web view will be allocated. Because of this, you have a couple different options.

  • First, you could create a property (@property (nonatomic, weak) UIViewController *viewController;) in your app delegate. When your view controller loads, set this property to the view controller with the web view so you have a reference to it in the app delegate.
  • Second, you can send an NSNotification that also sends the url. The benefit to this is down the road you may want to send this url to many different classes, which NSNotification allows. Each class can individually add themselves as observers for the notification. Here's an example: Send and receive messages through NSNotificationCenter in Objective-C?
Community
  • 1
  • 1
AdamPro13
  • 7,232
  • 2
  • 30
  • 28