2009-06-20 3 views
0

を使用することが可能ですか?NSMutableArrayのwriteToUrl

たとえば、 私は配列を持っており、特定のURLにアップロードしたいと思います。次にアプリケーションが起動するときに、その配列をダウンロードしたいと思います。

NSMutableArray *arrayToWrite = [[NSMutableArray alloc] initWithObjects:@"One",@"Two",nil]; 

[arrayToWrite writeToURL: 

[NSURL urlWithString:@"mywebsite.atwebpages.com/myArray.plist"] atomically:YES]; 

、実行時に:

NSMutableArray *arrayToRead = 

[[NSMutableArray alloc] initWithContentsOfURL:[NSURL   urlWithString:@"mywebsite.atwebpages.com/myArray.plist"]]; 

意味、私はウェブホスティングサービス(例えばbatcave.net上にあるURLにNSMutableArrayのを書きたい、URLは、情報と更新を受け取りますサーバーサイドのファイルに応じて。 は、セットアップのようなハイスコアは、ユーザーが自分のスコア、それはファイルは、他のユーザーが実行時にハイスコアをダウンロードしますサーバーのアップデートを送信します。あなたの質問、 のパート1については

+0

あなたの質問にはもっと明確にする必要があります。「URLに書き込む」とはどういう意味ですか?ホストのURLに配列の内容を追加しますか?または、アレイ内のデータをウェブサイトの特定の場所にアップロードしたいですか? – Tim

+0

[iPhoneのようなハイスコアのシステムを作成する]の可能な複製(https://stackoverflow.com/questions/1023289/creating-a-highscore-like-system-iphone-side) –

答えて

1

私は、あなたがしたいと仮定しますつかいます NSMutableArrayの内容は、あなたのWebサービスに送信し、いくつかの情報を返すような何らかの種類のURLリクエスト(POSTなど)を形成します。 URLはありますが、これを自分で行う簡単な方法があります。たとえば、配列のデータをループし、NSURLRequestを使用して、Webサービスのインターフェイスに準拠したURL要求を作成できます。リクエストを作成したら、NSURLConnectionオブジェクトを渡して送信することができます。

は、このクライアント側のコードは、データを提供するためのObj-C配列を使用してどのように見えるかの非常にシンプルかつ不完全な例...

NSMutableData *dataReceived; // Assume exists and is initialized 
NSURLConnection *myConnection; 

- (void)startRequest{ 
    NSLog(@"Start"); 

    NSString *baseURLAddress = @"http://en.wikipedia.org/wiki/"; 

    // This is the array we'll use to help make the URL request 
    NSArray *names = [NSArray arrayWithObjects: @"Jonny_Appleseed",nil]; 
    NSString *completeURLAsString = [baseURLAddress stringByAppendingString: [names objectAtIndex:0]]; 

    //NSURLRequest needs a NSURL Object 
    NSURL *completeURL = [NSURL URLWithString: completeURLAsString]; 

    NSURLRequest *myURLRequest = [NSURLRequest requestWithURL: completeURL]; 

    // self is the delegate, this means that this object will hanlde 
    // call-backs as the data transmission from the web server progresses 
    myConnection = [[NSURLConnection alloc] initWithRequest:myURLRequest delegate: self startImmediately:YES]; 
} 

// This is called automatically when there is new data from the web server, 
// we collect the server response and save it 
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    NSLog(@"Got some"); 
    [dataReceived appendData: data]; 
} 

// This is called automatically when transmission of data is complete 
- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    // You now have whatever the server sent... 
} 

あなたの質問の一部2に取り組むために、受信機を検討します有用な応答を得るためには、スクリプトやインフラストラクチャが必要になる可能性があります。ここで