I have a UIWebView that i am loading using loadHTMLString and i need to set cookies on the request header. I know how to do this using loadRequest but not loading the webview with loadHTMLString I don't have the request object. Has anyone done anything like this?
There will not be any difference between loading web view from loadRequest vs loadHTMLString method, shouldStartLoadWithRequest method will be called in both cases. You can override your headers there and add header as per your requirements.
Related
Actually I have two related questions here, about different use cases of loading requests in a UIWebView.
Is it safe to call - [UIWebView loadRequest:] on a web view that is inserted in the view hierarchy and its hidden property or the one of its superview is set to YES?
Is it safe to call - [UIWebView loadRequest:] on a web view that is not inserted in the view hierarchy?
In particular I'm interested whether it is considered to be a good practice to load request in a UIWebView that is not visible, and whether the delegate assigned to the instance of UIWebView will be notified once the request succeeds/fails. The reason I'm asking is that UIWebView class reference says "create a UIWebView object, attach it to a window, and send it a request to load web content", where the part telling that a UIWebView should be attached to a window makes me doubt if the above approaches are reliable.
I have successfully used [UIWebView loadRequest:] with objects that are not in the view hierarchy. I expect that the class reference just assumes that the view will be displayed as it's probably the most common use case.
Yes it is safe to call [UIWebView loadRequest:] on a web view that is inserted in the view hierarchy.Because you are going to use web in the view.Also if you just give the URL in the program,it is enough to get.
The following code for [UIWebView loadRequest] is
NSString *strurl =#"http:www.google.com";
NSURL *url=[NSURL urlWithString:strurl];
NSURLRequest *urlrequest =[NSURLRequest requestWithUrl:url];
[webView loadRequest:urlrequest];
Even it is safe to call [UIWebView loadRequest:] on a web view that is not inserted in the view hierarchy.Because you can dynamically create the view and write the code for web view through the program.
It works. The approach is completely reliable as long as you are not using any private API and following HIG. It is not a bad practice as long it suits to your requirement. If there is a hidden property available to you for UIWebView then of-course you can hide the webView as per your requirements.
About your below query, it is written in the documentation as per the sentence context.
The reason I'm asking is that UIWebView class reference says "create a
UIWebView object, attach it to a window, and send it a request to load
web content", where the part telling that a UIWebView should be
attached to a window makes me doubt if the above approaches are
reliable.
The full context is below, which clearly means that to display a webpage in your application using UIWebView, you have to do it in the mentioned way.
You use the UIWebView class to embed web content in your application.
To do so, you simply create a UIWebView object, attach it to a window,
and send it a request to load web content.
First thanks in advance to have a look on my issue. My issue is my am developing webview based application. It's same like as browser but i have a issue related to reload web view. When i am getting a value form popover bookmarked and try to reload the current web view from this next bookmark url, my web view do noting. I am stuck in this issue. Although when i enter any url through it works perfectly. May i doing something wrong with web view ?
temp = appDeleg.strURLFromPopover;
NSURL *url = [NSURL URLWithString:temp];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.myWebView loadRequest:requestObj];
[self.myWebView setDelegate:self];
[self.view addSubview:myWebView];
Please have a look..
The last line looks like very suspicious. In my experience, [self.view addSubview:myWebView] should be placed in the "initialization block" to avoid some lazy init issues. Are you sure All objects are NOT nil? if so, you can try to set UIWebView's delegate to your class, and debug in each method to test if your object are properly initialized. I tried your code, work fine in my side. Below are delegate method you might consider about
– webView:shouldStartLoadWithRequest:navigationType:
– webViewDidStartLoad:
– webViewDidFinishLoad:
– webView:didFailLoadWithError:
I'm having this problem in my iPhone app: I have a webView which is first loaded with an HTML string. After the user clicks a link and it loads the requested page, the webView won't go back when I call the method [UIWebView goBack]; I suppose webView doesn't cache the HTML string. Is there any way I can make webView cache my HTML string without having to save it in a NSString myself?
You can use the canGoBack property and if you can't go back, reload the UIWebView with the original html. If the user has navigated forward using links then the canGoBack property will return YES and a goBack can be initiated on the UIWebView. The _htmlString is a member variable that is set when the UIWebView is initialized using an HTML string. -rrh
- (void)goBack
{
if (_htmlString && ![_browserWebView canGoBack]) {
[_browserWebView loadHTMLString:_htmlString baseURL:nil];
return;
}
[_browserWebView goBack];
}
Try to create a NSURLRequest from the file URL and use loadRequest instead of loadhtmlString
NSURL *htmlFileUrl = [[NSBundle mainBundle] URLForResource:#"index" withExtension:#"html"];
NSURLRequest *localRequest = [NSURLRequest requestWithURL:htmlFileUrl];
self.webView.delegate = self;
[self.webView loadRequest:localRequest];
This sounds like a your webview variable isn't properly linked up to the webview instance that you are using. Breakpoint at this call and check whether your webView variable is 'nil'.
If it is, make sure your webView in your XIB file is linked to an IBOutlet variable in Interface Builder. This is a common mistake and something I tend to forget when designing a new page for the first time.
This tutorial covers a LOT on how to build interfaces using Interface builder which i'm sure you're familliar with but for those that aren't it's also useful. It has some good screenshots which help illustrate what I mean by 'linking' better than me typing "click the little + icon and drag the little thingy on to the UI element" :)
http://www.icodeblog.com/2008/07/30/iphone-programming-tutorial-connecting-code-to-an-interface-builder-view/
EDIT
The only other thing i can think of is that you are overwriting your webView variable by re-initialising it somewhere (it is already initialised by the XIB) and therefore you're calling goBack on a webview that doesn't exist on the screen.
I am new to iOS development. I will try my best to explain my question, please pardon me if you find it silly.
I was wondering if one can load a subview in a UINavigationController by clicking a link inside a UIWebView.
Explaining it further, let's say there is UINavigationController that has a subview and that subview has a UIWebView called A, and there is another subview(that is not loaded yet) called B. Is is possible to load B (or perform any other native Objective C event) when user taps on a link inside the UIWebView A?
For example in Instagram app for iPhone, as I have observed (I may be wrong), there are UIWebViews in views controlled by UINavigationControllers. Taping a link inside a web view loads a new view. How is it happening? Can one set up the communication between the web page loaded inside the UIWebView and the parent/super UIView?.
Please correct me if I am lost. I am a n00b in iOS development so far. Also reference me the APIs or tutorials to do this if this makes any sense. Thanks in advance.
If you want just to handle a click-on-link inside UIWebView, just implement a delegate for the UIWebView. It has webView:shouldStartLoadWithRequest:navigationType: , which is called any time any content is attempted to be loaded in UIWebView. In the implementation of this method, you can block loading of the HTML content and perform any obj-c code, you want.
Hope that helps!
i have a string called htmlString that contains some informations formatted in html. I need to put these info into a webView that load the entire html string, with color and fonts. And i need to know the string height. How can i do?
You want to do something like:
[_webView loadHTMLString:htmlStr
baseURL:[NSURL fileURLWithPath:path]];
You can view the docs here: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIWebView_Class/Reference/Reference.html
This will load your HTML into the webview and use the path you provide as a root for other documents. In other words, your html string could reference other files (css, javascript, etc...) and the baseURL is used to locate the urls that use relative paths.
EDIT:
To get the height, you could assign the UIWebView's delegate as it has a webViewDidFinishLoad: method to tell you when the page is rendered. Then you could execute javascript on the page to determine the final height using UIWebView's method - stringByEvaluatingJavaScriptFromString:
This answer also seems pretty relevant: How to determine UIWebView height based on content, within a variable height UITableView?
NSString *htmlStrings="Hello I m here";
[_webView loadHTMLString:htmlStrings
baseURL:[NSURL fileURLWithPath:null]];