2010年9月30日木曜日

Objective-C + libxml2 を使ってフィードを取り込みたいんだ (2)

前回書き忘れてた。Xcode 3.2.4 and iOS SDK 4.1 で試してる。
古いバージョンとか新しいバージョンのことは気にしない。

libxmlの前に NSURLConnection を使ってサーバにデータを取りにいく必要があるのでそっちから進める。
けど、説明は簡単に終わらせる。

NSURLConnection のメソッド initWithRequest:delegate: を呼び出すとサーバに接続して結果を返してくれる。
そんで、結果を返してくれる度にその結果に対応した NSURLConnection のデリゲートメソッドが実行されるようになってる。

  1. @implementation RSSImporter  
  2.   
  3. /** 
  4.  とりあえず init に NSURLConnection の処理を書いてみる。 
  5.  */  
  6. - (id)init {  
  7.   
  8. self = [super init];  
  9.    if (self != nil) {  
  10.       NSString *feedURL = @"http://eyesrobe.blogspot.com/feeds/posts/default?alt=rss";  
  11.       NSURLRequest *requestURL = [NSURLRequest requestWithURL:[NSURL URLWithString:feedURL]];  
  12.       NSURLConnection *urlConnection = [[[NSURLConnection alloc] initWithRequest:requestURL delegate:self] autorelease];  
  13.       if (urlConnection == nil) {  
  14.          NSLog(@"error");  
  15.       }  
  16.    }  
  17.   
  18.    return self;  
  19. }  
  20.   
  21.   
  22.   
  23. - (void)dealloc {  
  24.    [super dealloc];  
  25. }  
  26.   
  27.   
  28. #pragma mark NSURLConnection Delegate methods  
  29. //  
  30. // ここから下のメソッドが、NSURLConnectionのデリゲートメソッド  
  31. //  
  32.   
  33. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {  
  34.    // データ受信  
  35.    NSLog(@"受信中(データは分割されて受信される)");  
  36. }  
  37.   
  38. - (void)connectionDidFinishLoading:(NSURLConnection *)connection {  
  39.    // 受信完了  
  40.    NSLog(@"受信完了");  
  41. }  
  42.   
  43. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {  
  44.    // 受信エラー  
  45.    NSLog(@"エラー");  
  46. }  
  47.   
  48. @end  


NSURLConnection の説明終わり。
ここまでで取り敢えず実行させてみたい。
実行させるにはどこかでRSSImporterのインスタンスを作らないとなので、作る。
{YourProject}AppDelegate.m/.h でやることにする。

  1. //  
  2. // {YourProject}AppDelegate.m  
  3. // コードの{YourProject}は自分のプロジェクト名に置き換えてね。  
  4. //  
  5. // ここでは、RSSImporterクラスの使い方の説明しようとしてるだけなの  
  6. // で、「// これ追加」ってところを自分の環境にあわせて追加すること。  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. @class {YourProject}ViewController;  
  12. @class RSSImporter; // これ追加  
  13.   
  14. @interface {YourProject}AppDelegate : NSObject <UIApplicationDelegate> {  
  15.    UIWindow *window;  
  16.    {YourProject}ViewController *viewController;  
  17.    RSSImporter *importer; // これ追加  
  18. }  
  19.   
  20. @property (nonatomic, retain) IBOutlet UIWindow *window;  
  21. @property (nonatomic, retain) IBOutlet {YourProject}ViewController *viewController;  
  22. @property (nonatomic, retain) RSSImporter *importer; // これ追加  
  23.   
  24. @end  



  1. //  
  2. // {YourProject}AppDelegate.h  
  3. // コードの{YourProject}は自分のプロジェクト名に置き換えてね  
  4. //  
  5. // ここでは、RSSImporterクラスの使い方の説明しようとしてるだけなの  
  6. // で、「// これ追加」ってところを自分の環境にあわせて追加すること。  
  7. //  
  8.   
  9. #import "{YourProject}AppDelegate.h"  
  10. #import "{YourProject}ViewController.h"  
  11. #import "RSSImporter.h" // これ追加  
  12.   
  13. @implementation {YourProject}AppDelegate  
  14.   
  15. @synthesize window;  
  16. @synthesize viewController;  
  17. @synthesize importer; // これ追加  
  18.   
  19. #pragma mark -  
  20. #pragma mark Application lifecycle  
  21.   
  22. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {      
  23.       
  24.    importer = [[RSSImporter alloc] init]; // これ追加  
  25.   
  26.    [window addSubview:viewController.view];  
  27.    [window makeKeyAndVisible];  
  28.    return YES;  
  29. }  
  30.   
  31. ~(略)~  
  32.   
  33. - (void)dealloc {  
  34.    [importer release]; // これ追加  
  35.    [viewController release];  
  36.    [window release];  
  37.    [super dealloc];  
  38. }  
  39.   
  40. @end  



編集終わったら、ビルドと実行を行う。
そうするとコンソールに、

[Session started at 2010-09-30 15:21:35 +0900.]
2010-09-30 15:21:39.928 Foo[1715:207] 受信中(データは分割されて受信される)
2010-09-30 15:21:39.931 Foo[1715:207] 受信中(データは分割されて受信される)
2010-09-30 15:21:39.962 Foo[1715:207] 受信中(データは分割されて受信される)
2010-09-30 15:21:39.964 Foo[1715:207] 受信中(データは分割されて受信される)
2010-09-30 15:21:39.966 Foo[1715:207] 受信中(データは分割されて受信される)
2010-09-30 15:21:39.967 Foo[1715:207] 受信完了


な感じで表示さるので、これで NSURLConnection については理解できたということにする。

0 件のコメント:

コメントを投稿