Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread Display Modes
Old Jun 27, 2007, 07:28 PM   #1
macrumors 6502a
bbarnhart's Avatar
Join Date: Jan 2002
Location: Stilwell, Kansas
Send a message via Skype™ to bbarnhart
It can't be that hard: NSData & NSString

I've resurrected an old Cocoa project and after making some additions I'm still stuck on something that caused me to stop in the first place. I'm still learning Cocoa so I may not quite "get it" yet, but this can't be that hard.

I've got a xml file I want to upload to my web server. I want to add some data before the xml data. Basically, my url looks like this:

http://../myphp.php?type=Put&file=xxx.xml&data=<xmldata here>

So, I've tried building an NSString to put "type=Put&file=xxx.xml&data=" and then copy that into an NSMutableData object. Then I add the xml to the NSMutableData. I had it "working" once, but then it failed with a different xml file.

Am I making a mistake with Unicode or am I not understanding fully how NSString and NSMutableData works? Any help would be appreciated!

- (void) makeXML
  NSXMLDocument* xmlDoc = [[NSXMLDocument alloc] initWithRootElement:root];
  NSData *xmlData = [xmlDoc XMLDataWithOptions:NSXMLNodePrettyPrint];
  [xmlData writeToFile:@"/Users/b...barnhart/Temp/hello.xml" atomically:YES];
  [self uploadTest: xmlData];
  return TRUE;

- (void) uploadTest: (NSData*) data
  NSURL* webServiceURL = [NSURL URLWithString:@"http://.../~b...barnhart/.../xxxxxx.php"];
  NSMutableURLRequest* urlRequest = [NSMutableURLRequest requestWithURL:webServiceURL];

  NSString* str = @"type=Put&filename=xxx.xml&data=";
  const char* utf_str = [str UTF8String];
  NSMutableData* mute_data = [NSMutableData dataWithLength:strlen(utf_str) + [data length]];
  [mute_data appendBytes:utf_str length:strlen(utf_str)];
  [mute_data appendData:data];
  ... stops working down here, but I think the problem is above

bbarnhart is offline   0 Reply With Quote
Old Jun 27, 2007, 07:47 PM   #2
Moderator emeritus
kainjow's Avatar
Join Date: Jun 2000
The best way to go from NSString to NSData is via NSString's dataUsingEncoding:allowLossyConversion: method, and you usually want NSUTF8StringEncoding as the encoding.
kainjow is offline   0 Reply With Quote

MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
the item (Item XYZ) can't be moved to the trash because it can't be deleted Sprhmn Unknwn OS X 17 Jan 21, 2014 05:13 PM
Ripping a blu-ray with MakeMKV......surely it can't be this easy can it? nickosbad Apple TV and Home Theater 18 Jul 14, 2010 06:23 AM
It can't be that easy... NaCl Jailbreaks and iOS Hacks 1 Jul 4, 2010 03:29 PM
iPhone relocation warning - it can't be done whitehexagon iPhone 4 Mar 20, 2009 05:01 PM
virus..? no it can't be. Kaioken! Mac Basics and Help 18 Jan 1, 2008 01:24 AM

All times are GMT -5. The time now is 02:30 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013,, LLC