0

the question is simple but I saw the implementation is fairly awkward!!

I want to post an object e.g. Device object to web api web service

// Initialize Client
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://rezawebapi.com"]];
//Indicationg this device is online and sending its dEVICE token to the server
Device *device = [Device new];
device.DeviceToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"devicetoken"];
device.IsOnline = @"True";
//updating current active users of this app in the server
NSDictionary *dictionary = [[NSDictionary alloc]initWithObjectsAndKeys:
                            device.DeviceToken,@"DeviceToken",
                            device.IsOnline,@"IsOnline",
                            nil];


client.parameterEncoding = AFJSONParameterEncoding;
[client postPath:@"/api/iosAppstats" parameters:dictionary success:^(AFHTTPRequestOperation *operation, id responseObject)
 {
     NSLog(@"%@", responseObject);
     // it crashes on the next line because responseObject is NSData

 }failure:^(AFHTTPRequestOperation *operation, NSError *error)
 {
     NSLog(@"%@", error.localizedDescription);
 }];

1- is there anyway to send the object without creating an dictionary ?(it is error prone!)

2- when my deviceToken is null the object which it sends to the server is null. but consider here one property deviceToken is null but other properties have their own values! does anyone have any idea?

3- I have defined @property (assign, nonatomic) BOOL IsOnline; but when It creates the dictionary EXEX-BAD-ACCESS rises! how should I define bool value? (I had to define it as NSString. it is not an approved way)

1 Answer 1

1

1.

is there anyway to send the object without creating an dictionary ?(it is error prone!)

Your API takes JSON. JSON is just dictionaries, arrays, strings, and numbers. So, no. However, it is not error-prone. Just make sure to only put JSON-compliant objects in your dictionary. Read the NSJSONSerialization Overview for more info.

2.

when my deviceToken is null the object which it sends to the server is null. but consider here one property deviceToken is null but other properties have their own values! does anyone have any idea?

You could add deviceToken conditionally, like so:

NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
NSString *deviceToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"devicetoken"];
if (deviceToken) {
    [dictionary setObject:deviceToken forKey:@"DeviceToken"];
}

3.

I have defined @property (assign, nonatomic) BOOL IsOnline; but when It creates the dictionary EXEX-BAD-ACCESS rises! how should I define bool value? (I had to define it as NSString. it is not an approved way)

Using a BOOL violates this rule from the NSJSONSerialization overview I linked to in #1:

All objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull.

So if your property is a BOOL or other simple numerical type, wrap it in @() to make it an NSNumber:

[dictionary setObject:@(device.IsOnline) forKey:@"DeviceToken"];

This is the same as:

NSNumber *isOnlineNum = [NSNumber numberWithBool:device.isOnline];
[dictionary setObject:isOnlineNum forKey:@"DeviceToken"];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.