Webpage title doesn't update unless I call the method twice
NSURL *yourURL = [NSURL URLWithString: webpageURLLabel.text ];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:yourURL];
[webpagePreview loadRequest:request];
webpagePreview.scalesPageToFit = YES;
webpageTitleLabel.text = [webpagePreview stringByEvaluatingJavaScriptFromString:#"document.title"];
Any suggestions on how to fix this?
I guess your request is not finished, so you're too early to call a javascript on that page.
You should make the calling class a delegate of your webview and set the title on webViewDidFinishLoad:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
webpageTitleLabel.text = [webpagePreview stringByEvaluatingJavaScriptFromString:#"document.title"];
}
The above code fixed my issue.
Related
I am loading html content in UIWebview (iPhone6) and the content has actionable buttons. Upon pressing one of the buttons a pop up shows up in the middle of the content (content is approximately 6-7 pages long). As a result, user is unable to see the popup as she is on the first screen. Earlier, I thought it was content's issue, but on android, the pop up comes on the middle of the first page.
-(void) loadScorm {
NSString *scormUrl = self.content.media.scormUrl;
scormUrl = [NSString stringWithFormat:#"%#%#", #"https:" , scormUrl];
NSURL *url = [NSURL URLWithString:scormUrl];
NSString *body;
body = //constructed body
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
[request setHTTPMethod: #"POST"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:[body dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];
self.webView.scalesPageToFit = YES;
[self.webView loadRequest:request];
self.webView.frame = self.webViewContainer.frame;
}
I also read that javascript for UIWebView is enabled by default. What am I missing? Any pointers would be appreciated.
PS: Can't share the screenshot as the content is classified.
How you create your UIWebView via Code or IBOutlet.
If you are created your webView via IBOutlet then you have to check some webView property in Storyboard like
Then your webView will take Events.
And if you are created your UIWebView via code then write this code
[your_webView_Object setDataDetectorTypes: UIDataDetectorTypeAll];
[webview loadHTMLString: [NSString stringWithFormat:#"<div id ='foo' align='justify' style='font-size:22px; font-family:helvetica; color:#ffffff';>%#<div>",[jsonObject objectForKey:#"msg"]] baseURL:nil];
Hope you will find useful
Apparently, this was an issue from the side of content creator. The person didn't include the support of Safari and Chrome on iOS platform and hence, these issues.
I am attempting to load a locally hosted html file into a webview from an array. it looks something like this
_siteAddresses = [[NSArray alloc]
initWithObjects:#"file://localhost/var/mobile/Application/${APP_ID}/First Pentecostal Seminary.app/First_Pentecostal_Seminary/Main.html",...
with the corresponding code being
NSString *urlString = [_siteAddresses
objectAtIndex:indexPath.row];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
self.detailViewController.webView.scalesPageToFit = YES;
[self.detailViewController.webView loadRequest:request];
What am I doing wrong...is it my coding or perhaps the html coding (I believe there may be some java in there)
Two thoughts:
That example file URL doesn't look right. How did you construct that? If it was something from your bundle on your device, I'd expect something more like:
file:///var/mobile/Applications/9E670C3C-C8B1-4B09-AE66-B43F7DB29F4D/First Pentecostal Seminary.app/...
Obviously, you have to programmatically determine this URL by using NSBundle instance method URLForResource or by using the bundle's bundleURL and then adding the appropriate path components.
You should (a) specify the view controller to be the delegate for your web view; and then (b) implement webView:didFailLoadWithError: and look at the error there, and it will inform you what the error was, if any.
For example, I have a file, test.html sitting in my bundle, which I can load into a web view like so:
NSURL *bundleURL = [[NSBundle mainBundle] bundleURL];
NSURL *url = [bundleURL URLByAppendingPathComponent:#"test.html"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
I have set up my view controller as the delegate for the web view and have the following didFailLoadWithError:
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(#"%s: %#", __FUNCTION__, error);
}
So, when I tried to load a request with an invalid URL this time (test2.html, which I do not have in my bundle), I get the following error:
2014-02-26 23:35:43.593 MyApp[3531:70b] -[ViewController webView:didFailLoadWithError:]: Error Domain=NSURLErrorDomain Code=-1100 "The requested URL was not found on this server." UserInfo=0x8c32f40 {NSErrorFailingURLStringKey=file:///Users/user/Library/Application%20Support/iPhone%20Simulator/7.0.3/Applications/FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF/MyApp.app/test2.html, NSErrorFailingURLKey=file:///Users/user/Library/Application%20Support/iPhone%20Simulator/7.0.3/Applications/FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF/MyApp.app/test2.html, NSLocalizedDescription=The requested URL was not found on this server., NSUnderlyingError=0x10424c90 "The requested URL was not found on this server."}
Try this code
UIWebView *documentsWebView=[[UIWebView alloc]init];
documentsWebView.delegate=self;
documentsWebView.frame=CGRectMake(0, 0, 1024, 616);
documentsWebView.backgroundColor=[UIColor clearColor];
[self.view addSubview:documentsWebView];
NSString* htmlString = [NSString stringWithContentsOfFile:[_siteAddresses
objectAtIndex:indexPath.row] encoding:NSUTF8StringEncoding error:nil];
[documentsWebView loadHTMLString:htmlString baseURL:nil];
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];
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.
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"]]];