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

前端 未结 1 1322
夕颜
夕颜 2021-01-22 19:47

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

相关标签:
1条回答
  • 2021-01-22 20:29

    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.

    0 讨论(0)
提交回复
热议问题