Khoren Markosyan
Khoren Markosyan

Reputation: 67

How to define the API Url's in Objective-C

I want to define the API URL's in a single header file, like

#define BASE_URL                    @"http://xxx.xxx.xxx.xxx/api"

#define POSTS                       BASE_URL @"/user_posts"
#define kAPI_GET_POSTS_LIST_URL     POSTS
#define kAPI_ADD_LIKE_URL(id)       POSTS @"/" id @"/like"

......

But it does not work when the 'id' is dynamic. Is there any better ways to organize API Url's?

Upvotes: 3

Views: 588

Answers (3)

Augustine P A
Augustine P A

Reputation: 5068

try this,

#define BASE_URL                    @"http://www.........."
#define POST                        [NSString stringWithFormat:@"%@/user_posts",BASE_URL]
#define kAPI_GET_POSTS_LIST_URL     POSTS
#define kAPI_ADD_LIKE_URL(uri)      [NSString stringWithFormat:@"%@/%@/like",POSTS,uri]

Hope this will help :)

Upvotes: 0

Sunny Shah
Sunny Shah

Reputation: 13020

try this.

#define kAPI_ADD_LIKE_URL(id)       [NSString stringWithFormat:@"%@/%d/lik",POSTS,id]

Upvotes: 0

l0gg3r
l0gg3r

Reputation: 8954

bari or ;),
You can define like this

#define BASE_URL                    @"http://xxx.xxx.xxx.xxx/api"

#define POSTS                       BASE_URL @"/user_posts"
#define kAPI_GET_POSTS_LIST_URL     POSTS
#define kAPI_ADD_LIKE_URL(id)       [NSString stringWithFormat:@"%@/%@/like", POSTS, id]

And use it

NSString *someId = @"5";
kAPI_ADD_LIKE_URL(someId);

Upvotes: 2

Related Questions