I have a master table and detail view controller. The detail VC shows URLs through a web view.
If using the built in navigation of the master/detail setup, if I go back and then press a different table cell, the detail view will update the web view. This is true whether I use a UIWebView or a WKWebView.
However, within the detail view, without having to go back to the table, I want to give the user the option to view more than one url through the same web view. I am trying to do this in viewwillappear by changing the url for the request. However, the UIWebView (or alternatively WKWebView) continue to show the same url. I can't seem to get rid of the first url.
I've tried all kinds of ways to close or stop the existing web view, clear the cache, delete cookies etc. before reloading the new one but still see the same url. Would appreciate any suggestions. Here is my code and some of the things I've tried that don't work:
-(void) changeURL: (NSNumber*)param {
NSURL *testurl1=[NSURL URLWithString:#"http://www.apple.com"];
NSURL *testurl2 =[NSURL URLWithString:#"http://www.google.com"];
//if param is 1 {
_urlToLoad = testurl1;}
else {
_urlToLoad = testurl2;}
}
Edit: Method below is updateInterface, not viewWillAppear
-(void) updateInterface {
UIWebView *webView;
NSURLRequest *request=[NSURLRequest requestWithURL:_urlToLoad];
webView = nil;//This blocks the web view from loading at all for some reason I can't understand. Setting wkWebView to nil does not block loading.
[webView stopLoading];//no effect
[webView loadRequest:request];
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:request];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
NSString *myDomain = #"www.google.com";
for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
if([[cookie domain] isEqualToString:myDomain]) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
}
//wkwebview version
WKWebView *wkwebView;
WKWebViewConfiguration *wkConfig = [[WKWebViewConfiguration alloc] init];
wkwebView = nil;
wkwebView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:wkConfig];
wkwebView.navigationDelegate = self;
[wkwebView loadRequest:request];
[webView addSubview:wkwebView];
CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
webFrame.origin.y = 100.0; // statusbar
webFrame.origin.y -= 100.0; // statusbar 20 px cut from y of frame
webView = [[UIWebView alloc] initWithFrame:webFrame];
}
The UIViewController method "viewWillAppear" has the following signature in Objective-C:
- (void)viewWillAppear:(BOOL)animated
, you implemented it without any arguments.
The result is that, instead of being taken as an override of the original method defined in the superclass UIViewController, it is treated as a brand new, custom method introduced by your subclass; and unless you call it explicitly somewhere, it will never execute.
Source: SDK Reference
Related
I have a screen that shows a web view. However, I allow people to cycle through different web views by updating an object. When I load the first page, the web view loads fine. However, when I try to update the page from the new object in viewwillappear, while other elements such as a title that goes above the webview load fine, the web view does not refresh.
//in viewWillAppear
self.title = self.file.imageDescript;//The title loads fine
NSString *urlBase = #"https://www.~com/images/";
NSString * imagename = self.file.imagename;
NSString *url = [NSString stringWithFormat:#"%#%#",urlBase,imagename];
NSURL *realUrl = [NSURL URLWithString:url];
NSURLRequest *request = [NSURLRequest requestWithURL:realUrl];
[webView loadRequest:request];
I have tried various versions of setNeedsDisplay and also applied these to subviews of the web view without success as in this method:
- (void) forceRedrawInWebView:(UIWebView*)webView {
NSArray *views = webView.scrollView.subviews;
for(int i = 0; i<views.count; i++){
UIView *view = views[i];
//if([NSStringFromClass([view class]) isEqualToString:#"UIWebBrowserView"]){
[view setNeedsDisplayInRect:webView.bounds]; // Webkit Repaint, usually fast
[view setNeedsLayout]; // Webkit Relayout (slower than repaint)
// Causes redraw & relayout of *entire* UIWebView, onscreen and off, usually intensive
[view setNeedsDisplay];
[view setNeedsLayout];
// break; // glass in case of if statement (thanks Jake)
//}
}
}
I have read in one SO answer that since IOS8.2, refresh of UIWebView no longer works (as WKWebView) has replaced it. I would switch to WKWebView except that it is not supported in storyboards, apparently, and I have a bunch of stuff going on in the storyboard.
Note: this has nothing to do with refreshing the same web url because content has changed on a server. It has to do with loading an entirely different web url in the UIWebView
Can anyone suggest way to fix this?
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'm trying to make it so that the back button will only be enabled when the user presses a link and/or if canGoBack = TRUE. I tried using an if statement in the body but that didn't work. I then tried using what I could find online and it still didn't work. My code is below. What am I doing wrong? Thanks!
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest: (NSURLRequest *)request navigationType: (UIWebViewNavigationType)navigationType{
if (navigationType == UIWebViewNavigationTypeLinkClicked)
{
NSString *url = webView.request.URL.absoluteString;
NSURL *nsurl = [NSURL URLWithString:url];
NSURLRequest *nsrequest = [NSURLRequest requestWithURL:nsurl];
[webView loadRequest:nsrequest];
backButton.enabled = TRUE;
}
return TRUE;
}
shouldStartLoadWithRequest() won't get called if you haven't set the web view's delegate.
In order to use UIWebView its typically set as an outlet property of a view controller, so you should have something like this:
IBOutlet UIWebView* webView;
The webView has a delegate property which needs setting to your view controller, you can set delegates in a storyboard, but its more explicit to set it in code:
- (void)viewDidLoad
{
[super viewDidLoad];
self.webView.delegate = self;
99% of the time you just set delegates, but UIWebView's delegate is funny in that it needs setting to nil when finished with. So add this:
- (void)viewDidUnload
{
[self.webView stopLoading];
self.webView.delegate = nil;
UIWebView and WKWebView both have a canGoBack and canGoForward property on them. You can use KVO to see when these are enabled, and enable/disable your back and forward buttons accordingly.
http://nshipster.com/key-value-observing/
Well, after reading a bunch of SO posts on this issue, I still can't fix problems with my activity indicator. This indicator is in a view in a tab under the control of its own view controller. It has a view with a UIWebView which loads a local html page just fine. The view is loaded with initWithNibName and then awakeFromNib. Here's the part I think is relevant:
#implementation HelpViewController
#synthesize webView = _webView;
#synthesize back = _back;
#synthesize forward = _forward;
#synthesize aI = _aI;
- (void) viewDidLoad {
_aI = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[_aI stopAnimating];
// Now add the web view
NSString *filePath = [[NSBundle mainBundle]
pathForResource:#"Help"
ofType:#"html"];
[self.view addSubview:_webView];
_webView.delegate = self;
NSURL* fileURL = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:fileURL];
[_webView loadRequest:request];
[super viewDidLoad];
}
-(void) webViewDidStartLoad:(UIWebView *)webView {
[_aI startAnimating];
_back.enabled = NO;
_forward.enabled = NO;
}
-(void) webViewDidFinishLoad:(UIWebView *)webView {
[_aI stopAnimating];
if (webView.canGoBack) {
_back.enabled = YES;
_back.highlighted = YES;
}
if (webView.canGoForward) {
_forward.enabled = YES;
_forward.highlighted = YES;
}
}
The navigation buttons work fine. The activity indicator was placed in the nib, but in the main view, not on/over/under the webView. In the attributes, I have Hides When Stopped checked. If I check Animating the indicator is always visible and animated, no matter how I navigate through the UIWebView.. If I uncheck Animating, Hidden is automatically checked for me. In this case, the indicator never shows up. So it's either always on or always off.
I've read quite a bit about cases where you need to have the indicator on a different thread. I'm not sure, but I don't think that applies here (I load a local html page but allow users to navigate off and then back to the local page). But, I seem to have some disconnect; perhaps it is the fact that the indicator is in the main view but the pages are in the webView? Or I'm not calling things in the right methods. Or who knows... Thanks for any help!
You overwrite _aI in your viewDidLoad and never place it in your view hierarchy, so the object you are sending the messages to is never visible and the activity indicator placed in interface build will never change its state, so thats why its either always animating or hidden.
I have an iOS app that needs OAuth authentication of a Facebook account, but I have to use my own server for the API calls.
So I want to open a FBLogin screen (UIWebView), with a login URL, when a certain button is clicked.
I have the following code:
FBWebViewController.h
#import <UIKit/UIKit.h>
#interface FBWebViewController : UIViewController <UIWebViewDelegate>
- (void)initFBWebView;
#end
FBWebViewController.m
- (void)initFBWebView {
NSLog(#"DetailViewController->initUIWebView {");
UIWebView *aWebView;
// init and create the UIWebView
aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
aWebView.autoresizesSubviews = YES;
aWebView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
//set the web view delegates for the web view to be itself
[aWebView setDelegate:self];
//Set the URL to go to for your UIWebView
NSString *urlAddress = #"http://www.google.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//load the URL into the web view.
[aWebView loadRequest:requestObj];
//add the web view to the content view
[self.view addSubview:aWebView];
}
and in my Authorize.m file, which handles all the API calls etc.
- (void)fbLogin:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
FBWebViewController *webview = [[FBWebViewController alloc] init];
[webview view];
[webview initFBWebView];
}
Nothing happens. No WebView is opened. I put some debug statements in the initWithFbWebView method, and the method is called and run until the end, but no screen is showing up.
What am I doing wrong?