How to show a section of HTML using webview? - html

I have been searching through questions trying to find an answer but I can't seem to figure out what I am doing. I want to display just a section of my HTML in webview.
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 640.0)];
NSURL *URL = [NSURL URLWithString:#"http://stackoverflow.com"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:URL];
webView.delegate = self ;
[webView loadRequest:requestObj];
[self.view addSubview:webView];
This works perfectly displaying the whole webpage. But lets say I want to just display a certain div, what do I have to do?
I have tried adding in getElemntByID and getElementsByClass but it doesn't seem to work for me..

the answer of your question.
NSString *htmlPage = #"<html><body><h1>StackOverFlow</h1></body></html>";
[yourWebView loadHTMLString:htmlPage baseURL:nil];

Related

How to load localized html file from WKWebView?

1st StackOverflow question for me...
!! Objective-C !!
I'm in the process of updating an old app and am working on help screens.
I chose to display some useful How-tos through some html mainly for the ease of formatting the text (bullet points, colors, bold, underline etc...).
Since the app is a bit complex, The help screens are in a total of 3:
A main info webpage that links to 2 other detailed pages.
Tha app is localized in English (Base) and French.
Once the setup was complete, and with everything working fine, I went ahead and localized the base html files.
Now, they are neatly placed in the Base.lproj and fr.lproj files.
But when when I tap on a link to another page from the main WebView, nothing happens !
Any ideas ?
Thanks
Here are some bits of code I'm using:
In the .h file :
#interface howToViewController : UIViewController <WKUIDelegate>
{
WKWebView *infoText;
NSString *uRLString;
}
#property (nonatomic, retain) WKWebView *infoText;
In the .m file: (had to programmatically create the WKWebView because of the known Xcode 9 bug when creating it through Storyboards)
- (void)viewDidLoad
{
[super viewDidLoad];
WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
WKWebView *aWebView = [[WKWebView alloc] initWithFrame:self.view.frame
configuration:theConfiguration];
aWebView.UIDelegate = self;
self.infoText = aWebView;
NSURL *url = [[NSBundle mainBundle] URLForResource:#"infoHTML" withExtension:#"html"];
NSString *html = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSURL *baseUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[infoText loadHTMLString:html baseURL:baseUrl];
[self.view addSubview:infoText];
}
In the html file, the links that no longer work once the files are localized:
<p>3/ More information is available for the respective tabs by pressing the following links :</p>
<ul><li>Limitations
</li></ul>
<ul><li>Fueling
</li></ul>
Working code from one of my apps. I'm using NSURLRequest and WKWebView's loadRequest instance method. I'm not sure what's going wrong with your use of loadHTMLString:baseURL: but I think it may have something to do with the value you're passing for the baseURL argument. Have you cleared the console, then single-stepped over that line of code to see if you're getting any message in the console?
NSURL *url = [[NSBundle mainBundle] URLForResource: pageString withExtension: #"html"];
NSURLRequest *request = [NSURLRequest requestWithURL: url];
[webView loadRequest: request];

Open a Pdf in IOS App

I am making a IOS App and i want to open a PDF in div tag of my html file.I have found a code which works well on windows but it is not working in iOS app. In iOS app it just show the image of the first page.
Open the pdf in Webview and keep a button on top right corner (or anywhere) You want it.. And their you can write code for dismissing the WebView.
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 300, 300)];
NSString *path = [[NSBundle mainBundle] pathForResource:#"documentName" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
And in Done Button Action Just remove the WebView

WKWebView Could not create a sandbox extension for '/'

I would like to use external file .html to use WKWebView. Or is there a solution without external file ? All is working good on simulator, but not on device...
Here is my code :
NSString *path = [[NSBundle mainBundle] pathForResource:#"myHTML" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
WKWebViewConfiguration *theConfiguration =
[[WKWebViewConfiguration alloc] init];
[theConfiguration.userContentController
addScriptMessageHandler:self name:#"interOp"];
_webView = [[WKWebView alloc] initWithFrame:self.view.frame
configuration:theConfiguration];
[_webView loadRequest:request];
[self.view addSubview:_webView];
I've read this topic : WKWebView not loading local files under iOS 8
I don't want to use a server. I'm using Objective-C, and I want that it works on only one view, not like the https://github.com/shazron/WKWebViewFIleUrlTest project with 2 views and button and I need to have connexion with internet in the page (read iFrame).
What have I to do ?
You have to copy all your web files to the app's NSTemporaryDirectory and call your index.html from there. Works fine on iOS 8.3 for me.

Delete Cache with UIWebview in an iOS application

I have a problem with UIWebview. I call several time an url for print a pictures in my webview and I count how many times my pictures was displayed.
When I call the url of the pictures the count match to the number of printing.
NSURL *URL = [NSURL URLWithString:#"http://url_pictures.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
[adView loadRequest:request];
When I call the url with loadhtmlstring my counter is to one. It's very important for me to use loadhtmlstring because my API returns me a html code with javascript.
NSString *html = #"<html><head></head><body>document.write(\"<a target=\"_blank\"ref=\"http://url_redirect.com\"><img border=\"0\" src=\"http://url_pictures.com\"></a>\");</body></html>";
NSString *baseURL = [mbAdUtil getBaseURL:html];
NSString *script = [[NSString alloc] initWithFormat:#"%#", html];
[adView loadHTMLString:script baseURL:[NSURL URLWithString:baseURL]];
I tested a lot of things that my counter is egal to printing.
this is some example of what I tested :
[webView loadHTMLString:#"" baseURL:nil];
[webView stopLoading];
[webView setDelegate:nil];
[webView removeFromSuperview];
Another :
// remove all cached responses
[[NSURLCache sharedURLCache] removeAllCachedResponses];
// set an empty cache
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
// remove the cache for a particular request
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:request];
Also, I tested by destroying my instance of my project and create another.
I destroyed my webview, all my variable... and so much else that I can not remember.
My counter is always 1.
My better solution is :
[webView loadHTMLString:#"" baseURL:nil];
[webView stopLoading];
[webView setDelegate:nil];
[webView removeFromSuperview];
With this I count 2 to 3 printing but the visual result is not that I expect.
Add cache policy on NSURLRequest:
NSURL *URL = [NSURL URLWithString:#"http://url_pictures.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10];
[adView loadRequest:request];
there three policy that will most likely help your solution:
NSURLRequestReloadIgnoringLocalCacheData,
NSURLRequestReloadIgnoringLocalAndRemoteCacheData,
NSURLRequestReloadIgnoringCacheData
I solved my problem !
I remove all my tests and I added timestamp has all level :
NSString *html = #"<html><head></head><body>document.write(\"<a target=\"_blank\"ref=\"http://url_redirect.com?time = 123456789\"><img border=\"0\" src=\"http://url_pictures.com?time = 123456789\"></a>\");</body></html>";
NSString *baseURL = [mbAdUtil getBaseURL:html];
it same as if there were several level cache. It's very stange but it works...
My solution is :
if http://url_redirect.com or http://url_pictures.com call another url, we must add the timestamp for me.

Unable to load html string in UIWebView using loadHTMLString:baseURL in iOS?

I am trying to embed youtube video into my iOS application.For that I have created a UIWebView & trying to load the Youtube video from following here
I have gone through the all the answers for the above problem. Even then its not working.
I have also tried loading very simple HTML
NSString *embedHTML =[NSString stringWithFormat:#"<html><body>Hello World</body></html>"];
[webView loadHTMLString:embedHTML baseURL:nil];
Even then, I am getting compile error Parse Issue Expecte ']'
I have tried cleaning, quitting the XCode & relaunching it again. I donno, I am not able to use that method. How to use the above loadHTMLString method for my UIWebView.
PS : Please do not tag this question as duplicate. I have tried all the solutions in Stackoverflow. Nothing has worked
WebView *webDesc = [[UIWebView alloc]initWithFrame:CGRectMake(12, 50, 276, 228)];
NSString *embedHTML = #"<html><head></head><body><p>1. You agree that you will be the technician servicing this work order?.<br>2. You are comfortable with the scope of work on this work order?.<br>3. You understand that if you go to site and fail to do quality repair for any reason, you will not be paid?.<br>4. You must dress business casual when going on the work order.</p></body></html>";
webDesc.userInteractionEnabled = NO;
webDesc.opaque = NO;
webDesc.backgroundColor = [UIColor clearColor];
[webDesc loadHTMLString: embedHTML baseURL: nil];
- (NSString *)getHTMLContent
{
NSString *cssPath = [[NSBundle mainBundle] pathForResource:#"baseline" ofType:#"css"];
NSData *cssData = [NSData dataWithContentsOfFile:cssPath];
NSString *cssStyle = [[NSString alloc] initWithData:cssData encoding:NSASCIIStringEncoding];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSString *subtitle = [NSString stringWithFormat:#"%# | %#", self.article.author, [dateFormatter stringFromDate:self.article.publishedDate]];
NSString *htmlString = [NSString stringWithFormat:#"<html><head><meta name='viewport' content='width=device-width; initial-scale=1.0; maximum-scale=1.0;'></head><style type=\"text/css\">%#</style><body><div id=\"container\"><h1>%#</h1><p class='subtitle'>%#</p>%#</div></body></html>", cssStyle, self.article.title, subtitle, self.article.content];
return htmlString;
}
It is very simple. You just have to add only one line. Try It:
NSString *htmlString = #"<html><head></head><body><p>1. You agree that you will be the technician servicing this work order?.<br>2. You are comfortable with the scope of work on this work order?.<br>3. You understand that if you go to site and fail to do quality repair for any reason, you will not be paid?.<br>4. You must dress business casual when going on the work order.</p></body></html>";
[WebView loadHTMLString: htmlString baseURL: nil];
You probably need to provide more code if you want people to help you. I just used webView in a similar way and it's working fine.
self.webView = [[UIWebView alloc] initWithFrame:myFrame];
self.webView.scalesPageToFit = YES;
[self.webView setBackgroundColor:[UIColor whiteColor]];
//pass the string to the webview
[self.webView loadHTMLString:[[self.itineraryArray objectAtIndex:indexPath.row] valueForKey:#"body"] baseURL:nil];
//add it to the subview
[self.view addSubview:self.webView];
Can you provide more information and code?
it doesnt seems like an issue on webview.Please do add the code where it breaks.Error says you missed a ] somewhere in the code
try to load the url directly into the webview :
UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(100, 100, 200, 250)];
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://youtube.com/embed/-0Xa4bHcJu8"]]];