iosobjective-ciphoneurlhome-button

Open an URL in UIWebView


I have an important question. I'm creating an iPhone app. I have an UIWebView (https://www.google.de/) with goBack, goForward and reload. Now, I want a Button e.g. Bar Button Item named Home or Homepage, to open an URL (https://www.google.de/) in this WebView, to go back to the homepage. How can I do this? How can I "say" the button to open the link in my WebView? I ask for answer.

//  ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (nonatomic, strong) IBOutlet UIWebView *site   ;
@end



//  ViewController.m

#import "ViewController.h"

@interface ViewController ()
@end
@implementation ViewController
-(IBAction)refresh:(id)sender; {

    NSURL *url=[NSURL URLWithString: @"https://www.google.de/"]; NSURLRequest * requestURL=[NSURLRequest requestWithURL:url]; [_site loadRequest:requestURL];

}
- (void)viewDidLoad {

    [self refresh:self];

    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

}
- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}
@end

Solution

  • UIWebView has methods, goBack and goForward. You can make actions and bind it with your bar buttons to go back and forward whenever you want.

    - (IBAction)goBack:(id)sender {
        [_site goBack];
    }
    
    - (IBAction)goForward:(id)sender {
        [_site goForward];
    }
    
    - (IBAction)goHomepage:(id)sender {
        NSURL *url=[NSURL URLWithString: @"https://www.google.de/"]; 
        NSURLRequest *requestURL=[NSURLRequest requestWithURL:url]; 
        [_site loadRequest:requestURL];
    }
    

    To help you more, here's a tutorial on this, and if you don't want to read that tutorial, you can check their source code from here !