这个位很麻烦。。。。
#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
如果出现问题,我似乎不知道是什么原因造成的。我已经在谷歌上搜索了这些垃圾,并一遍又一遍地检查了我的代码,但我似乎不明白这一点!
请帮忙。
你以前也问过这个问题,我回答了。我在下面重复该答复的有关部分。如果有什么不合理的地方,请在下面留言。
我猜您正试图在UIWebView
中加载html页面?你的UIWebView
显然需要一个IBOutlet
。(如果你不熟悉IBOutlet
,请查看苹果教程你的第一个iOS应用程序。)
无论如何,在下面的例子中,我假设你的IBOutlet
被称为webview
,因此我可能建议去掉NSOperationQueue
和NSUrlConnection
,让UIWebView
为你加载html:
- (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];
}
浏览一些iPhone编程教程(只需在谷歌上搜索,你就会获得大量点击),或者查看《苹果应用程序编程指南》,或者查看上的精彩资源http://developer.apple.com.
更新:
顺便说一句,如果你坚持使用NSOperationQueue
和NSUrlConnection
,你的网络视图仍然需要一个IBOutlet
。但修改后的代码看起来像:
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");
}
}];
我认为loadRequest
要简单得多,但如果你真的想这样做,那就开始吧。
'我正在尝试使用uiwebview来启动一个web应用程序。如果你知道什么更好的方法,请告诉我!">
[theWebView loadRequest:[NSURLRequest requestWithURL:theURL]];
您有3个不同的错误。首先,"(UIWebView)"没有任何意义,其次,如果你有一个确实有意义的语句,它后面需要一个分号。第三,NSLog后面应该有一个分号(@"Error:%@",Error)。为了可读性,最好将一些代码放在单独的行中——它应该看起来像这样:
[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);
}];
话虽如此,你应该使用@Rob发布的方法来让你的代码工作。