[twitter-dev] iPhone image uploading

2009-12-17 Thread Infinity 320
Hello guys!

I have found on the twitter-site this faq:

 The image update methods require multipart form data. They do not accept a 
 URL to an image not do they accept the raw image bytes. They instead require 
 the data to be delivered in the form of a file upload filed as defined in 
 RFC1867. The content-type attribute of the image field is checked for valid 
 image type. If you are using PHP/CURL there is a known bug that has since 
 been fixed in the CVS version of PHP. Most installations are not yet using 
 this version and therefore fail during image upload.

I wrote a little code to upload a profile image to my twitter, but
somethings wrong.
Here's my code:

- (void)uploadProfileImage {
UIImage *myImage = [UIImage imageNamed:@pic1.png];
NSData *myImageData = [[NSData alloc]
initWithData:UIImagePNGRepresentation(myImage)];
NSString *bodyString = [NSString stringWithFormat:@image=%@,
[[[NSString alloc] initWithData:myImageData
encoding:NSUTF8StringEncoding] autorelease]];

NSString *apiUrl = @http://twitter.com/account/
update_profile_image.json;

NSURL *url = [NSURL URLWithString:apiUrl];
NSMutableURLRequest *updateRequest = [NSMutableURLRequest
requestWithURL:url

 
cachePolicy:NSURLRequestReloadIgnoringCacheData

 
timeoutInterval:TWITTER_SEND_UPDATE_TIMEOUT];
[updateRequest setHTTPShouldHandleCookies:NO];
[updateRequest setHTTPMethod:@POST];
[updateRequest setValue:@application/x-www-form-urlencoded
forHTTPHeaderField:@Content-Type];
[updateRequest setHTTPBody:[bodyString
dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];
[updateRequest setValue:[NSString stringWithFormat:@%d, [bodyString
length]] forHTTPHeaderField:@Content-Length];

NSLog(@Trying to connect...);
NSURLConnection *theConnection = [[NSURLConnection alloc]
initWithRequest:updateRequest delegate:self];

if (theConnection) {
//_receivedData = [[NSMutableData data] retain];
NSLog(@Connected!);
} else {
NSLog(@Not connected!);
// Inform the user that the download could not be made
}
}


And one more method needed:

- (void)connection:(NSURLConnection *)connection
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)
challenge {
if ([challenge previousFailureCount] == 0) {
NSURLCredential *newCredential = [NSURLCredential
credentialWithUser:@username password:@password
persistence:NSURLCredentialPersistenceNone];
[[challenge sender] useCredential:newCredential
forAuthenticationChallenge:challenge];
} else {
[[challenge sender] cancelAuthenticationChallenge:challenge];
NSLog(@Invalid username and password!);
}
}


And with this method I am receiving the information from twitter:

- (void)connection:(NSURLConnection *)connection didReceiveData:
(NSData *)data {
NSLog([[[NSString alloc] initWithData:data
encoding:NSASCIIStringEncoding] autorelease]);
}

But I am getting some strange error. The response isn't a json answer,
it is a html-like answer. Maybe do you have any idea why is that?


Re: [twitter-dev] iPhone image uploading

2009-12-17 Thread Andrew Badera
If that really a response from Twitter? Looks more like it's on your
app's side (SWAG) ...

∞ Andy Badera
∞ +1 518-641-1280 Google Voice
∞ This email is: [ ] bloggable [x] ask first [ ] private
∞ Google me: http://www.google.com/search?q=andrew%20badera



On Thu, Dec 17, 2009 at 9:54 AM, Infinity 320 infinity...@gmail.com wrote:
 Hello guys!

 I have found on the twitter-site this faq:

 The image update methods require multipart form data. They do not accept a 
 URL to an image not do they accept the raw image bytes. They instead require 
 the data to be delivered in the form of a file upload filed as defined in 
 RFC1867. The content-type attribute of the image field is checked for valid 
 image type. If you are using PHP/CURL there is a known bug that has since 
 been fixed in the CVS version of PHP. Most installations are not yet using 
 this version and therefore fail during image upload.

 I wrote a little code to upload a profile image to my twitter, but
 somethings wrong.
 Here's my code:

    - (void)uploadProfileImage {
        UIImage *myImage = [UIImage imageNamed:@pic1.png];
        NSData *myImageData = [[NSData alloc]
 initWithData:UIImagePNGRepresentation(myImage)];
        NSString *bodyString = [NSString stringWithFormat:@image=%@,
 [[[NSString alloc] initWithData:myImageData
 encoding:NSUTF8StringEncoding] autorelease]];

        NSString *apiUrl = @http://twitter.com/account/
 update_profile_image.json;

        NSURL *url = [NSURL URLWithString:apiUrl];
        NSMutableURLRequest *updateRequest = [NSMutableURLRequest
 requestWithURL:url
                                                                               
                                                   
 cachePolicy:NSURLRequestReloadIgnoringCacheData
                                                                               
                                           
 timeoutInterval:TWITTER_SEND_UPDATE_TIMEOUT];
        [updateRequest setHTTPShouldHandleCookies:NO];
        [updateRequest setHTTPMethod:@POST];
        [updateRequest setValue:@application/x-www-form-urlencoded
 forHTTPHeaderField:@Content-Type];
        [updateRequest setHTTPBody:[bodyString
 dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];
        [updateRequest setValue:[NSString stringWithFormat:@%d, [bodyString
 length]] forHTTPHeaderField:@Content-Length];

        NSLog(@Trying to connect...);
        NSURLConnection *theConnection = [[NSURLConnection alloc]
 initWithRequest:updateRequest delegate:self];

        if (theConnection) {
                //_receivedData = [[NSMutableData data] retain];
                NSLog(@Connected!);
        } else {
                NSLog(@Not connected!);
                // Inform the user that the download could not be made
        }
 }


 And one more method needed:

    - (void)connection:(NSURLConnection *)connection
 didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)
 challenge {
        if ([challenge previousFailureCount] == 0) {
                NSURLCredential *newCredential = [NSURLCredential
 credentialWithUser:@username password:@password
 persistence:NSURLCredentialPersistenceNone];
                [[challenge sender] useCredential:newCredential
 forAuthenticationChallenge:challenge];
        } else {
                [[challenge sender] cancelAuthenticationChallenge:challenge];
                NSLog(@Invalid username and password!);
        }
 }


 And with this method I am receiving the information from twitter:

    - (void)connection:(NSURLConnection *)connection didReceiveData:
 (NSData *)data {
        NSLog([[[NSString alloc] initWithData:data
 encoding:NSASCIIStringEncoding] autorelease]);
 }

 But I am getting some strange error. The response isn't a json answer,
 it is a html-like answer. Maybe do you have any idea why is that?