I am using the function (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL
[webView loadHTMLString:sourceCode baseURL:nil];
to load some source-code I wrote, with links to internet content. For instance :
Google
The string load well, but there is no way to access the google link then.
Do you know what I am doing wrong ? Is there anything to do with the baseURL ?
your webpage must return a string that just contain http://www.google.com. If you need to use the link, you must modify the NSString that you receive to extract the right url!
Try setting the dataDetectorType for your webview like this:
webView.dataDetectorTypes = UIDataDetectorTypeLink;
Hope this helps!
Related
Its known that we could embed local media files inside webview and links such as from youtube (streamable).
But I am not sure, if we can link to urls like above. I tried with <embed>, <video>, <iframe> and <object>. Nothing worked for me.
NSString *htmlString = #"<html><head></head><body><video
width="%0.0f" height="%0.0f" src="%#"></video></body></html>";
NSString* html = [NSString stringWithFormat:url,self.webView.frame.size.width,self.webView.frame.size.height,self.htmlstr];
[self.webView loadHTMLString:html baseURL:nil];
I am answering with certain observation, correct me (this answer) if its wrong.
Local file or a link (itunes music or movies link) can be given to
and played on UIWebView
Remote link (file) can be shown as well, but it has to streamable like youtube link. Just giving a link wont solve the problem it seems.
How can I do an if statement to check if UIWebView is displaying a certain page or not?
Thanks,
James
currentURL = webView.request.URL.absoluteString;
NSLog(#"%#",([currentURL isEqualToString:desiredURL] ? #"YES" : #"NO")];
You could try using the url property of the request - see SO for an example
But I'm not sure how you could handle different encodings of the same URL or IP addresses instead of FQDN's.
I'm looking for a way to load my UIWebView with a local CSS file that should affect on the UIWebView's loadRequest.
For the clearness:
I have an UIWebView that I loadRequest it with a website url.
I also have a local CSS file that should affect this loadRequest url.
I want to load this CSS file onto the UIWebView.
Thanks,
This stackoverflow question appears to have one or two answers that may help you.
You need to load the local CSS (using a method not unlike #Shrey uses, but looking for your CSS file), and somehow inject it into the page, and the only way appears to be to use:
[webview stringByEvaluatingJavaScriptFromString:someJavascriptToInjectCSS];
and some clever Javascript to modify the page to add the CSS in.
Hope this helps point you in the right direction. I have used this method to inject stuff into pages, so it does work, but I don't know Javascript well enough to write the code to inject your CSS into the page.
This is the Swift version.
Load the css file on the webViewDidFinishLoad delegate method and use stringByEvaluatingJavaScriptFromString.
Credits here
func loadWebViewStyles() {
guard let cssPath = NSBundle.mainBundle().pathForResource("theme", ofType: "css") else {
return
}
let loadStyles = "var script = document.createElement('link'); script.type = 'text/css'; script.rel = 'stylesheet'; script.href = '\(cssPath)'; document.getElementsByTagName('body')[0].appendChild(script);"
webView.stringByEvaluatingJavaScriptFromString(loadStyles)
}
try this:
[WebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"]isDirectory:NO]]];
i tried the following one from iphone cook book .i displayed HTML content through accessing private API of UITextview like
#interface UITextView (extended)
- (void)setContentToHTMLString:(NSString *) contentText;
#end
it works FINE.but Apple wont allow accessing private API.any solution pls?
Do you want to display actual HTML elements or just a string you took off the web? For the latter, you would do the following:
NSURL *stringURL = [NSURL URLWithString:#"http://yoursite.com/page.html"];
NSString *responseString = [NSString stringWithContentsOfURL:stringURL encoding: NSUTF8StringEncoding error:nil];
myTextView.text = responseString;
You could try using the Three20 library, which includes a styled text label:
http://github.com/facebook/three20/
The class TTStyledTextLabel of the Three20 framework has a text property "text" of type TTStyledText. This TTStyledText class has a textFromXHTML: static method which takes an NSString with any kind of HTML text inside, and might help you do what you want. However, it does not allow editing of the text, at least not as far as I know.
Hope this helps!
If you want HTML, your best bet is to use a dedicated UIWebView.
You can try this one.This is uses core text framework
https://github.com/aryaxt/iOS-Rich-Text-Editor
There's a known problem with embedded UIWebViews that if you load data into them using loadHTMLString or loadData, the canGoBack/canGoForward properties and goBack/goForward methods don't work. These only work when using loadRequest.
Since Safari's normal app cache doesn't work in embedded UIWebViews, creating a native app that effectively caches otherwise live content becomes impossible/unusable. That is, I can cache the contents of the HTML, Javascript, images, etc. and load them via loadHTMLString or loadData, but then the back and forward buttons don't work.
I could also use loadRequest and specify a file URL, but that breaks when it comes to communicating with the live site -- even if I specify a tag (because of cookie domain issues).
I have a work around that involves basically re-implementing the app cache using local store (and not having the native app do any caching itself), which is OK, but not really ideal. Are there any other work arounds/something I missed?
I am using the UIWebView's canGoBack to check to see if I'm at the first page of the history. If I am then I just call the little method I used to load the first page (displayLocalResource sets up the HTMLString and loads it into the webView). Here is a snippet:
//Implementing a back button
- (void)backOne:(id)sender{
if ([webView canGoBack]) {
// There's a valid webpage to go back to, so go there
[webView goBack];
} else {
// You've reached the end of the line, so reload your own data
[self displayLocalResource];
}
}
So do you download the HTML yourself, then pass it to UIWebView as a string? Why so? Do you modify it on the fly or something?
Maybe a custom URL schema would help? You use loadRequest with a schema of your own, which in turn works with HTTP and then feeds the webview whatever data you want?
I had a same problem. I tried manage the history, but it is error prone. Now I have discovered a better solution of this.
What you want to do is simply add a loadRequest to about:blank and make that as a placeholder for you before you call loadHTMLString/loadData. Then you are totally free from monitoring the history. The webview.canGoBack and canGoForward will just work. Of course, you will need a hack to handle go back to the placeholder about:blank. You can do that in webViewDidFinishLoad. Here is the code highlight:
In the function when you call loadHTMLString:
[weakSelf.fbWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"about:blank"]]];
[weakSelf.fbWebView loadHTMLString:stringResponse baseURL:url];
Code to handle goBack:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
if ([webView.request.URL.absoluteString isEqualToString:#"about:blank"]
&& ![webView canGoBack] && [webView canGoForward]) {
[weakSelf.fbWebView loadHTMLString:stringResponse baseURL:url];
}
}
I think it is also possible expand this solution to handle those loadHTMLString that is not the first load. Just by having a Stack to record all the string response and insert an about:blank on each loadHTMLString. And pop the stack when each time go back to about:blank.
Could you fetch the content, save it to the local filesystem, point the webview to the local filesystem using file:// URLs, then intercept the link follows with shouldStartLoadWithRequest to fetch more to local fs, point webview at new local content, etc?
I've had good luck with UIWebView and file:/// URLs. Basically you'd be intercepting load requests, fetching stuff yourself, writing it to the local filesystem with rewritten URLs, then loading that into the browser.
There seems to be no way to load/save the browser history.
Loading the string into a temp file and using that as a URL request seems to cure this. It's something about loading the string directly that causes UIWebView not to see it as the home page you can navigate back to. This code worked for me:
//If you load the string like this, then "webView.canGoBack" never returns YES. It's documented frequently on the web.
//Loading the string as a URL instead seems to work better.
//[self.myWebView loadHTMLString:str baseURL:nil];
//write the string to a temp file
NSString *fileName = #"homepage.html";
NSURL *fileURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:fileName]];
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
[data writeToURL:fileURL atomically:NO];
//open that temp file in the UIWebView
[self.myWebView loadRequest:[NSURLRequest requestWithURL:fileURL]];
Use this to enable/disable the back button:
- (void)webViewDidFinishLoad:(UIWebView *)webView{
//this is to check if we're back at the root page.
if (webView.canGoBack == YES) {
self.backButton.enabled=YES;
}
else {
self.backButton.enabled=NO;
}
}