Question

I have a method in my app which sends a request to the server to check if the user is currently logged in. This ColdFusion function returns type boolean. When I NSLog the data returned, this is what I get:

<wddxPacket version='1.0'><header/><data><boolean value='true'/></data></wddxPacket>.

Currently, to determine if it returned true or false I'm searching for a substring 'true' in that returned string. That does not seem like a good solution. My question is, is there a better way to get an Objective-C BOOL from this function? If it's better to change the ColdFusion function, I luckily can do that. Thanks!

ColdFusion function:

<cffunction name="loggedIn" returnType="Boolean" output="false" access="remote">
    <cfargument name="sessionID" type="UUID" required="true">
    <cfquery name="q_session" datasource="#request.db_dsn#">
        SELECT ...
    </cfquery>
    <cfreturn q_session.recordCount gte 1>
</cffunction>

Objective-C method snippet:

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:myURLVar];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setHTTPBody:postData];

    NSError *requestError = [[NSError alloc] init];
    NSHTTPURLResponse *response = nil;
    NSData *urlData = [NSURLConnection sendSynchronousRequest:request
                                            returningResponse:&response
                                                        error:&requestError];

    NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
    NSLog(@"%@", responseData);
    if ([responseData rangeOfString:@"true" options:NSCaseInsensitiveSearch].location != NSNotFound){
        sessionIsActive = YES;
    }
Was it helpful?

Solution

I think you can add returnFormat="plain" to your cffunction.

plain: ensure that the return value is a type that ColdFusion can convert directly to a string, and return the string value without serialization. Valid types include all simple types, such as numbers, and XML objects. If the return value is a complex type, such as an array, or a binary value, ColdFusion generates an error. If you specify a returntype attribute, its value must be any, boolean, date, guid, numeric, string, uuid, variablename, or XML; otherwise, ColdFusion generates an error.

By default, ColdFusion serializes all return types (including simple return types), except XML, into WDDX format, and returns XML data as XML text.

That should give you just the string, which would be simpler to parse.

[source]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top