I have implemented a browser in my application by using UIWebView, by default I'm loading google page in my browser.
When I search something in the google page ,the UIWebViewDelegate's webView:shouldStartLoadWithRequest:navigationType: method is called.
The problem is when I tap on the back button from this search page no delegates are getting called, so I am having a problem disabling my back button.
This problem happens only in an iPad application not in an iPhone application.
This code may help u...
A UIWebView is a UIView that can load a web page while remaining in the user's application.
Navigation to other webpages is allowed through the use of imbedded links in a web page itself. Forward and backward navigation through history can be set up with instance methods goForward and goBack, but the programmer must supply the buttons.
The following example uses a UIWebView, and
1) adds forward and backward buttons. The buttons are enabled and highlighted using UIWebViewDelegate optional methods webViewDidStartLoad: and webViewDidFinishLoad:
2) adds a UIActivityIndicatorView which displays while the web page is loading
In the .h file for the WebViewController :
Declare the UIWebView, Optionally : add buttons to control moving forward and backward through browsing history and IBActions for pressing the buttons, Optionally again : add a UIActivityIndicatorView.
#interface WebViewController : UIViewController <UIWebViewDelegate>
{
UIWebView *webView;
UIButton *back;
UIButton *forward;
UIActivityIndicatorView *activityIndicator;
}
#property(nonatomic,retain)IBOutlet UIWebView *webView;
#property(nonatomic,retain)IBOutlet UIButton *back;
#property(nonatomic,retain)IBOutlet UIButton *forward;
#property(nonatomic,retain)IBOutlet UIActivityIndicatorView *activityIndicator;
-(IBAction)backButtonPressed: (id)sender;
-(IBAction)forwardButtonPressed: (id)sender;
#end
//In the .m file for the WebViewController
#implementation WebViewController
#synthesize webView;
#synthesize back;
#synthesize forward;
#synthesize activityIndicator;
//method for going backwards in the webpage history
-(IBAction)backButtonPressed:(id)sender {
[webView goBack];
}
//method for going forward in the webpage history
-(IBAction)forwardButtonPressed:(id)sender
{
[webView goForward];
}
//programmer defined method to load the webpage
-(void)startWebViewLoad
{
//NSString *urlAddress = #"http://www.google.com";
NSString *urlAddress = #"http://cagt.bu.edu/page/IPhone-summer2010-wiki_problemsandsolutions";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
}
// acivityIndicator is set up here
- (void)viewDidLoad
{
//start an animator symbol for the webpage loading to follow
UIActivityIndicatorView *progressWheel = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
//makes activity indicator disappear when it is stopped
progressWheel.hidesWhenStopped = YES;
//used to locate position of activity indicator
progressWheel.center = CGPointMake(160, 160);
self.activityIndicator = progressWheel;
[self.view addSubview: self.activityIndicator];
[self.activityIndicator startAnimating];
[progressWheel release];
[super viewDidLoad];
//call another method to do the webpage loading
[self performSelector:#selector(startWebViewLoad) withObject:nil afterDelay:0];
}
- (void)dealloc
{
[webView release];
[back release];
[forward release];
[activityIndicator release];
[super dealloc];
}
#pragma mark UIWebViewDelegate methods
//only used here to enable or disable the back and forward buttons
- (void)webViewDidStartLoad:(UIWebView *)thisWebView
{
back.enabled = NO;
forward.enabled = NO;
}
- (void)webViewDidFinishLoad:(UIWebView *)thisWebView
{
//stop the activity indicator when done loading
[self.activityIndicator stopAnimating];
//canGoBack and canGoForward are properties which indicate if there is
//any forward or backward history
if(thisWebView.canGoBack == YES)
{
back.enabled = YES;
back.highlighted = YES;
}
if(thisWebView.canGoForward == YES)
{
forward.enabled = YES;
forward.highlighted = YES;
}
}
#end
/*****************************/
//In viewDidLoad for the class which adds the WebViewController:
WebViewController *ourWebVC = [[WebViewController alloc] initWithNibName:#"WebViewController" bundle:nil];
ourWebVC.title = #"WebView";
[self.view addSubview:ourWebVC];
//release ourWebVC somewhere else
In your case ,You have to ignore/avoid "caching data". Following lines of code may help.
NSURLRequest *requestObj = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com"] cachePolicy: NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0];
[webView loadRequest:requestObj];
Related
I have a small app for iOS that uses WebView.
When I open a PDF file from WebView, I can't close the window or go back. How to add the button back or close, when the PDF file is opened.
URL to PDF file | PDF file is opened
#import "HomeController.h"
#interface HomeController ()
#end
#implementation HomeController
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *myURL = [NSURL URLWithString:#"http://test.mysite.com"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[myWebView loadRequest:myRequest];
}
#end
EDITED
//I create global BOOL isPdf in header file
#property (nonatomic, assign) BOOL isPdf;
//I make request in view did load
myWebView.delegate=self;
NSURL *myURL = [NSURL URLWithString:#"http://test.mysite.com"];
_isPdf = [myURL.lastPathComponent isEqualToString:#".pdf"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[myWebView loadRequest:myRequest];
NSLog(#"Run WebView with the URL");
//And then in my webview delegate method
- (void)webViewDidFinishLoad:(UIWebView *)webView{
if (_isPdf) {
UIBarButtonItem *backButton=[[UIBarButtonItem alloc]initWithTitle:#"Back" style:UIBarButtonItemStyleDone target:self action:#selector(backButtonPressed)];
self.navigationItem.rightBarButtonItem=backButton;
NSLog(#"Show back button %i", _isPdf);
}else{
self.navigationItem.rightBarButtonItem=nil;
NSLog(#"Not show back button %i", _isPdf);
}
}
// Add backButtonPressed method
-(void)backButtonPressed{
//update the method according to your need
if (myWebView.canGoBack) {
[myWebView goBack];
}else{
[self.navigationController popViewControllerAnimated:true];
}
}
You need to do three things
Confirm the webview delegates
Implement webview delegate webviewDidFinishLoad methods
Create a methods for the button action
First confirm the webview delegate in view did load
webview.delegate=self //(don't forgot to add the webview protocol <UIWebviewDelegate>)
Second implement webview delegate
-(void)webViewDidFinishLoad:(UIWebView *)webView{
if (webView.canGoBack) {
UIBarButtonItem *backButton=[[UIBarButtonItem alloc]initWithTitle:#"Back" style:UIBarButtonItemStyleDone target:self action:#selector(backButtonPressed)];
self.navigationItem.rightBarButtonItem=backButton;
}else{
self.navigationItem.rightBarButtonItem=nil;
}
}
Third add backButtonPressed method
-(void)backButtonPressed{
//update the method according to your need
if (webview.canGoBack) {
[webview goBack]
}else{
[self.navigationController popViewControllerAnimated:true];
}
}
Edit
if you want to show button only on pdf then when you make request like your doing in view did load create a global BOOL isPdf and set it when you load request like
NSURL *myURL = [NSURL URLWithString:#"http://test.mysite.com"];
isPdf=[myURL.lastPathComponent isEqualToString:#".pdf"];
and then in your webview delegate method :-
-(void)webViewDidFinishLoad:(UIWebView *)webView{
if (isPdf) {
UIBarButtonItem *backButton=[[UIBarButtonItem alloc]initWithTitle:#"Back" style:UIBarButtonItemStyleDone target:self action:#selector(backButtonPressed)];
self.navigationItem.rightBarButtonItem=backButton;
}else{
self.navigationItem.rightBarButtonItem=nil;
}
}
I'm a newbie in iOS programming, i am trying to make an iOS library that can be useful on my future applications. The app will have a button that will call the library and will load a website(the address link will come from the application).
I tried searching but none of it is working.
WebLibrary.h
#import <Foundation/Foundation.h>
#interface WebLibrary : NSObject
- (void)showUIWebView:(NSURL*)urlToOpen
{
/* UIViewController *myVC = [self.navigationController.viewControllers lastObject];
//This is your last view in the navigationController hierarchy.
UIWebView *newWebView = [[UIWebView alloc] initWithFrame:myVC.view.frame];
[myVC.view addSubview:newWebView];
*/
}
#end
WebLibrary.m
#import "WebLibrary.h"
#implementation WebLibrary
/* -(void) showUIWebView:(NSURL*)urlToOpen
{
//some codes here
}
*/
#end
If you want to load an URL on your webview, you need to call the loadRequest: method to perform it, example:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:_urlPath]];
[request addValue:#"YES" forHTTPHeaderField:#"Mobile-App"];
[_webView loadRequest:request];
You can add the webview to your viewcontroller in viewDidLoad method:
-(void) viewDidLoad{
[super viewDidLoad];
//custom your view
_webView = [[UIWebView alloc] initWithFrame: self.view.frame];
_webView.scalesPageToFit = YES;
[self.view addSubView: _webView];
}
You should read about UIViewController and the methods in it to understand clearly.
I want to load a URL on a UIViewController with an UIWebView and UIActivityIndicatorView, but UIActivityIndicator never appears and UIWebView never loads the URL.
This is my code:
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.title = #"Web";
[self displayURL:[NSURL URLWithString:#"(Website)"]];
}
-(void) webViewDidFinishLoad:(UIWebView *)webView {
[self.loadView stopAnimating];
self.loadView.hidden = YES;
}
-(void) displayURL:(NSURL *) aURL {
self.web.delegate = self;
self.loadView.hidden = NO;
[self.loadView startAnimating];
[self.web loadRequest:[NSURLRequest requestWithURL:aURL]];
}
If you add your webView by code, you need add
[self.view addSubview: web];
after you created webView, and of course you need to add UIActivityIndicatorView as a subView of webView or superView, so you need to add [web addSubview: loadView] or [self.view insertSubview: loadView aboveSubview: web];.
And about the URL, if your website URL is like www.google.com, you need to change #"(Website)" to the real URL. If your website is a file copied in your project, you need to change [NSURL URLWithString: ]; to [NSURL fileURLWithPath:[NSBundle mainBundle][pathForResource: ofType:]];.
Really poorly formulated question.
UIWebViewController does not work
UIWebViewController does not exists, you are talking about a UIWebView in a UIViewController
Code is very incomplete
I created an iOS app in Xcode. There are four ViewControllers in my app. You can navigate through the app by tapping the buttons in the bottom toolbar.
How can I prevent a ViewController from reloading when visiting it again?
So there are a couple of ways to keep track of whether the view has been loaded or not. One way is to create singleton and add several boolean properties to monitor whether the view has been loaded. Another way is to us NSUserDefaults to store a property once it has been loaded the first time. If you go that route then here is what the code would look like in your viewDidLoad method:
if (![[[NSUserDefaults standardUserDefaults] objectForKey:#"homeLoadFlag"] isEqualToString:#"YES"]) {
NSURL *url=[NSURL URLWithString: #"http://google.com"];
NSURLRequest * requestURL=[NSURLRequest requestWithURL:url];
[_homewebview loadRequest:requestURL];
[[NSUserDefaults standardUserDefaults] setObject:#"YES" forKey:#"homeLoadFlag"];}
This place a wrapper around your call to load the webView and will only make the call when it's loaded the first time.
This largely depends on what your view controllers contain so it's hard to answer without seeing your code. That said, rather than 'presenting' the view controllers another option would be to hide/unhide the views (or animating them on and off the screen). You could do this with four view controllers or with four views managed by a single controller.
There are a couple of ways of going about this. Personally I would have one viewController that controls all four views. If you are using a storyboard then I would have the toolbar at the bottom and then you can either 1) add the three webviews and the textview to the storyboard - or 2) you can one of the views and then create the other three views programatically. If you went the later route you could simply place the one web view (we'll call it webView1) on the storyboard and then override the viewDidLayoutSubviews and add the lines
-(void)viewDidLayoutSubviews {
CGRect viewFrame=webView1.frame;
UIWebView *webView2=[[UIWebView alloc] initWithFrame:viewFrame];
webView2.hidden=YES;
UIWebView *webView3=[[UIWebView alloc] initWithFrame:viewFrame];
webView3.hidden=YES;
UITextView *textView=[[UITextView alloc] initWithFrame:viewFrame];
textView.hidden=YES;
}
Then on your toolBar you can have your buttons unhide the view you want to show by changing the view property, for example, if you want to show the second webView you would simply say:
webView2.hidden=NO;
Here's the code of one ViewController.h/ViewController.m file:
//
// HomeViewController.h
// App_Single
//
//
#import <UIKit/UIKit.h>
#interface HomeViewController : UIViewController
#property (nonatomic, strong) IBOutlet UIWebView *homewebview;
#property (nonatomic, strong) IBOutlet UIWebView *website;
#end
//
// HomeViewController.m
// App_Single
//
//
#import "HomeViewController.h"
#import <SystemConfiguration/SystemConfiguration.h>
#import "Reachability.h"
#interface HomeViewController ()
#end
#implementation HomeViewController
- (BOOL)connected
{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
return !(networkStatus == NotReachable);
}
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url=[NSURL URLWithString: #"http://google.com"]; NSURLRequest * requestURL=[NSURLRequest requestWithURL:url]; [_homewebview loadRequest:requestURL];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
// Setting the swipe direction.
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
// Adding the swipe gesture on WebView
[_homewebview addGestureRecognizer:swipeLeft];
[_homewebview addGestureRecognizer:swipeRight];
if (![self connected])
{
// not connected
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Keine Internetverbindung vorhanden!" message:#"No network connection available!" delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil];
[alert show];
} else
{
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSUInteger)supportedInterfaceOrientations {
return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown | UIInterfaceOrientationMaskLandscape);
}
- (IBAction)goHomepage:(id)sender {
NSURL *url=[NSURL URLWithString: #"http://google.com"];
NSURLRequest *requestURL=[NSURLRequest requestWithURL:url];
[_website loadRequest:requestURL];
}
- (IBAction)openSearch:(id)sender {
NSURL *url=[NSURL URLWithString: #"http://google.com/search"];
NSURLRequest *requestURL=[NSURLRequest requestWithURL:url];
[_website loadRequest:requestURL];
}
- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
[_homewebview goForward];
}
if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
[_homewebview goBack];
}
}
#end
I have an iOS project with the following files inside it:
**PopOverContentViewController.h,
PopOverContentViewController.m,
MasterViewController.h,
MasterViewController.m,
DetailViewController.h,
DetailViewController.m.**
I created some buttons in PopOverViewController which have this method as their action:
- (void) buttonPressed
{
NSLog(#"The button was pressed");
UIWebView *myWebView = [[UIWebView alloc]
initWithFrame:self.detailViewController.view.bounds];
NSURL *myUrl = [NSURL URLWithString:#"http://www.lau.edu.lb"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myUrl];
[myWebView loadRequest:myRequest];
[self.detailViewController.view addSubview:myWebView];
if ([self isInPopover])
{
[self.myPopOver dismissPopoverAnimated:YES];
}
}
The problem is that I am not seeing the webpage opening, the DetailviewController doesn't change.
Keep in mind I have the following lines written in PopOverContentViewController.h:
#class DetailViewController;
#property DetailViewController *detailViewController;
And that I have imported DetailViewController.h to the implementation file of PopOverViewController.
I tried loading a page from information in the masterviewcontroller and it appeared in the detailviewcontroller, but I'm clueless as to why it isn't working from the popover.
Thank you!