Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If I were you, I'd check out the ASIHTTPRequest library. It's an HTTP client library for Cocoa that makes life EVER so much easier for people who do a lot of web interactions from their iPhone apps.</p> <p>Here's me, using ASIFormDataRequest (a component of ASIHTTPRequest) to upload an image from my iPhone app. The client's term for these images is "marks"--they're going on a map of local pictures taken by users all around the local area. Users are invited to "make your mark". You can imagine the hilarity that ensues.</p> <p>Anyhoo, <code>self.mark</code> is an instance of my Mark class that encapsulates the data about an image-and-details package I'm uploading. I have a data manager singleton I use in the first line of this method, which contains the current CLLocation, so I can get geocode info for this picture.</p> <p>Notice I don't concern myself with encoding types or multipart boundaries. The library handles all that. </p> <pre><code>-(void)completeUpload { CLLocation *currentLoc = [DataManager sharedDataManager].currentLocation; self.mark.latitude = [NSNumber numberWithDouble:currentLoc.coordinate.latitude]; self.mark.longitude = [NSNumber numberWithDouble:currentLoc.coordinate.longitude]; ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL: [NSURL URLWithString:[NSString stringWithFormat:@"%@image_upload.php", WEBAPPURL]]]; [request setPostValue:self.mark.titleText forKey:@"title"]; [request setPostValue:self.mark.descriptionText forKey:@"description"]; [request setPostValue:self.mark.event.guid forKey:@"event"]; [request setPostValue:self.mark.latitude forKey:@"latitude"]; [request setPostValue:self.mark.longitude forKey:@"longitude"]; [request setPostValue:self.mark.project forKey:@"project"]; [request setPostValue:[[NSUserDefaults standardUserDefaults] valueForKey:@"userID"] forKey:@"user_id"]; request.timeOutSeconds = 120; int i = 1; for (NSString *tag in self.tags) { [request setPostValue:tag forKey:[NSString stringWithFormat:@"tag-%d", i]]; i++; } NSData *imageData = UIImagePNGRepresentation(self.mark.image); NSData *thumbData = UIImagePNGRepresentation(self.mark.thumbnail); [request setData:imageData forKey:@"file"]; [request setData:thumbData forKey:@"thumb"]; self.progress.progress = 20.0; [request setUploadProgressDelegate:self.progress]; request.showAccurateProgress = YES; request.delegate = self; [request startAsynchronous]; } </code></pre> <p>EDIT: By the way, the PHP script I'm posting to is behind HTTP authentication. ASI caches those credentials, and I provided them earlier, so I don't have to provide them again here. I note that because otherwise, the way this post and the corresponding PHP script is written, anybody could fake anybody else's user ID value and post whatever under their username. You have to think about web-application security when you build an app like this, no different than if it was a web site. It IS a web site, actually, just browsed through a non-traditional client.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload