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.
Related
I had create a framework that send data to server. Whenever I get response from the server I translate it using NSLocalizedString but it not working.
I tried to change Main Bundle to my Framework Bundle like this :
NSString* mainBundlePath = [[NSBundle mainBundle] resourcePath];
NSString* frameworkBundlePath = [mainBundlePath stringByAppendingPathComponent:#"Frameworks/MYFRAMEWORK.framework"];
NSBundle *bundle = [NSBundle bundleWithPath:frameworkBundlePath];
[bundle localizedStringForKey:#"Message" value:#"" table:nil];
But still not working. is there any way to localized message when the Localizable.string is inside the framework ?
Thanks
Finally, I tried to recreate Localizable.string and whenever I want to localize I use this :
NSString *message = [[NSBundle bundleForClass:self.class] localizedStringForKey:#"MESSAGE" value:nil table:nil];
I put this code inside class of framework, so bundle of self.class is direct to framework bundle. Thank's for everybody help.
Try to use [NSBundle bundleForClass:self.class].
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
}
I have made an app that creates a pdf and stores it in the apps documents folder. I would now like to open it and view it from within the app when a 'View pdf' UIButton is pressed.
I have already looked at a few questions on here and I have considered either a separate view controller or perhaps a scroll view.
What is the best method to use?
UPDATE:
I have followed advice and I am trying to use QLPreviewController. I have added QuickLook framework and now have the following, but I am stuck on how to get the path recognised in the pathForResource. Any suggestions?
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller
{
return 1;
}
- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
NSString *path=[[NSBundle mainBundle] pathForResource:[pdfPathWithFileName] ofType:nil];
return [NSURL fileURLWithPath:path];
}
- (IBAction)viewPdfButton:(id)sender {
NSString *filename= #"ObservationPDF.pdf";
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documnetDirectory = [path objectAtIndex:0];
NSString *pdfPathWithFileName = [documnetDirectory stringByAppendingPathComponent:filename];
[self generatePdf: pdfPathWithFileName];
QLPreviewController *previewController=[[QLPreviewController alloc]init];
previewController.delegate=self;
previewController.dataSource=self;
[self presentViewController:previewController animated:YES completion:nil];
}
If the PDF file is in the app documents folder then you shouldn't be thinking about passing it to another app, you should be looking to present the file inside the app. 2 general options:
Add a UIWebView and load the local file into it
Use QLPreviewController to show a new view containing the PDF
The web view is simple and requires no transition on the UI. The preview controller needs a transition but offers some sharing / printing support for free.
This line is confused (and invalid syntax by the looks of it):
NSString *path=[[NSBundle mainBundle] pathForResource:[pdfPathWithFileName] ofType:nil];
You only use NSBundle to get items out of the bundle, and that isn't what you have. You should just be creating the URL with the file path where you save the file:
[NSURL fileURLWithPath:pdfPathWithFileName];
(which you may store or you may need to recreate in the same way as when you save the file)
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.
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?