Using webview in tab bar application - ios

I am using this code to load html content in webview
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"about.html" ofType:nil]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
When AboutViewController tab(featured) is selected .Program shows blank screen does loads any kind of html content .
The sample program is given on following link .https://drive.google.com/open?id=0B5pNDpbvZ8SnbEhzMFFTNXJDXzA
Why code is not loading html ?

Reviewing your project, your code is fine but the real problem is that your AboutViewController viewDidLoad is not called, you must change the segue type of your NavigationController from push to rootViewController, to you AboutViewController and start working as should
Edited Code
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *pathString = [[NSBundle mainBundle]pathForResource:#"about.html" ofType:nil];
if(pathString)
{
NSURL *url = [NSURL fileURLWithPath:pathString];
if(url)
{
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
NSLog(#"%#", url.description);
}
NSLog(#"%#", pathString);
}
WAS
SHOULD BE
RESULT
Hope this helps you

Related

-(IBAction)goToWebSite:(id)sender not working

Just started my first project and I can't get it to work properly
It's a keyword search for my website.
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *webSite = #"http://www.mydomain.eu/";
NSURL *url = [NSURL URLWithString:webSite];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webPage loadRequest:request];
}
-(IBAction)goToWebSite:(id)sender {
NSString *webSite = #""http://www.mydomain.eu/m/?keyword=,addressBar.text];
[webPage loadRequest:request];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
The app loads so the first part is working but after it needs to fire the submit, by going to:
http://www.mydomain.eu/m/?keyword=,addressBar.text
It loads my website and after entering a keyword it should go to
http://www.mydomain.eu/m/?keyword=A_KEYWORD
Try in this way. It may help
-(IBAction)goToWebSite:(id)sender {
NSString *webSite = [NSString stringWithFormat:#"http://www.mydomain.eu/m/?keyword=%#",addressBar.text];
NSURL *url = [NSURL URLWithString:webSite];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webPage loadRequest:request];
}

Objective c/cocoa web view loading when typing string url but not when passing NSString*

In my DetailViewController.h file, I have declared a NSString * property:
#property (copy, nonatomic) NSString *url;
In my DetailViewController.m file, I have:
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *myURL = [NSURL URLWithString:#"http://google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:myURL];
[self.webView loadRequest:request];
}
This loads google.com in my web view but the following code does not:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *url = self.url;
NSLog(#"this is url %#", url);
NSURL *myURL = [NSURL URLWithString:url];
NSURLRequest *request = [NSURLRequest requestWithURL:myURL];
[self.webView loadRequest:request];
}
Which prints out: "this is url http://google.com".
Both url and #"http://google.com" are NSString* objects but why is the first coding loading the website but the second is not?
So realized there was whitespace at the end of url that I hadn't noticed until I printed out:
NSLog(#"this is url %#.", url);
Notice the period I added in the end. I had whitespace there, hence rendering my url invalid. I had to trim out the whitespace somewhere else in my code.

How to load URL in a textfield in xcode when any website is selected?

I have created an app which is a web browser. Whenever I enter any URL in text field it goes to the required web page. I have included an UIWebView to display webpage. By default google website is opened. But when I select any website from google I want to display that URL in the textfield. How do I do it?
- (void)viewDidLoad
{
[super viewDidLoad];
self.myTextField.text = #"http://www.google.com";
NSURL *myURL = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[_myWebView loadRequest:myRequest];
self.myTextField.delegate = self;
}
You can do like this
- (void)viewDidLoad
{
[super viewDidLoad];
self.myTextField.text = #"http://www.google.com";
NSURL *myURL = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[_myWebView loadRequest:myRequest];
_myWebView.delegate = self;
self.myTextField.delegate = self;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView;
{
NSURL *requestURL = [webView.request URL];
txtView.text = [requestURL absoluteString];
}

Opening link in WebView

I have an application at the moment that basically opens a url depending on what the user has typed into a textbox. The logic for this is as such:
Predetermined beginning + User Input + Predetermined End
So basically my URL is 3 concatenated strings. Now I know the link is being formed properly (I've put the same snip of code into a label) but nothing happens when I press the button to load the webview.
It works perfectly fine when I use the below, where I explicitly type https://google.com
- (IBAction)btnSearchPress:(id)sender {
[self.view endEditing:YES];
[super viewDidLoad];
NSString *fullURL = #"https://google.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_wbView loadRequest:requestObj];
}
However nothing happens when I use this code which includes my concatenated url:
- (IBAction)btnSearchPress:(id)sender {
[self.view endEditing:YES];
[super viewDidLoad];
NSString *fullURL = [NSString stringWithFormat:#"%#%#%#", _urlPrefix.text,_txtInput.text, _urlSuffix.text];
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_wbView loadRequest:requestObj];
}
Any help would be much appreciated.
UPDATE: I'm fairly sure having the stringWithFormat is causing this problem just unsure how to get around it.:
Check if fullURL starts with http:// or https://, if not prefix that.
- (IBAction)btnSearchPress:(id)sender {
[self.view endEditing:YES];
[super viewDidLoad];
NSString *fullURL = [NSString stringWithFormat:#"%#%#%#", _urlPrefix.text,_txtInput.text, _urlSuffix.text];
if ( ! ([fullURL hasPrefix:#"http://"] || [ fullURL hasPrefix:#"https://"]) ) {
fullURL = [NSString stringWithFormat:#"http://%#", fullURL ];
}
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_wbView loadRequest:requestObj];
}

Unable to load html file in UIWebView

I have a view controller that has a UIWebView. I want to load a html file in the UIWebView.
I have kept my html file under the Supporting files(Generated by Xcode)->Resources->html.
Also , I have created the html file and dragged and dropped in to the Html folder.
My code:
-(void)viewDidLoad
{
[super viewDidLoad];
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"contact"
ofType:#"html" inDirectory:#"Resources/html"] ;
NSURL *url = [NSURL fileURLWithPath:htmlFile ];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
_webView.delegate=(id)self;
}
What am I missing here . Looks a silly one but no luck..
Please help.
Replace your view did load method with this method. I checked with it. It's working for me. See that your html file should be placed as shown in image.(As per your need)
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIWebView *webView=[[UIWebView alloc]init];
webView.frame=CGRectMake(0, 0, 320, 568);
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"contact" ofType:#"html" inDirectory:nil];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
//Append javascript
//NSString *script = #"<script>alert(\"This is an alert!!\");</script>";
//htmlString = [htmlString stringByAppendingString:script];
webView.delegate = self;
[webView loadHTMLString:htmlString baseURL:nil];
[self.view addSubview:webView];
}

Resources