|
הרשם | ![]() |
שאלות ותשובות | ![]() |
רשימת חברים | ![]() |
לוח שנה | ![]() |
הודעות מהיום | ![]() |
חיפוש | ![]() |
|
![]() |
![]() |
|
כלים לאשכול | תצורת הצגה |
![]() |
# 1 |
עסק רשום [?]
|
שאלה| העברת נתונים ב-POST לשרת מאפליקציית iOS בxCode
שלום לכולם.
נתקעתי בבעיה קלה שאני לא מצליח לפתור, אשמח אם מישהוא יוכל לעזור לי בה. באפליקציה אני מאפשר למשתמש לבחור תמונה ממאגר התמונות שלו באייפון, בעזרת imagePicker. לאחר שהוא בחר את התמונה, אני רוצה לבצע מעבר שלה לשרת באינטרנט. הבעיה היא שאני כבר הרבה מאוד זמן לא הצלחתי לעשות את זה. מדריכים שקיימים ברשת לגבי העניין או שלא תקינים או שלא מותאמים לגרסא 6. הקוד שבו השתמשתי: קוד:
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info { [[picker presentingViewController] dismissViewControllerAnimated:YES completion:nil]; UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; // NSData *imageData = UIImageJPEGRepresentation(image,0.2); //change Image to NSData NSData *imageData = [[NSData alloc] initWithData:UIImageJPEGRepresentation(image, 1)]; NSString *urlSTR = @"http://localhost/Places/index.php"; NSURL *url = [[NSURL alloc] initWithString:urlSTR]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60]; [request setHTTPMethod:@"POST"]; // We need to add a header field named Content-Type with a value that tells that it's a form and also add a boundary. // I just picked a boundary by using one from a previous trace, you can just copy/paste from the traces. NSString *boundary = @"----WebKitFormBoundarycC4YiaUFwM44F6rT"; NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; // end of what we've added to the header // the body of the post NSMutableData *body = [NSMutableData data]; // Now we need to append the different data 'segments'. We first start by adding the boundary. [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; // Now append the image // Note that the name of the form field is exactly the same as in the trace ('attachment[file]' in my case)! // You can choose whatever filename you want. [body appendData:[[NSString stringWithFormat:@"Content-Disposition: attachment; name=\"%@\"; filename=\"%@\"\r\n", image, @"dasd"] dataUsingEncoding:NSUTF8StringEncoding]]; // We now need to tell the receiver what content type we have // In my case it's a png image. If you have a jpg, set it to 'image/jpg' [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; // Now we append the actual image data [body appendData:[NSData dataWithData:imageData]]; // and again the delimiting boundary [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; // adding the body we've created to the request [request setHTTPBody:body]; NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; NSLog(returnString); } בשורה האחרונה קיימת לי הערה קטנה: Format string is not a string literal (potentially insecure) הערה קטנה: אני חושב שזה קשור לBoundary, אבל לא בטוח בכך. תודה מראש ![]()
__________________
דניאל גולוב. Last edited by DCaptain; 28-04-13 at 17:09.. |
![]() |
![]() |
חברים פעילים הצופים באשכול זה: 1 (0 חברים ו- 1 אורחים) | |
|
|