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];
}
Related
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
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.
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];
}
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];
}
I am getting an error in Xcode when adding some labelLoading code in the .m file. Hoping someone can help me out and point out the error in the code. Don't beat me up too bad as I am learning. I just can't get past this error.
#interface ViewController ()
#end
#implementation ViewController
#synthesize viewWeb;
#synthesize labelLoading;
#synthesize tqwWeb;
#synthesize blogWeb;
#synthesize eventsWeb;
#synthesize resWeb;
#synthesize homeWeb;
- (void)viewDidLoad
{
[viewWeb setDelegate:self];
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
[labelLoading setHidden:NO];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[labelLoading setHidden:YES];
}
{ **This is where the error Expected identifier or "(" occurs**
[super viewDidLoad];
NSString *fullURL = #"http://www.rtics.com/DC1/";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[viewWeb loadRequest:requestObj];
[super viewDidLoad];
NSString *afullURL = #"http://www.rtics.com/DC2";
NSURL *aurl = [NSURL URLWithString:afullURL];
NSURLRequest *arequestObj = [NSURLRequest requestWithURL:aurl];
[tqwWeb loadRequest:arequestObj];
[super viewDidLoad];
NSString *bfullURL = #"http:/www.rtics.com/DC3/";
NSURL *burl = [NSURL URLWithString:bfullURL];
NSURLRequest *brequestObj = [NSURLRequest requestWithURL:burl];
[blogWeb loadRequest:brequestObj];
[super viewDidLoad];
NSString *cfullURL = #"https://www.rtics.com/DC4";
NSURL *curl = [NSURL URLWithString:cfullURL];
NSURLRequest *crequestObj = [NSURLRequest requestWithURL:curl];
[eventsWeb loadRequest:crequestObj];
[super viewDidLoad];
NSString *dfullURL = #"http://www.rtics.com/DC5";
NSURL *durl = [NSURL URLWithString:dfullURL];
NSURLRequest *drequestObj = [NSURLRequest requestWithURL:durl];
[resWeb loadRequest:drequestObj];
[super viewDidLoad];
NSString *efullURL = #"http://www.rtics.com/";
NSURL *eurl = [NSURL URLWithString:efullURL];
NSURLRequest *erequestObj = [NSURLRequest requestWithURL:eurl];
[homeWeb loadRequest:erequestObj];
}
- (void)viewDidUnload
{
[self setViewWeb:nil];
[self setLabelLoading:nil];
[self setWebView:nil];
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
#end
The line before your error should have - (void)viewDidLoad and you should only call [super viewDidLoad] once. Move anything in your other viewDidLoad to this new method and delete your other viewDidLoad.
For example:
- (void)viewDidLoad
{ **This is where the error Expected identifier or "(" occurs**
[super viewDidLoad];
[viewWeb setDelegate:self]; // Moved from previous viewDidLoad method
NSString *fullURL = #"http://www.rtics.com/DC1/";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[viewWeb loadRequest:requestObj];
NSString *afullURL = #"http://www.rtics.com/DC2";
NSURL *aurl = [NSURL URLWithString:afullURL];
NSURLRequest *arequestObj = [NSURLRequest requestWithURL:aurl];
[tqwWeb loadRequest:arequestObj];
NSString *bfullURL = #"http:/www.rtics.com/DC3/";
NSURL *burl = [NSURL URLWithString:bfullURL];
NSURLRequest *brequestObj = [NSURLRequest requestWithURL:burl];
[blogWeb loadRequest:brequestObj];
NSString *cfullURL = #"https://www.rtics.com/DC4";
NSURL *curl = [NSURL URLWithString:cfullURL];
NSURLRequest *crequestObj = [NSURLRequest requestWithURL:curl];
[eventsWeb loadRequest:crequestObj];
NSString *dfullURL = #"http://www.rtics.com/DC5";
NSURL *durl = [NSURL URLWithString:dfullURL];
NSURLRequest *drequestObj = [NSURLRequest requestWithURL:durl];
[resWeb loadRequest:drequestObj];
NSString *efullURL = #"http://www.rtics.com/";
NSURL *eurl = [NSURL URLWithString:efullURL];
NSURLRequest *erequestObj = [NSURLRequest requestWithURL:eurl];
[homeWeb loadRequest:erequestObj];
}