无法在 UIWebView 中加载页面
我正在开发一个新闻应用程序,它从 rss feed 获取内容。 当我单击 TableViewCell 时,我将带有标题、链接等的 NSDictionary 对象传递给下一个 ViewController。在 ViewController 中我定义 NSDictionary *item;我可以通过设置视图控制器的标题来验证值是否正确传递,如下所示: self.title = [item objectForKey:@"link"];标题显示了我尝试在 UIWebView 中打开的链接。
这是我的 ViewController 的实现
//
// NewsDetailViewController.m
// NewsApp2
//
// Created by Marco Soria on 12/27/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "NewsDetailViewController.h"
@implementation NewsDetailViewController
@synthesize item, itemTitle, itemDate, itemSummary;
-(id)initWithItem:(NSDictionary *)theItem{
if(self = [super initWithNibName:@"NewsDetail" bundle:[NSBundle mainBundle]]){
self.item = theItem;
self.title = [item objectForKey:@"link"];
}
return self;
}
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
NSString *urlAddress = [self.item objectForKey:@"link"];
NSURL *baseURL = [[NSURL URLWithString: urlAddress] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:baseURL];
[self.itemSummary loadHTMLString:urlAddress baseURL:nil];
[self.itemSummary loadRequest:request];
[baseURL release];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
现在,当我分配像这样的地址 @"http://somesite.com"
时,UIWebView 加载得很好,但是当我这样做时: NSString *urlAddress = [ self.item objectForKey:@"link"];
它永远不会加载。正如我提到的,我检查了 [self.item objectForKey:@"link"];
的值是一个有效的 url,因为它显示在导航栏的标题中。
如果我这样做: [self.itemSummary loadHTMLString:urlAddress baseURL:nil]; UIWebView 显示 url,这是验证 urlAddress 是否具有正确 url 的另一种方法。
我做错了什么?
I i'm developing a News App that get the content from a rss feed.
When i click on a TableViewCell I pass a NSDictionary object with the title, link, etc to the next ViewController. In the ViewController I define NSDictionary *item; and I can verify that the values are passed correctly by setting the title of the viewcontroller like this: self.title = [item objectForKey:@"link"]; the title shows the link im trying to open in my UIWebView.
Here is my ViewController's implementation
//
// NewsDetailViewController.m
// NewsApp2
//
// Created by Marco Soria on 12/27/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "NewsDetailViewController.h"
@implementation NewsDetailViewController
@synthesize item, itemTitle, itemDate, itemSummary;
-(id)initWithItem:(NSDictionary *)theItem{
if(self = [super initWithNibName:@"NewsDetail" bundle:[NSBundle mainBundle]]){
self.item = theItem;
self.title = [item objectForKey:@"link"];
}
return self;
}
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
NSString *urlAddress = [self.item objectForKey:@"link"];
NSURL *baseURL = [[NSURL URLWithString: urlAddress] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:baseURL];
[self.itemSummary loadHTMLString:urlAddress baseURL:nil];
[self.itemSummary loadRequest:request];
[baseURL release];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
Now, when I assign the address like this @"http://somesite.com"
the UIWebView loads just fine but when i do this: NSString *urlAddress = [self.item objectForKey:@"link"];
it never loads. As I mentioned i checked that the value of [self.item objectForKey:@"link"];
is a valid url since its displaying it in the title of the navigationbar.
if I do this: [self.itemSummary loadHTMLString:urlAddress baseURL:nil];
the UIWebView displays the url, another way of verifying that urlAddress has the correct url.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在哪里用
[item objectForKey:@"link"]
分配self.title
?您是否已将
item
添加为 .h 文件中视图控制器的属性?如果您还没有在上面发布的viewDidLoad
方法中,您将无法访问self.item
。在您的 .h 文件中:
在您的 .m 文件中(调整此项以适合您的 init 方法):
执行此操作后,您将可以通过
在视图控制器的方法中对
.item
属性进行全局访问self.itemWhere do you assign
self.title
with[item objectForKey:@"link"]
?Have you added
item
as a property of your view controller in your .h file? If you haven't then in yourviewDidLoad
method you posted above, you would not have access toself.item
.In your .h file:
In your .m file (adjust this to suit your init method):
After you do that you will have global access in your view controller's methods to the
item
property viaself.item
.它已经解决了,我已经解决了
并且现在可以工作,显然我在网址后面发送了一些空格,这就是为什么它不是有效的网址,即使它看起来有效。我 NSLog 并得到了 baseUrl http://site.com/item.php?id=2458%20%0A%09%20%20%20% 20%20%20%20%20%09%20%20%20%20%09%09%09%09
It's solved, i did
and works now, apparently I was sending some spaces after the url and that's why it wasn't a valid url even though it apeared valid. I NSLog and got baseUrl http://site.com/item.php?id=2458%20%0A%09%20%20%20%20%20%20%20%20%09%20%20%20%20%09%09%09%09