Search code examples
iosobjective-cxcodewebviewxcode5

Xcode 5 Loading a URL in a new View Controller with a WebView in it


It seems that all the methods in earlier versions of Xcode are not working for me.

What I have right now:
I have a main menu with a button that segues into another view with a different controller with a WebView in it (has a property so the URL to load can be set)

What I want: When I load that view from that button I want to pass a string from the first view to the new view that represents the URL and load it in the web view. Basically I want a generic WebView view that can load different URLs passed by the views that segue to it.

Here is the code for the WebView H file

#import <UIKit/UIKit.h>

@interface BRWebView : UIViewController
{
    IBOutlet UIWebView *requestWebView;

}
@property (strong, nonatomic) NSString *loadUrl;

@end

In the ViewController for the MainMenu there is this for the button listener

-(IBAction)requestButton:(id)sender
{
    BRWebView *web = [self.storyboard    instantiateViewControllerWithIdentifier:@"BRWebView"];
    web.loadUrl = @"Websitename.com";
    [self presentModalViewController:web animated:YES];

}

And in the BRWebView.m file there is this

- (void)viewDidLoad
{

    [super viewDidLoad];

    NSURL *url = [NSURL URLWithString:_loadUrl];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [requestWebView loadRequest:request];
}

Solution

  • EDIT after code in the question :

    In the ViewController replace your code by :

    - (IBAction)requestButton:(id)sender
    {
        BRWebView *web = [self.storyboard instantiateViewControllerWithIdentifier:@"BRWebView"];
        web.loadUrl = @"http://websitename.com";
        [self presentViewController:web animated:YES completion:nil];
    }
    

    and in the BRWebView.m remove your code in viewDidLoad and add viewWillAppear :

    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    
        // URL Request Object
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:[NSURL URLWithString:_loadUrl]];
    
        // Load the request in the UIWebView
        [_requestWebView loadRequest:requestObj];
    }
    

    Old answer :

    You need to pass your URL data with a property like this (without navigation controller) :

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([[segue identifier] isEqualToString:@"showDetail"]) 
        {
            // Get reference to the destination view controller
            ViewControllerB *viewControllerB = segue.destinationViewController;
    
            // Pass URL
            viewControllerB.webviewURL = @"http://myurl.com";
        }
    }
    

    with navigation controller :

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([[segue identifier] isEqualToString:@"showDetail"]) 
        {
            UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
            ViewControllerB *viewControllerB = (ViewControllerB *)navController.topViewController;
    
            // Pass URL
            viewControllerB.webviewURL = @"http://myurl.com";
        }
    }
    

    On your second view .h :

    @property (nonatomic, strong) NSString *webviewURL;
    

    second view .m :

    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    
        // URL Request Object
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:[NSURL URLWithString:_webviewURL]];
    
        // Load the request in the UIWebView
        [self.webView loadRequest:requestObj];
    }