I am using UIWebView loadRequest to request a new URL. The UIWebView is inside a TabBar View. When I place the request inside awakeFromNib it works. When a make a subsequent call the view does not change.
[webDisplay loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com"]]];
If I place the initial request inside viewDidLoad instead of awakeFromNib it also doesn't seem to work.
I note that if I track request by delegating shouldStartLoadWithRequest this method also doesn't seem to be called by this loadRequest call.
Tabbar view controllers are loaded only once i.e when a particular tab is selected for the first time. So, viewDidLoad and awakeFromNib are only called once.
You need to call loadrequest in viewDidAppear method for the webview to be reloaded with latest content whenever that particular tab is selected.
Related
I have a UIWebView in my view controller and have set its delegate to the view controller via code (i.e. not through IB). I have also setup the appropriate delegate methods: shouldStartLoadWithRequest, webViewDidStartLoad, webViewDidFinishLoad and didFailLoadWithError.
In my view controller's viewDidLoad method I load an appropriate URL with this code:
[self.webView loadRequest:reqURL];
95% of the time everything works great and the page loads in the UIWebView object and displays as expected. Occasionally, however, the page doesn't load.
After stepping through my code I realized that on the times that it doesn't work the shouldStartLoadWithRequest delegate method fires, but webViewDidStartLoad doesn't.
Does anyone have any idea what's going on here? I couldn't find anything on Stack Overflow that specifically addressed this unique issue I'm having and am slowly reaching my breaking point. Thanks in advance!
You should make sure that your shouldStartLoadWithRequest implementation returns YES for all conditions at which you need your webView to load.
my question is that i use ios 4.1 and viewWillAppear is not call when i go via
[self.view addSubView:self.mySecondView.view];
then and in the first view i alloc second view on ViewDidLoad .. i just want to call ViewWillApear on Second View because want to do the task dynamic.
is their some thing alternative... because InitWithNibName method also fire when alloc and init occur.. i want to fire some method when i add to that view.
Thanks
The same issue is discussed before,Just go through below SO links.
iPhone viewWillAppear not firing
viewDidLoad gets called, viewWillAppear does not get called, view does not appear on screen
http://forums.macrumors.com/showthread.php?t=575530
here is the tutorial post
http://www.idev101.com/code/User_Interface/UINavigationController/viewWillAppear.html
Like iPhone safari browser, I want to update UItextview with current url of UIWebView
I tried shouldStartLoadWithRequest delegate method, but I end up with url of some ads/iframe
If I use webViewDidFinishLoad delegate method, then address bar gets updated at the end of the request.
Please help me on the best approach.
You want to use the mainDocumentURL method on the NSURLRequest in webView:shouldStartLoadWithRequest:. This should contain the main document URL regardless of whether the page is loading or a resource on the page (e.g. a frame) is loading.
Use webviewDidFinishLoad when delegated from your webview, and then query it for the title with [webview stringByEvaluatingJavaScriptFromString:#"document.title"] or weboutWebView.request.URL.absoluteString for the url.
My application uses UITabBarController with 4 tabs. Each tab will have a UIWebView along with other types of objects. When the app launches I need to call the method for this first webView to retrieve my web content.
I have this method in my viewdidLoad:
[self performSelectorInBackground:#selector(getAdvisory) withObject:nil];
The web content on the first tab works fine. I'm just at a loss to get my other tabs to load up. I think I would use a switch or if statement but I do not know how to tell which view is loaded.
I need to do the same for the rest of the tabs. The app has a single view controller.
When setting an action using a button everything works fine. I just do not know how to call the method when a different view (tapping tabs) loads.
Also when retrieving data from the network, what are the best methods to use to not tie up the main thread? I have read where NSOperation would be used in this scenario. Is this correct? If so how would I go about doing this?
Thanks in advance.
I was looking for a solution to this problem and there is a better method.
Simply override the viewDidAppear method and insert the code you want to execute when the view does appear!
As an example the following code will call myMethod.
-(void) viewDidAppear:(BOOL)animated {
[self MyMethod];
}
maybe by using a user delegate of your UITabBar and the method:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
Sent to the delegate when the user selects a tab bar item.
See reference.
I am relatively new to Objective C/iPhone Development.
I am developing a simple app with two views using a Navigation controller.
The first view contains several buttons a and the second view contains a uiwebview.
The idea is that when a button is pressed it switches to the second view and the web view loads a local html file. Which html file is loaded depends on which button was pressed.
At the moment, the view successfully changes and I can specify a html file to load in the viewdidload method of the webviewcontroller. However this obviosly means that every button triggers the same html file.
Therefore I want to create a NSMutable string which can be accessed from both views(i.e set to a particular filename in the first view and then retrieved when the seoncd view loads).
I have searched the internet for hours and trued to use global variables, the singleton method and accessing a variable in the appdelegate. However no matter which method I try to implement, the uiwebview always displays the same html file(which is the first file alphabetically)
Thanks for any help given. I greatly appreciate any suggestions.
You don't have to use an NSMutableString for this.
Add a property url, just a normal NSString, to the second view controller. Before pushing the second view controller onto the navigation stack, you set that url property to the local url you want to load.
Then, in the second view controller class, implement:
- (void)setUrl:(NSString *)newUrl {
if(url != newUrl){
[url release];
url = [newUrl retain];
// [webView load.... load the new url in the webView.
}
}
This may be beyond you current knowledge, it contains some vital Objective-C code, like properties, setters and memory management, but just test it out and see what you can do to make it work.
EDIT You don't even need this property. Just implement a method like loadUrl: in the webViewController.
- (void)loadUrl:(NSString *)url {
// [webView load.... load the URL in the webView
}
Then, before pushing the webViewController, call this method with the URL you want to load. Avoids a property and gets rid of all the additional memory management code.
if (self.myWebViewController == nil) {
//initialise
webViewController *viewWeb = [[webViewController alloc] initWithNibName:#"webViewController" bundle:[NSBundle mainBundle]];
self.myWebViewController = viewWeb;
[viewWeb release];
}
[self.myWebViewController loadUrl:#"some-local-url"];
[self.navigationController pushViewController:self.myWebViewController animated:YES];
As a note, a good practice is to capitalize each classname. Now you have a class webViewController, but in Cocoa it's common to name the class WebViewController, then you can have a variable called webViewController. Also, you can clearly see whether you are dealing with object (variable) or just a class.
EDIT Updated
So I forgot about the fact that the webView will not have been loaded until your view gets loaded. To solve it you'll have to reintroduce the property I talked about earlier. This time some adjustments are made. In your webViewController class, add the following code:
- (void)viewDidLoad {
// viewDidLoad gets automatically called once the view has loaded. Here we want to load the webView.
[self loadWebView];
}
- (void)setUrl:(NSString *)newUrl {
if(url != newUrl){
[url release];
url = [newUrl retain];
// Update the webView with the newly set URL. This will not do anything if the view hasn't been loaded, since webView will still be nil. That's why we call loadWebView again when the view gets loaded, in viewDidLoad.
[self loadWebView];
}
}
- (void)loadWebView {
// Here you'll have to load the url. You can access it using `self.url`.
//[webView load...
}
Now, in the first view controller, update [self.myWebViewController loadUrl:#"some-local-url"]; to self.myWebViewController.url = #"some-local-url";.
I assume that the web view controller is initialised from the navigation controller, so why don't you just pass it that string when it is initialised. Then when a button is pressed it uses the correct string to instigate the web view controller.
Another idea, one I think you've tried, but I am not sure, is that the web view controller has a static method and value. When you press the button it then calls the static method on the web view controller. Then when web view controller is loading up, it calls a get static method of the value.