ChangUZ
ChangUZ

Reputation: 5438

iPhone, NSURLConnection > didReceiveData:(NSData *)data, How to check result data

I use url connection (http).

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

        NSString *filePath; /* .../link.plist */
        [data writeToFile:filePath atomically:YES];
    }

After connection, I check result file (link.plist)

    - (void)checkLinkResult {

        NSString *filePath; //link.plist
        NSString *result = [[NSString alloc] initWithContentsOfFile:filePath];
    }

It works fine.

But I want to check Result String, directly, without making file.

"NSData -> file -> NSString" (now) ====> "NSData -> NSString" (i want)

Help me plz.

Upvotes: 0

Views: 2503

Answers (3)

Sepehr Mahmoudian
Sepehr Mahmoudian

Reputation: 771

Follow the following steps:

  1. Declare a file scope NSMutableData instance.
  2. in your connection:DidRecieveData: callback append "data" to the previously created NSMutableData instance.
  3. In connectionDidFinishLoading: callback use initWithData:encoding: of NSString and pass the NSMutableData instance and NSUTF8StringEncoding as parameters.

Upvotes: 0

Chau Than
Chau Than

Reputation: 150

It's dependent your data.

If your data is Image

UIIMage * image = [[UIImage alloc] initWithData:Receivedata

If data is string

NSMutableString *string = [[NSMutableString alloc] initwithData:Receivedata encoding:nil]

Upvotes: 1

Tyler
Tyler

Reputation: 2907

In didReceiveData write to an NSMutableData object, then in checkLinkResult create a string from the NSMUtableData using the appropriate encoding.

It's actually how it's done in the apple NSConnection tutorial.

Upvotes: 0

Related Questions