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.