Objective-c-url-loading-system

提供:Dev Guides
移動先:案内検索

Objective-CのURLロードシステム

URLの読み込みは、URL、つまりインターネットからのアイテムにアクセスするのに役立ちます。 それは、次のクラスの助けを借りて提供されています-

  • NSMutableURLRequest
  • NSURLConnection
  • NSURLCache
  • NSURLAuthenticationChallenge
  • NSURLCredential
  • NSURLProtectionSpace
  • NSURLResponse
  • NSURLダウンロード *NSURLSession

URL読み込みの簡単な例を次に示します。 これはコマンドラインでは実行できません。 Cocoaアプリケーションを作成する必要があります。

これを行うには、XCodeで[新規]を選択し、[プロジェクト]を選択して、表示されるウィンドウの[OS Xアプリケーション]セクションで[Cocoaアプリケーション]を選択します。

[次へ]をクリックして一連の手順を完了すると、プロジェクト名を指定するように求められ、名前を付けることができます。

appdelegate.hファイルは次のようになります-

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow* window;

@end

AppDelegate.mファイルを次のように更新します-

#import "AppDelegate.h"

@interface SampleClass:NSObject<NSURLConnectionDelegate> {
   NSMutableData *_responseData;
}

- (void)initiateURLConnection;
@end

@implementation SampleClass
- (void)initiateURLConnection {

  //Create the request.
   NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://date.jsontest.com"]];

  //Create url connection and fire request
   NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
   [conn start];
}

#pragma mark NSURLConnection Delegate Methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
  //A response has been received, this is where we initialize the instance var you created
  //so that we can append data to it in the didReceiveData method
  //Furthermore, this method is called each time there is a redirect so reinitializing it
  //also serves to clear it
   _responseData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
  //Append the new data to the instance variable you declared
   [_responseData appendData:data];
}

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
   willCacheResponse:(NSCachedURLResponse*)cachedResponse {
  //Return nil to indicate not necessary to store a cached response for this connection
   return nil;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
  //The request is complete and data has been received
  //You can parse the stuff in your instance variable now
   NSLog(@"%@",[[NSString alloc]initWithData:_responseData encoding:NSUTF8StringEncoding]);
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
  //The request has failed for some reason!
  //Check the error var
}
@end

@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
   SampleClass *sampleClass = [[SampleClass alloc]init];
   [sampleClass initiateURLConnection];
  //Insert code here to initialize your application
}
@end

プログラムをコンパイルして実行すると、次の結果が得られます。

2013-09-29 16:50:31.953 NSURLConnectionSample[1444:303] {
   "time": "11:20:31 AM",
   "milliseconds_since_epoch": 1380453631948,
   "date": "09-29-2013"
}

上記のプログラムでは、JSON形式で時間をとり、時間を表示する簡単なURL接続を作成しました。