How do I make a label load content located on a website?

我的梦境 提交于 2020-01-11 10:38:16

问题


I want to make a label which indicates if a band is on tour (System of a down fyi)

So I want a label in the interface to adjust to a value given on my server. ( So it needs to extract data from a (.html) file and display it as a label. )

WebViews are messy in my opinion and a label looks better.


回答1:


I'd recommend using AFNetworking - http://afnetworking.com/. It will allow you to pull data from your server.

For example, you could create an XML file containing the data that you need. Then you can parse it using NSXMLParser and do whatever you need with the data.

Example:

NSURL *feedURL = [[NSURL alloc] initWithString:@"http://yourserver.com/yourxmlfile.xml"];
NSURLRequest *feedRequest = [[NSURLRequest alloc] initWithURL:feedURL];
AFXMLRequestOperation *feedOperation = [AFXMLRequestOperation XMLParserRequestOperationWithRequest:feedRequest success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser) {
    NSLog(@"XMLRequest successful - starting parser..");
    [XMLParser setDelegate:self];
    [XMLParser parse];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSXMLParser *XMLParser) {
    UIAlertView *connectionError = [[UIAlertView alloc] initWithTitle:@"Connection Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
    [connectionError show];
}];

XML Example:

This is the file you will upload to your server. I use the title yourxmlfile.xml in the AFXMLRequestOperation, but call it whatever you want.

<?xml version="1.0"?>
<band>
    <bandname>The Band</bandname>
    <bandontour>1</bandontour>
</band>

Using NSXMLParser (delegation)

Create an ivar (in your .h) to hold the data as it is parsed.

@property (nonatomic, retain) NSString *currentProperty;

This will temporarily hold elements data, then you need to do something with it when the parser reaches didEndElement.

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {    
    if ([elementName isEqualToString:@"bandontour"]) {
        // found the <bandontour> tag
    }    
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    self.currentProperty = string;
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    if ([elementName isEqualToString:@"bandontour"]) {
        // finished getting the data in <bandontour>

        // do something now that you've got your data retrieved
        if (self.currentProperty) int bandOnTour = self.currentProperty.intValue;
        if (bandOnTour == 1) self.yourLabel.text = @"Band is on tour!";
        else self.yourLabel.text = @"Not on tour.";
    }  
}

See NSXMLParser class reference for more information.



来源:https://stackoverflow.com/questions/16043833/how-do-i-make-a-label-load-content-located-on-a-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!