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.
Related
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];
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];
}
Hi I want to load html and .js file from my project. These are the below steps i am following to load html file through framework but i am not able to hit html files.
Steps followed.
Created a framework which has webview as subview in it.
Created Resource.bundle and added all my html,.js files and images in the bundle.
When i add this framework into my project and try to access the image from the bundle i am not able to.
when i try to hit the html file i am not able to laod the html file in my webview.
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:#"Resource" ofType:#"bundle"];
NSString *htmlFile = [[NSBundle bundleWithPath:bundlePath] pathForResource:#"simple" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
NSURL *url=[[NSBundle mainBundle] bundleURL];
[webview loadHTMLString:htmlString baseURL:url];
[self addSubview:webview];
Can anyone tell me why this wont work and is this the right approach to follow?
I create a bundle with index.html that call a js with a simple alert, with this code i'm able to se the alert
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:#"BundleName" ofType:#"bundle"];
NSString *htmlFile = [[NSBundle bundleWithPath:bundlePath] pathForResource:#"index" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[_web loadHTMLString:htmlString baseURL:[[NSBundle mainBundle] bundleURL]];
the html work in local? the webview is initialized?
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"]]];
First I would like to point out I'm very new to web development and more comfortable with iOS development, so excuse me if there's something fundamental I'm not understanding.
I've seen how you can place an HTML file into your apps directory and load it into a web view (Example). This is great, but how can you create a new local HTML file within the app? Such that the user can create a new html file to type in, and then store it (basic document style app functionality). Perhaps with some sort of Javascript (I'm not too familiar with such Javascript)?
You can build the HTML in a NSString like so:
// get user input
NSString *userText = #"Hello, world!";
// build the HTML
NSString *html = [NSString stringWithFormat:#"<html><body>%#</body><html>", userText];
// build the path where you're going to save the HTML
NSString *docsFolder = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *filename = [docsFolder stringByAppendingPathComponent:#"sample.html"];
// save the NSString that contains the HTML to a file
NSError *error;
[html writeToFile:filename atomically:NO encoding:NSUTF8StringEncoding error:&error];
Clearly I'm just setting the userText to some literal, but you could obviously let the user type it into a UITextView and grab it from there.
You can then load the HTML into a web view with:
[self.webView loadHTMLString:html baseURL:nil];
or with:
NSURL *url = [NSURL fileURLWithPath:filename];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];