1

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
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • 3
    There are plenty tutorials on this simple matter. F eks. [this](http://iosdeveloperzone.com/2013/11/17/tutorial-building-a-web-browser-with-uiwebview-revisited-part-1/) – David Karlsson Jan 07 '15 at 08:50

1 Answers1

2

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 !

Hemang
  • 26,840
  • 19
  • 119
  • 186
  • Thank you very much for your answer. This helped me, now it works perfectly. Still one question: I want to write an email in this app. There is an button named Contact or E-Mail. When you click on it, it should open an email window in my app to write an email to me. I hope you understand me? (I have Xcode 6.) –  Jan 07 '15 at 11:52
  • @David, Glad I could help you! You need to add `MessageUI.framework` in your project and use `MFMailComposeViewController` to send an email from your app. [Here's a good article (I think)](http://www.android-ios-tutorials.com/ios/ios-send-email-programmatically-with-attachment/) on how to send an email (with attachments) in iOS? Please check it out, it should surely help you. Good luck :) – Hemang Jan 07 '15 at 12:58
  • "and connect it with the Action (declared in the ViewController with IBAction)": What does it mean? Which action. Do you know it? –  Jan 07 '15 at 13:12
  • If you've a `IBOutlet` of a `UIButton` then you can bind an action (`IBAction`) of it so when user tap on it, that action will be call. – Hemang Jan 07 '15 at 13:40
  • If this is what you can't understand, don't worry! Just add a button, the way you've added back and forward button, and add an action for that, like the way you've added action for it. – Hemang Jan 07 '15 at 13:43