-2

This bit is being troublesome....

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//1
NSString *urlString = @"http://zaphod_beeblebrox.pythonanywhere.com/";
//2
NSURL *url = [NSURL URLWithString:urlString];
//3
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//4
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
//5
[NSURLConnection sendAsynchronousRequest:request queue:queue
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError         *error) {
                           if ([data length] > 0 && error == nil) (UIWebView)
                        else if ((error != nil) NSLog(@"Error: %@", error))}];

}@end

I can't seem to figure out what is causing else if to have problems. I have googled the crap out of this and have checked my code over and over, but I cant seem to figure this out!

please help.

  • What is the problem you are observing? It seems that your code is incomplete as posted. Are you getting a compiler error? – Greg Hewgill Oct 04 '12 at 22:48
  • I can't seem to figure out what you are doing. At all. – Bergasms Oct 04 '12 at 22:50
  • This code makes no sense: `if (…) (UIWebView) else if …`. What are you trying to do? – Marcelo Cantos Oct 04 '12 at 22:51
  • else if keeps getting an expected expression error, I have no extra semi colons or anything else that most people have when this error comes up. I haven't had any compiler errors yet. I am trying to use a uiwebview to bring up a web app. If you know of any better way, please let me know! Here is the sight where I got most of the code: [link] (http://www.roseindia.net/tutorial/iphone/examples/nslog/nslognsstringexample.html) – user1718637 Oct 04 '12 at 22:56
  • Why are you using `NSURLConnection`? In my answer below, I show you a much simpler way of doing it, using `loadRequest`. But I also show you how to fix this code if you really want to do it this way. – Rob Oct 05 '12 at 17:26

3 Answers3

2

You asked this same question before, which I answered. I repeat the relevant portions of that answer below. If there's something that didn't make sense, just leave a comment below.


I'm guessing you're trying to load a html page in a UIWebView? You obviously need an IBOutlet for your UIWebView. (If you're not familiar with IBOutlet, check out the Apple tutorial Your First iOS App.)

Anyway, in my examples below, I'm going to assume your IBOutlet is called webview, and thus I might advise getting rid of the NSOperationQueue and NSUrlConnection and just have the UIWebView load the html for you:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString     *urlString = @"http://zaphod_beeblebrox.pythonanywhere.com/";
    NSURL        *url       = [NSURL URLWithString:urlString];
    NSURLRequest *request   = [NSURLRequest requestWithURL:url];

    [self.webview loadRequest:request];
}

It might be worth going through a few iPhone programming tutorials (just google it and you'll get tons of hits), too or look at the Apple App Programming Guide or check out the wonderful resources at http://developer.apple.com.


Update:

By the way, if you insist on using NSOperationQueue and NSUrlConnection, you still need an IBOutlet for your webview. But the revised code would look like:

NSString         *urlString = @"http://zaphod_beeblebrox.pythonanywhere.com/";
NSURL            *url       = [NSURL URLWithString:urlString];
NSURLRequest     *request   = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue     = [[NSOperationQueue alloc] init];

[NSURLConnection sendAsynchronousRequest:request
                                   queue:queue
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                           if ([data length] > 0 && error == nil)
                           {
                               NSString *htmlString = [NSString stringWithUTF8String:data.bytes];
                               [self.webview loadHTMLString:htmlString baseURL:url];
                           }
                           else if (error != nil)
                           {
                               NSLog(@"Error: %@", error);
                           }
                           else
                           {
                               NSLog(@"No data returned");
                           }
                       }];

I think loadRequest is much simpler, but if you really want to do it this way, here you go.

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044
1

' I am trying to use a uiwebview to bring up a web app. If you know of any better way, please let me know!'

[theWebView loadRequest:[NSURLRequest requestWithURL:theURL]];
Bergasms
  • 2,203
  • 1
  • 18
  • 20
1

You have 3 different errors. First "(UIWebView)" doesn't mean anything, and secondly, if you had a statement there that did mean something it would need a semicolon after it. Thirdly, there should be a semicolon after NSLog(@"Error: %@", error). It would be better to put some of the code on separate lines for readability -- it should look like this:

[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    if ([data length] > 0 && error == nil)
        ;//a valid expression here with a semicolon at the end
    else if (error != nil)
        NSLog(@"Error: %@", error);
 }];

That being said, you should use the method that @Rob posted to make your code work.

rdelmar
  • 103,982
  • 12
  • 207
  • 218