1

I have an app that communicates with a web service VIA SOAP. So far the communications have been strictly string values and the like. Some of these values in classes, some not.

My question is: Is it possible to pass an instance of a class through SOAP?

Something with the syntax like..

//class declaration in .h
 Myclass class

in the .m

 class.string = @"stuff";
 class.integer = 5;

then create the SOAP string

[NSString stringWithFormat:@"<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<stuff xmlns="http://website.com/">
  <class>%@</class>
</stuff>
</soap:Body>
</soap:Envelope>,class];

Is something like this possible as long as the web service knows how to handle it?

Ben S
  • 68,394
  • 30
  • 171
  • 212
Jesse Durham
  • 285
  • 1
  • 5
  • 18

2 Answers2

1

To transfer an instance of a class, look into the NSCoding protocol.

The Archiving and Serializations Programming Guide has some examples on how to do what you're asking, specifically the Encoding and Decoding Objects chapter.

Ben S
  • 68,394
  • 30
  • 171
  • 212
  • I have no control over what is done on the webservice side. Its written in VB I believe. Will it be able to handle this information if I pass it? Or will they have to go through a process to decode the object? – Jesse Durham Jan 10 '12 at 21:32
  • VB doesn't understand Objective-C binaries (ehm, obviously). Serializing the object is only good if you intend to store it and then get it back to the objective-c side. It is a bad idea if you intend to pass values to the server. In this case just write some XML from Objective-C. – Jano Jan 10 '12 at 21:47
  • There was no requirement that VB was involved in the question. I assumed the instance was being stored by the web service. I would go with JSON or XML in that case. Simply encode the object's state in one of those formats and decode it in VB. – Ben S Jan 10 '12 at 21:57
  • Ok, yeah my mistake. This object is being passed to the server and will not be used again by my app. I was going to mention that just now but you guys found out already. I'll use the XML option in that case. Thanks. – Jesse Durham Jan 10 '12 at 22:04
0

Use NSKeyedArchiver.

// serialize object to data with the given key
+ (NSData*) archive:(id<NSCoding>)object key:(NSString*const)key {
    NSMutableData *data = [[NSMutableData alloc] init];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [archiver encodeObject:object forKey:key];
    [archiver finishEncoding];
    [archiver release];
    return [data autorelease];
}

+ (id<NSCoding>) unarchiveKey:(NSString*const)key data:(NSData*)codedData{
    // unarchive
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:codedData];
    id<NSCoding,NSObject> object = [unarchiver decodeObjectForKey:key];
    [unarchiver finishDecoding];
    [unarchiver release];
    return object;
}

and you need to edit the object to serialize to implement NSCoding for its ivars:

#pragma mark - NSCoding

- (void)encodeWithCoder:(NSCoder*)coder {
    [super encodeWithCoder:coder];
    [coder encodeObject:self.myVar forKey:@"someArbitraryKey"];
    // ... other ivars
}

- (id)initWithCoder:(NSCoder*)coder {
    self = [super initWithCoder:coder];
    if(self) {
        self.myVar = [coder decodeObjectForKey:@"someArbitraryKey"];
        // ...other ivars
    }
    return self;
}

So create two methods for archive and unarchive, and implement NSCoding in the object to serialize. Then take the NSData and make it Base64 to add it to SOAP. Basically that. I didn'tn compile anything, good luck. Read Ben's link.

Community
  • 1
  • 1
Jano
  • 62,815
  • 21
  • 164
  • 192