I hard code all URLs in my mobile app which I know is not a good idea. For example, if I were to ever need to move domain names I'd need to rename all URLs within the app from www.oldname.com to www.newname.com on each and every page.
I'm not sure where and how to store them so that I can modify this information on a single page and have it automatically changed across all pages.
In PHP I'd create a single file with variables that'd I'd include in all pages. I'm not sure how this is done in objective-c however. How do you do it?
You can create a constants file and include that in all other file where the URL is listed.
Create a file, e.g. constants.h where you put all your contstants.
In constants.h you could put something like this:
#define kApiBaseUrl #"https://myapi.mydomain.com"
And in your other files you acces it like this example:
NSURL *url = [NSURL URLWithString:kApiBaseUrl];
Probably this is like what you did use to do in PHP.
You can also set predefined URL's in the PLIST of your app but this is probably easier.
similar to edwardmp's response, you can also create a Constants.h file and define some static constant strings.
static NSString *const kSiteRootURL = #"http://www.someurl.com";
Check the folder "Supporting Files" inside your project you should have a file named "nameOfYourApp-Prefix.pch" then you can define the root URL for exemple
#define ROOT_URL #"yourwebsite.com"
and you can access ROOT_URL from anywhere in your project
You can keep your URLs in a property list file (File => New file => Property ...). You will have something like "config.plist". Similar with other property file, it is key => value file where the value can be a string, an arrays, etc.
Use NSBundle to load the file in your app, like
NSString *path = [[NSBundle mainBundle] pathForResource:#"config" ofType:#"plist"];
NSDictionary *config = [[NSDictionary alloc] initWithContentsOfFile:path];
This will give a benefit where you don't have to re-compile again your codes whenever you make any change on the URLs.
You can add a NSString to your .pch file to be included in all of modules.
Related
I want to import the excel file in Xcode bundle.
When I click on button then all excel content store in the database then it visible in the table from the database.
Umm, you can use this: https://github.com/davedelong/CHCSVParser BUT you have to store the file as CSV, not XLSX or XLS
Also, you can store this in the bundle, you just have to call to the bundle, something like this:
NSString *fileName = #"theFileName";
NSString *pathToFile =[[NSBundle mainBundle] pathForResource:fileName ofType: #"csv"];
the #"csv" may need to be #"txt", try it with both
These stuffs may help u
File search with specific Extension Objective c
for read (import) a XLS file https://github.com/dhoerl/DHlibxls its working well
if you need to write (export) a XLS file, u use pods https://cocoapods.org/pods/JXLS its also working
if you need to edit a XLSX file https://github.com/renebigot/XlsxReaderWriter (i'm not sure, because i don't work with it)
If you just need to show the files(XLS/XLSX) to the users, you can use a UIWebView
I am learning iOS. I have a confusion about the Plist file, about the creator, applicable platform, format and so on. So what the Plist file really is?
PList is a property list.
You can find more useful information at:
https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/AboutInformationPropertyListFiles.html
http://nscookbook.com/2013/02/ios-programming-recipe-13-using-property-lists-plists/
How to use pList in iOS Programming
And the following one would give you more infor about the specific keys:
https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Introduction/Introduction.html
A plist file is a property list. You can either create it using the nice animations Xcode gives you, or you can create pragmatically using XML. A plist file is something that can store objects (string, bool, data, date, number), like a database. And you can run through the plist file to retrieve or store the information just like a database.
In games you mostly save your score using NSUserDefaults as the data isn't sensitive, however saving information like a home address in NSUserDefaults isn't the best idea. Instead you'd rather want to save the information in a database - a plist file. Apple uses plist files in their apps. When you open contacts the information is retrieved from the plist and then put into a UITableView. When you click on a person it gives you their details, the details which were received from the plist file.
Another great things about a plist file is that you can change it from binary to XML and vice versa. Why would you ever want to change it to binary? Sometimes when you're dealing with large data e.g. a whole dictionary, it'll be faster to run through the data is binary than it would be in XML. To change it into binary, you go to terminal and use this command, plutil -convert binary1 yourFile.plist. To change binary to XML you use this command, plutil -convert xml1 yourFile.plist.
A plist in raw XML looks like:
A plist with the nice animation in Xcode looks like:
And finally a plist in binary looks like:
Now lets say you've created your plist and stored all the information in it that you want. To retrieve this information (in objc) use the following code.
NSString *path;
path = [[NSBundle mainBundle] pathForResource:#"file" ofType:#"plist"];
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path];
for (NSString *str in array) {
#autoreleasepool {
NSLog(#"%#", str);
}
}
Hope this helped you!!
My .json file contains some confidential settings and I want to include it in static library.But while supplying my static library I just want to provide .a file of static library to my client and I don't won't them to use drag and drop method in order to use its functionality.How can I achieve adding .json file in .a static file?
You cannot place any resource file inside .a file. Best option is to make a bundle file and keep the json file inside it. But then the your client can see those file if he wants to.
If confidentiality is a matter, another option is to create a class that returns this json as NSString.
For example :
#interface LibraryHelper
+ (NSString *)jsonConfig;
#end
#implemenatation LibraryHelper
+ (NSString *)jsonConfig {
NSString *json = #"<your json string hard coded>";
}
#end
If you don't want to have the developer drag another file in along with the .a binary then you need to compile the json file into the binary itself - as a string then parsed at runtime.
Having said this, if you just need a few config params then you'd be better off just putting the values directly into variables.
Also check out iOS .framework bundles in Xcode 6, they provide a nice way to group your binary, header files and any additional resource files. In the Xcode 6 new project window choose Frameworks & Library > Cocoa Touch Framework.
We are making a common library in c++ for database and file reading.
We have decided a common folder structure for placing the files.
For eg. Insert statement files will be in a Lib/Files/Insert/
I will pass root directory of the project to c++ library, the library then will automatically append the path of the file to root directory.
For eg. If I need insert file of Author, I will pass /path/to/root/ to library. The library will append the rest of the path and pick up file like this: /path/to/root/Lib/Files/Insert/Author.txt
I was not able to open the file from c++ code.
Then I tried this to get the path of file using Objective-C:
NSString * authorPath = [[NSBundle mainBundle] pathForResource:#"Author" ofType:#"txt"];
It is showing the following path:
/Users/homam/Library/Application Support/iPhone Simulator/7.0.3/Applications/9C882381-DAA7-464D-AA84-8DB9288AAD71/CoreLibraryDemo.app/Author.txt
Although I have placed it in the folder /Lib/Files/Insert/, it is not included in the above path before Author.txt
So how can I maintain folder structure in iOS and read file from c++ library.
In my App for the iPad I'm trying to load an image file programmatically. The file is NOT part of my project, hence it is not referenced in XCode. There is no entry for the file in XCode's Groups and Files column.
The image has to be loaded at runtime instead, it name being read from a Property List.
I'm trying to load the file like this:
NSString* pathToImageFile = [[NSBundle mainBundle] pathForResource:#"MyImage" ofType:#"png" inDirectory:#"MyDirectory"];
UIImage* retVal = [UIImage imageWithContentsOfFile:pathToImageFile];
In this case, mydirectory lives in the main bundle like this:
MyAmazingApp.app/MyDirectory/MyImage.png
Unfortunately, the Image will not load. I can't add the image file to my project, as its name is to be determined at runtime and cannot be known in advance. The file name is read from a config file (a Property List) at runtime instead.
What am I doing wrong? Your help will be very much appreciated.
You can use NSFileManager to get the contents of a directory, or several directories. You obviously have to know something about the file you want to load so that you can identify it, but you could for example use NSFileManager to help you generate a list of images in your app bundle and in your app's Documents directory.