私のアプリでは、これを使用して Twitter ハンドルからステータスを取得しています: http://api.twitter.com/1/statuses/user_timeline/ABCD.xml しかし、テーブルビューに何も表示されないことがあります。NSXMLParser を使用しています。コードは次のとおりです。
Class A :
xmlParser = [[XMLParser alloc] loadXMLByURL:@"http://api.twitter.com/1/statuses/user_timeline/ABCD.xml"];
XMLParser.m
-(id) loadXMLByURL:(NSString *)urlString
{
//spinnerControl = [[SpinnerController alloc]init];
//[spinnerControl getAlertViewWithSpinner];
tweets = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
parser = [[NSXMLParser alloc] initWithData:data];
NSString * result = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Complete:%@",result);
parser.delegate = self;
[parser parse];
return self;
}
-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementname isEqualToString:@"status"])
{
currentTweet = [Tweet alloc];
}
}
-(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
-(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementname isEqualToString:@"text"])
{
currentTweet.content = currentNodeContent;
}
if (alreadyHaveDate != 1)
{
if ([elementname isEqualToString:@"created_at"])
{
currentTweet.dateCreated = currentNodeContent;
alreadyHaveDate = 1;
}
}
if ([elementname isEqualToString:@"profile_image_url"]) {
currentTweet.artistImage = currentNodeContent;
}
if ([elementname isEqualToString:@"name"]) {
currentTweet.artistName = currentNodeContent;
}
if ([elementname isEqualToString:@"screen_name"]) {
currentTweet.screenName = currentNodeContent;
}
if ([elementname isEqualToString:@"status"])
{
[tweets addObject:currentTweet];
alreadyHaveDate = 0;
}
//[spinnerControl removeAlertView];
}
Tweet.h :
@interface Tweet : NSObject
{
NSString *content;
NSString *dateCreated;
NSString *artistImage;
NSString *artistName;
NSString *screenName;
}
@property (nonatomic, retain) NSString *content;
@property (nonatomic, retain) NSString *dateCreated;
@property (nonatomic, retain) NSString *artistImage;
@property (nonatomic, retain) NSString *artistName;
@property (nonatomic, retain) NSString *screenName;
@end
Tweet.m
@implementation Tweet
@synthesize content, dateCreated,artistImage,artistName,screenName;
@end
特に、デバイスが 3G に接続されている場合。