rss中的WebView无法正常工作

时间:2015-08-05 06:24:51

标签: ios objective-c rss rss-reader

您好我在Xcode中创建了Rss Feeder,而我的网页视图无效  为什么不工作?

APPMasterViewController

 - (void)viewDidLoad {
        [super viewDidLoad];
        feeds = [[NSMutableArray alloc] init];
        NSURL *url1 = [NSURL URLWithString:@"http://raphaelsebban.com/feed/"];
        parser = [[NSXMLParser alloc] initWithContentsOfURL:url1];

        [parser setDelegate:self];
        [parser setShouldResolveExternalEntities:NO];
        [parser parse];
  }

    - (UITableViewCell *)tableView:(UITableView *)tableView 
                         cellForRowAtIndexPath:(NSIndexPath *)indexPath    
  {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"title"];
    return cell;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
        namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
        attributes:(NSDictionary *)attributeDict {

    element = elementName;        
    if ([element isEqualToString:@"item"]) {

        item    = [[NSMutableDictionary alloc] init];
        title   = [[NSMutableString alloc] init];
        url   = [[NSMutableString alloc] init];

    }        
 }

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
        namespaceURI:(NSString *)namespaceURI 
        qualifiedName:(NSString *)qName {

    if ([elementName isEqualToString:@"item"]) {

        [item setObject:title forKey:@"title"];
        [item setObject:url forKey:@"link"];            
        [feeds addObject:[item copy]];

    }

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

    if ([element isEqualToString:@"title"]) {
        [title appendString:string];
    } else if ([element isEqualToString:@"link"]) {
        [url stringByAppendingString:string];
    }        
}

    - (void)parserDidEndDocument:(NSXMLParser *)parser {            
        [self.tableView reloadData];            
    }

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        if ([[segue identifier] isEqualToString:@"ShowDetail"]) {                
            NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
            NSString *string = [feeds[indexPath.row] objectForKey: @"link"];
            [[segue destinationViewController] setUrl:string];                
        }
    }

APPDetailViewController

- (void)viewDidLoad {
    [ super viewDidLoad];
     NSURL *myURL = [NSURL URLWithString: [self.url stringByAddingPercentEscapesUsingEncoding:                                           NSUTF16StringEncoding]];
     NSURLRequest *request = [NSURLRequest requestWithURL:myURL];
    [self.webView loadRequest:request];        
    [self webViewDidFinishLoad:_webView];
   }


-(void)webViewDidFinishLoad:(UIWebView *)webView {    
   NSLog(@"method was called");    
}

这是我的日志:

>2015-08-05 09:10:51.810 #raph[4226:436663] method was called

似乎无效,我的webView仍为白色

3 个答案:

答案 0 :(得分:1)

终于它的工作了!不要忘记之前创建IBOutlet。

祝你好运!

这是我的代码:

<强> APPMasterViewcontroller

    - (void)viewDidLoad {
    [super viewDidLoad];
    feeds = [[NSMutableArray alloc] init];
    NSURL *rssUrl = [NSURL URLWithString:@"yourlink/feed/"]; 
    parser = [[NSXMLParser alloc] initWithContentsOfURL:rssUrl];

    [parser setDelegate:self];
    [parser setShouldResolveExternalEntities:NO];
    [parser parse];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return feeds.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"title"];
    return cell;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {

    element = elementName;

    if ([element isEqualToString:@"item"]) {

        item    = [[NSMutableDictionary alloc] init];
        title   = [[NSMutableString alloc] init];
        url   = [[NSMutableString alloc] init];

    }

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

    if ([elementName isEqualToString:@"item"]) {

        [item setObject:title forKey:@"title"];
        [item setObject:url forKey:@"link"];

        [feeds addObject:[item copy]];

    }

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

    if ([element isEqualToString:@"title"]) {
        [title appendString:string];
    } else if ([element isEqualToString:@"link"]) {
        [url appendString:string];
    }

}

- (void)parserDidEndDocument:(NSXMLParser *)parser {

    [self.tableView reloadData];

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    //if ([[segue identifier] isEqualToString:@"showDetail"]) {

        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

        NSString *string = [feeds[indexPath.row] objectForKey: @"link"];

        [[segue destinationViewController] setUrl:string];
    //}
}


**APPDetailViewController**

- (void)viewDidLoad {
        [super viewDidLoad];
        NSURL *myURL = [NSURL URLWithString: [self.url stringByAddingPercentEscapesUsingEncoding:                                           NSUTF16StringEncoding]];
        NSString *urlString=[self.url stringByReplacingOccurrencesOfString:@"\n\t\t" withString:@""];
        myURL =[NSURL URLWithString:urlString];
        NSLog(@"url=%@",urlString);

        NSURLRequest *request = [NSURLRequest requestWithURL:myURL];
        [self.webView loadRequest:request];
    }

答案 1 :(得分:0)

我有同样的错误。有些URL已打开但有些只显示白屏。所以我做了这个更改,现在总是webview加载网址。

这是我的代码:

-(void)addWebView {
   webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64,self.view.frame.size.width, self.view.frame.size.height)];
   webView.backgroundColor = [UIColor clearColor];
   webView.delegate = self;

  [self.view addSubview:webView];
  [webView addSubview:self.actInd];

  //self.linkURL  is some URL NSString (not NSURL)
  NSString *htmlStr  = [NSString stringWithFormat:@"<script>window.location=%@;</script>",[[NSString alloc]
                                                                                          initWithData:[NSJSONSerialization dataWithJSONObject:self.linkURL options:NSJSONReadingAllowFragments error:NULL] encoding:NSUTF8StringEncoding]];

  [webView loadHTMLString:htmlStr baseURL:nil];

  webView.scalesPageToFit = YES;
}

希望得到这个帮助并解决你的问题

答案 2 :(得分:0)

您使用过'http://chicago.craigslist.org/search/ata?format=rss'吗?还是'http://chicago.craigslist.org/search/ata?format=rss&format=rss'?您必须使用第二个,因为它指定了类别。

并检查你的问题或不知道。