无法在 UIWebView 中加载页面

发布于 2024-10-09 08:08:58 字数 2791 浏览 0 评论 0原文

我正在开发一个新闻应用程序,它从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

围归者 2024-10-16 08:08:58

您在哪里用 [item objectForKey:@"link"] 分配 self.title

您是否已将 item 添加为 .h 文件中视图控制器的属性?如果您还没有在上面发布的 viewDidLoad 方法中,您将无法访问 self.item

在您的 .h 文件中:

@interface MyViewController : UIViewController {
    NSDictionary *item;
}
@property (nonatomic, readwrite, retain) NSDictionary *item;

在您的 .m 文件中(调整此项以适合您的 init 方法):

-(id)initWithItem:(NSDictionary *)item {
    if (self = [super initWithNibName: nil bundle: nil]) {
        self.item = item;   
    }
    return self;
}

执行此操作后,您将可以通过 在视图控制器的方法中对 item 属性进行全局访问self.item.

Where 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 your viewDidLoad method you posted above, you would not have access to self.item.

In your .h file:

@interface MyViewController : UIViewController {
    NSDictionary *item;
}
@property (nonatomic, readwrite, retain) NSDictionary *item;

In your .m file (adjust this to suit your init method):

-(id)initWithItem:(NSDictionary *)item {
    if (self = [super initWithNibName: nil bundle: nil]) {
        self.item = item;   
    }
    return self;
}

After you do that you will have global access in your view controller's methods to the item property via self.item.

甜心 2024-10-16 08:08:58

它已经解决了,我已经解决了

NSURL *baseURL = [[NSURL URLWithString: [urlAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] retain]; 

并且现在可以工作,显然我在网址后面发送了一些空格,这就是为什么它不是有效的网址,即使它看起来有效。我 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

NSURL *baseURL = [[NSURL URLWithString: [urlAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] retain]; 

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文