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

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"]]];

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];

How to show only section of site in UiWebView?

I want to show site in UiWebView but I don't need to show all site but only from <section id="xxx"> to </section> with optional <head> for CSS styles. I tried to download html code and paste it to UiWebView
NSString *string = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://example.com/page"] encoding:NSUTF8StringEncoding error:nil];
NSString* content2 = string;
NSString* beforeBody = #"<section id=";
NSString* afterBody = #"</section>";
NSString* finalContent = [[beforeBody stringByAppendingString:content2]
stringByAppendingString: afterBody];
[_webView loadHTMLString:finalContent baseURL:nil];
but it didn't work for me. Any ideas? Thank you for help!
You can do the following:
Create 2 UIWebViews in your viewController, webView and webView2, implement #interface ViewController : UIViewController <UIWebViewDelegate> in your viewController header.
In viewDidAppear you will load the fullHTMLString from the page you would like to work with into webView2 and afterwards set the delegate for webView2 like so:
-(void)viewDidAppear:(BOOL)animated
{
NSString *fullHTMLString = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"https://python.org"] encoding:NSASCIIStringEncoding error:nil];
[self.webView2 setHidden:YES];
[self.webView2 loadHTMLString:fullHTMLString baseURL:nil];
[self.webView2 setDelegate: self];
}
You will have to implement webViewDidFinishLoad delegate method, and in it you call for [stringByEvaluatingJavaScriptFromString], which will retrieve your desired single element based on parameters you give it.
In this example I chose to retrieve it by ElementID, you can use javascript to retrieve it in other ways.
Once you have your desired element to display, you create an HTML string and insert it into it, afterwards you load the full html with the single element into the first webView (webView and not webView2) this is done to prevent an endless loop while calling the delegate method.
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *singleElementString = [webView stringByEvaluatingJavaScriptFromString:#"document.getElementById(\"mainnav\").innerHTML;"];
NSString *html = [[NSString alloc] initWithFormat:#"<html>%#</html>", singleElementString];
[self.webView loadHTMLString:html baseURL:nil];
}

Loading local html from an array in xcode

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];

How to show a section of HTML using webview?

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];

iOS - Showing App version in HTML page

I'm showing About in form of html in my iOS application. About.html is also bundled along with the application.
I want to show the application version in About html page automatically so that I dont have to edit the HTML manually everytime I bump the version.
Currently what I'm doing is as below:
I'm creating the html as <b>Version %#</b>
In Objective C code, I'm writing it as
NSString* aboutFilePath = [[NSBundle mainBundle] pathForResource:#"About" ofType:#"html"];
NSString* htmlStr = [NSString alloc] initWithContentsOfFile:aboutFilePath encoding:NSUTF8StringEncoding error:nil];
NSString* formattedStr = [NSString stringWithFormat:htmlStr, [self appNameAndVersionNumberDisplayString];
[self.webView loadHTMLString:formattedStr baseURL:nil];
- (NSString *)appNameAndVersionNumberDisplayString {
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
NSString *appDisplayName = [infoDictionary objectForKey:#"CFBundleDisplayName"];
NSString *majorVersion = [infoDictionary objectForKey:#"CFBundleShortVersionString"];
NSString *minorVersion = [infoDictionary objectForKey:#"CFBundleVersion"];
return [NSString stringWithFormat:#"%#, Version %# (%#)",
appDisplayName, majorVersion, minorVersion];
}
Is it good way to do it or is there any better way to achieve it?
You are doing it in near to perfect manner. There is no problem in proceeding with your approach.
That's ok. If you want to localise the text in the future then that might change the order of the parameters in the text so using something more like SUB_VERSION as the identifier to be replaced and then using the string replacement methods (instead of format method) would be better.