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];
}
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
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];
}
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];
}
My UIWebView doesn't seem to be loading:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *urlAddress = [defaults stringForKey:#"webPage"];
NSLog(#"%#", urlAddress);
NSURLRequest *requestObj = [NSURLRequest requestWithURL:[NSURL URLWithString:urlAddress]];
webView.delegate = self;
[webView loadRequest:requestObj];
}
It is in a UIViewController (connected through IB) and urlAddress returns google.com
Can you check if the URL being fetched is valid?
NSString *urlAddress = [defaults stringForKey:#"webPage"];
NSLog(#"%#", urlAddress);
NSURL *u = [NSURL URLWithString:urlAddress];
if(u){
NSURLRequest *requestObj = [NSURLRequest requestWithURL:u];
webView.delegate = self;
[webView loadRequest:requestObj];
}else{
[[[UIAlertView alloc] initWithTitle:#"" message:#"invalid url." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil] show];
}
google.com is not a valid url, it should be http://www.google.com. So maybe that is the issue here.
Try this code:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
webView.delegate = self;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *urlAddress = [defaults stringForKey:#"webPage"];
NSLog(#"%#", urlAddress);
[self openURLFromString:urlAddress];
}
- (void) openURLFromString:(NSString*) urlString
{
NSURL *url = [self validateAddress:urlString];
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url];
[webView loadRequest:request];
}
- (NSURL*) validateAddress:(NSString*) address
{
NSURL* result = [NSURL URLWithString:address];
if (!result.scheme)
{
NSString* modifiedURLString = [NSString stringWithFormat:#"http://%#", address];
result = [NSURL URLWithString:modifiedURLString];
}
return result;
}
#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSMutableURLRequest *)urlRequest navigationType:(UIWebViewNavigationType)navigationType
{
return YES;
}
You're missing base URL part of the URL ( Click here to read more about this ), which is necessary.
So try this:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *urlAddress = [defaults stringForKey:#"webPage"];
NSString * theURL = [NSString stringWithFormat:#"http://%#", urlAddress]; // you can also use stringByAppendingString if you prefer
NSLog(#"%#", theURL);
NSURLRequest *requestObj = [NSURLRequest requestWithURL:[NSURL URLWithString:theURL]];
webView.delegate = self;
[webView loadRequest:requestObj];
}
HTH :)