Dynamically load storyBoards - ios

I'm working on a storyBoard loading system and I need to check for the presence of a specific storyBoard.
I made a function checking for it (given a specific StoryBoard name) :
(NSString *)ressourceNameForDevice:(NSString *)rootName extension:(NSString *)ext
{
NSString *retString = rootName;
NSString *iPadString = [NSString stringWithFormat:#"%#-iPad", rootName];
NSString *iPhoneWideString = [NSString stringWithFormat:#"%#-wide", rootName];
if (IS_IPAD &&
(nil != [[NSBundle mainBundle] pathForResource:iPadString ofType:ext]))
{
retString = iPadString;
}
if (IS_IPHONE_WIDE &&
(nil != [[NSBundle mainBundle] pathForResource:iPhoneWideString ofType:ext]))
{
retString = iPhoneWideString;
}
return retString;
}
I'm calling this function with #"storyboard" as extension argument.
My probleme is that the function fails on nil != [[NSBundle mainBundle] pathForResource:iPhoneWideString ofType:ext], even though the storyBoard IS present in my project and bundle, it doesn't get found. I guess I should give it another extension, I tried with #"nib" too but no more result.
Anyone knows how to check that ?

Solved it. The problem was that once a storyBoard gets compiled the ressource extension isn't .storyboard but .storyboardc, same way that a .xib compiled file has the .nib extension.

Lets say you have "xyz.storyboard" in your project to be loaded. Then write it as:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"xyz"
bundle: nil];
It should do the needful.

Related

locating a text file Objective C

I am trying to get path of legal.txt in my project. However, whatever I do. I am getting a null path back. it is in the Policies directory. How do I get it's path as a string. I need to send that path to a webview controller. Can someone help.
-(void)linkPressedOnTextView:(PPLinksTextView *)tv url:(NSURL *)url{
if ([url.host isEqualToString:#"sign_up"]) {
GetStartedController *getStartedVC = [[GetStartedController alloc] init];
[self.navigationController pushViewController:getStartedVC animated:YES];
}
else if([url.host isEqualToString:#"consentMedicalSevices"]){
PPNavigationViewController *vc = [[PPNavigationViewController alloc]init];
[vc openPolicies:#"Policies/privacy.txt"];
Will this work. I need a way to direct user to a webview once he clicks on the link and privacy text opens up.
From apple doc:
pathForResource:ofType:inDirectory:
Returns the full pathname for the resource file identified by the specified name and extension and residing in a given bundle directory.
NSString *filePath = [NSBundle pathForResource:#"file" ofType:#"txt" inDirectory:[[NSBundle mainBundle] bundlePath]];
NSLog(#"File Path: %#", filePath);
Output:
File Path: /Users/Ashok/Library/Developer/CoreSimulator/Devices/F629F99F-C745-46EB-8A11-01BC9FF1E592/data/Containers/Bundle/Application/EB4EA25F-65E6-4CA8-B8C2-C7C0C64C6C0F/Sample.app/file.txt

UIImage Caching causing Localization issues at runtime

I am trying to load a .xib based on the language selected by the user at the runtime.
To get the particular Bundle I use the following code
-(NSBundle*) getMyBundle{
NSArray *languages=[NSLocale preferredLanguages];
NSString *basePath=[[NSBundle mainBundle] pathForResource:#"Base" ofType:#"lproj"];
if(languages.count){
NSString *langKey=[languages objectAtIndex:0];
if(![util checkIfStringIsEmpty:langKey]){
NSString *path=[[NSBundle mainBundle] pathForResource:langKey ofType:#"lproj"];
if(![util checkIfStringIsEmpty:path]){
return [NSBundle bundleWithPath:path];;
}
}
}
return [NSBundle bundleWithPath:basePath];
}
-(void) loadMyController{
AViewController *controller=[[AViewController alloc] initWithNibName:#"AViewController" bundle:[self getMyBundle]];
[self.navigationController pushViewController:controller animated:YES];
}
So, with above code .xib are loaded correctly , but since images are cached, they are not picked up.
So, any solution to overcome this issue will be a great help.

Find out if XIB exists at runtime

Is there anyway to do it?
I've tried pathForResource:ofType: on NSBundle and it can't find the xib when it's really there. If I try to load it and it doesn't exist loadNibNamed:owner:options: crashes and so does nibWithNibName:bundle:
Basically, I want to know at runtime whether I should register a xib or just a class for a given cell type on a collection view.
Are you sure pathForResource:ofType: is not working? It's not just the common xib / nib confusion?
(.xib-files are ofType:#"nib")...
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *path = [mainBundle pathForResource:#"crapXib" ofType:#"nib"]; // file is called crapXib.xib
if (path){
// whatever...
} else{
// whatever else
}
You can try to use NSFileManager to check if file exists, if application is not localized all .nibs live in root directory of .ipa, for example:
NSString * root = [[NSBundle mainBundle] resourcePath];
if ([[NSFileManager defaultManager] fileExistsAtPath: [root stringByAppendingPathComponent:#"MainViewController.nib"]])
{
// exisits
}

NSBundle while unit testing with OCUnit returns (null)

Problem : Retrieving from NSBundle returns (null) while running Unit Test, returns valid object on run time.
What have I done ?
I have searched SO with similar problems, apple's developer and other online resources for this problem but with no luck.
I have tried solution from SO questions; OCUnit & NSBundle, NSBundle pathForResource returns nil.
All solutions I went through online point out to one thing Bundle for run time and tests are different.
All of them recommend having
[NSBundle bundleForClass: [self class]];
instead of
[NSBundle mainBundle];
Question : I don't understand if the above fix is with setter injection or in the source file itself. ? Is there any other way I can test the method getIpAdress ?
Code
Class that returns (null),
// iRNetworkingObject.m
#implementation iRNetworkingObject
-(NSString*) getIpAdress {
NSDictionary *infoDict = [[NSBundle bundleForClass:[self class]] infoDictionary];
NSString *lookUpAdress = [infoDict objectForKey:#"LookUpIpAdress"];
return lookUpAdress;
}
#end
Test class
- (void) testGetIpAdress {
iRNetworkingObject* networkingObject = [[iRNetworkingObject alloc] init];
NSString* testCase = #"192.168.2.1";
NSString* encondedString = [networkingObject getIpAdress]];
STAssertTrue([testCase isEqualToString:encondedString], #"Not equal");
}
bundleForClass: is important in test code, to make sure you get the application bundle instead of the test bundle. You shouldn't have to do that in production code.
Your key #"LookUpIpAdress" is misspelled. Is that the problem?

writing to plist works on simulator but not on device [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Not able to write to Plist
I have this problem. I have a plist file named 'instellingen'. In the file, different settings are stored. For example, displaying the games rules at startup. This works just fine on the simulator but when i tested it on my iPad, the plist dat wont get saved.
here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
plistPath = [[NSBundle mainBundle] pathForResource:#"instellingen" ofType:#"plist"];
instellingen = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
if ([[instellingen objectForKey:#"regelAtRuntime"] boolValue] == TRUE) {
[self.showRulesButton setTitle:#"Spelregels niet meer tonen" forState:UIControlStateNormal];
}else{
[self.showRulesButton setTitle:#"Spelregels wel tonen" forState:UIControlStateNormal];
}
[self.mainContent flashScrollIndicators];
}
- (IBAction)dismissRules:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)dontShowRegels:(id)sender {
if ([[instellingen objectForKey:#"regelAtRuntime"] boolValue] == TRUE) {
[instellingen setValue:#"0" forKey:#"regelAtRuntime"];
}else{
[instellingen setValue:#"1" forKey:#"regelAtRuntime"];
}
[instellingen writeToFile:plistPath atomically:YES];
}
Reading the rules from a dire rent VC seems to work fine, with this pice of code:
NSString *plistPath = [[NSBundle mainBundle] pathForResource:#"instellingen" ofType:#"plist"];
instellingen = [NSDictionary dictionaryWithContentsOfFile:plistPath];
if ([[instellingen objectForKey:#"regelAtRuntime"] boolValue] == TRUE) {
NSLog(#"We're in!");
[self performSegueWithIdentifier:#"showRulesSegue" sender:self];
}
Does anyone know what i'm doing wrong? I've tried running a clean build (Product Clean).
Any help means a lot!
The app bundle is read-write on Simulator but read-only on device. When your app is first launched, you should copy the plist to the Documents directory (if it needs to be backed up) or Library/Caches (if it doesn't), then use that version in your app.

Resources