Where to store base url string in the xcode app?
Where should I store the base url string 开发者_运维知识库in order to be able to make REST requests using a variable, representing this string, not hard-coding it every time I'm performing a request?
I've found that it helps to create a separate header (.h) file, which defines the base URL as well as the extensions that I use for all calls. For example:
#define kWebServiceAPIBaseURL @"https://api.webservice.com"
#define kUserLoginURL @"%@/User/login?username=%@&password=%@"
Then, in your appname_Prefix.pch file, import your new header file, so you don't have to manually import it everywhere. The Prefix file is automatically appended to the beginning of each header file.
This has helped me immensely, and makes it so much easier to switch your app from sending requests to a dev API and a production API, as you only then have to change the URL in one place.
精彩评论