pathForResource Not Working on ios6 - ios

Sorry if this is elementary. Banging my head over it. My app works fine in iOS5.1 and below, but fails to reach my .txt files in my Supporting Files folder in ios6. It always returns "null". I researched this and have checked the Bundle, all is there. The table lists about 8 speakers. On click, it is supposed to pull a DetailView loaded from a .txt file.
...[code]...
else if (self.makeLabel.text == #"Peter Sengenberger") {
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"sengenberger" ofType:#"txt"];
if (filePath) {
NSString *myText1 = [NSString stringWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding error:nil];
if (myText1) {
textData= [[UITextView alloc] initWithFrame:CGRectMake(5,0, 280,200)]; //size.height-30 )];
textData.text = myText1;
[textData setFont:[UIFont systemFontOfSize:14]];
[textData setBackgroundColor:[UIColor clearColor]];
[textData setTextColor:[UIColor blackColor]];
textData.editable = NO;
textData.dataDetectorTypes = UIDataDetectorTypeLink;
[textScroll addSubview:textData];
}
}
} // end if
Thanks for any help!

Related

How to show the output from a deb install ios

I'm building a jailbreak app in which it allows deb installs.
I'm stuck on showing the output or install process in another view controller, similar to iFile and Cydia's own installer.
Does anybody know how to get this to show?
What I have so far is a table view, tap file, action sheet pops up asking to install. Pressing install starts the install process and also opens another blank UIView. How would I pass that data to the opened view?
UPDATE 1 as requested:
Code for installing deb and pushing new view controller to display output:
//Deb file extension
NSString *debFileExtension = [fileName pathExtension];
NSLog(#"fileExtension is: %#", externalFileExtension);
NSSet *supportedFileExtensions = [NSSet setWithObjects:#"deb", nil];
if ([supportedFileExtensions containsObject:[debFileExtension lowercaseString]]) {
documentController = nil;
NSString *actionSheetTitle = fileName;
BlockActionSheet *sheet = [BlockActionSheet sheetWithTitle:actionSheetTitle];
[sheet addButtonWithTitle:#"Install" block:^{
NSString *appsyncDebPath = [path stringByAppendingPathComponent:fileName];
NSString *cmdString=[NSString stringWithFormat:#"/usr/bin/dpkg -i %#",appsyncDebPath];
const char *cmdChar=[cmdString UTF8String];
system(cmdChar);
DebViewController * vc = [[DebViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
[vc release];
NSLog(#"Install pressed %#", cmdString);
}];
[sheet setDestructiveButtonWithTitle:#"Cancel" block:nil];
[sheet showInView:self.view];
}
From that the DebViewController gets called. The issue is the displaying of the output or log or w/e in the new view.
Would a regular UIView work? Or do I need a specific view to receive it?
UPDATE 2: with suggested NSTask.
NSTask *task1 = [[NSTask alloc] init];
NSPipe *pipe1 = [NSPipe pipe];
[task1 setLaunchPath: #"/usr/bin/dpkg"];
[task1 setArguments: [NSArray arrayWithObjects: #"-i", nil]];
[task1 setStandardOutput: pipe1];
[task1 launch];
NSFileHandle *file = [pipe1 fileHandleForReading];
NSData * data = [file readDataToEndOfFile];
NSString * string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(#"Result: %#", string);
UITextView *txtview = [[UITextView alloc]initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, self.view.bounds.size.height)];
txtview.text = string;
//some other setup like setting the font for the UITextView...
[txtview sizeToFit];
[self.view addSubview:txtview];
UPDATE 3:
Everything is working!! Minus 1 thing.
NSString *debPath = [path stringByAppendingPathComponent:fileName];
NSTask *task1 = [[NSTask alloc] init];
NSPipe *pipe1 = [NSPipe pipe];
[task1 setLaunchPath: #"/Applications/myapp.app/"];
[task1 setArguments: [NSArray arrayWithObjects: #"/usr/bin/dpkg", #"-i", debPath, nil]];
[task1 setStandardOutput: pipe1];
[task1 launch];
NSFileHandle *file = [pipe1 fileHandleForReading];
NSData * data = [file readDataToEndOfFile];
OutputViewController * debOutput = [[OutputViewController alloc] init];
UINavigationController *vc = [[UINavigationController alloc] initWithRootViewController:debOutput];
[self.navigationController presentViewController:vc animated:YES completion:nil];
debOutput.output = [[NSString alloc] initWithData:data encoding: NSUTF8StringEncoding];
So the provided code above works as it should, and the receiving view controller displays the output.
Only thing is, that it's not displaying the full output of the deb install. almost like its shorting the lines. I have the output set up as follows:
UITextView *l = [[UITextView alloc] initWithFrame:CGRectMake(5, 0, self.view.frame.size.width-5, self.view.frame.size.height)];
l.editable = NO;
l.textAlignment = NSTextAlignmentLeft;
l.font=[UIFont boldSystemFontOfSize:14];
l.textColor = [UIColor whiteColor];
l.backgroundColor = [UIColor colorWithWhite:0.1f alpha:1.0f];
l.text = [NSString stringWithFormat: #"%#", output];
l.textContainer.lineBreakMode = NSLineBreakByWordWrapping;
[l release];
UPDATE 4:
So what I ended up doing was loading some text in the viewDidLoad when the view first showed:
NSString *cmd0 = #"Running Debian Packager";
NSString *cmd1 = #"Executing Command: /usr/bin/dpkg -i";
NSString *cmd2 = #"Preparing - ";
NSString *cmd3 = #"Installing......Please wait...";
l.text = [NSString stringWithFormat:#"%#\n\n%#\n\n%#%#\n\n%#", cmd0, cmd1, cmd2, fileName, cmd3];
l.textContainer.lineBreakMode = NSLineBreakByWordWrapping;
[view addSubview:l];
Then called the deb install process in the viewDidAppear, which replaces the above code with the output:
//NSTask
NSString *debPath = [path stringByAppendingPathComponent:vc.fileName1];
NSTask *task1 = [[[NSTask alloc] init] autorelease];
NSPipe *pipe1 = [NSPipe pipe];
[task1 setLaunchPath: #"/Applications/myapp.app/process"];
[task1 setArguments: [NSArray arrayWithObjects:#"/usr/bin/dpkg", #"-i", debPath, #"2>/tmp/dpkg.log" ,nil]];
[task1 setStandardOutput: pipe1];
[task1 launch];
NSFileHandle *file = [pipe1 fileHandleForReading];
NSData *data = [file readDataToEndOfFile];
NSString *string = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
NSString *cmd3 = #"Success";
NSString *cmd4 = #"*If this package requires a respring, please do so above*";
NSString *dependencies = #"*You are seeing this message because this package requires the additional dependencies listed above*";
NSString *closeCydia = #"*If you are seeing this message, Cydia is probably open. Please close, and try to install the package again*";
//DPKG LOG FILE
NSError *error;
NSString *logPath = [NSString stringWithFormat:#"/tmp"];
NSString *dpkgLogFile = [logPath stringByAppendingPathComponent:#"dpkg.log"];
NSString *logContents = [NSString stringWithContentsOfFile:dpkgLogFile encoding:NSUTF8StringEncoding error:&error];
NSString *dependsString = #"dpkg: dependency problems";
NSString *lockString = #"dpkg: status database area is locked by another process";
if ([logContents containsString:lockString]) {
l.text = [NSString stringWithFormat:#"%#\n%#", logContents, closeCydia];
self.navigationController.navigationBar.topItem.rightBarButtonItem = nil;
}else if ([logContents containsString:dependsString]){
l.text = [NSString stringWithFormat:#"%#\n%#\n%#", string, logContents, dependencies];
self.navigationController.navigationBar.topItem.rightBarButtonItem = nil;
}else{
l.text = [NSString stringWithFormat:#"%#\n%#%#\n\n%#", string, logContents, cmd3, cmd4];
}
[view addSubview:l];
Depending on what the deb installs, I customized the output, i.e. If it has depends, or if the process is locked because Cydia is open.
All in all I'm happy with the turnout. Thanks to Nate for the direction to use NSTask, worked like a charm.
The only thing to make it better is to have it print off or readout, similar to how Cydia goes through line by line.
Instead of using the system() command to run the dpkg command line, I would suggest using NSTask to run the command, which makes it easier to capture the output as a NSString. Once you have a string, you can copy it into a text view, or wherever you like.
NSTask is a private API on iOS, but it's public on OS X, so there's lots of documentation available. In order to use it in your project, just find a copy of the NSTask.h header and copy it into your project (and #import it, of course).
Here's an example of using NSTask to capture command line output in a UIApplication.
Or, another one.
If your install process may take a while, and you'd like your UI to be responsive while it's running, it would be a good idea to run the method that performs the task in the background (using GCD, for example), and then write the resulting string to your UIView (text field, etc.) back on the main/UI thread.

Too many open files iOS

i am new on ios programming, when my app is running , i am taking these errors. I am loading 950+ images in my app and i am using ARC.
ImageIO: CGImageRead_mapData 'open' failed '/Users/apple/Library/Application Support/iPhone Simulator/6.1/Applications/16551664-4694-4742-85DC-2C3C0ADC5289/demo.app/menu-24-20.png'error = 24 (Too many open files)
ImageIO: CGImageRead_mapData 'open' failed '/Users/apple/Library/Application Support/iPhone Simulator/6.1/Applications/16551664-4694-4742-85DC-2C3C0ADC5289/demo.app/menu-24-20.png'error = 24 (Too many open files)
ImageIO: CGImageRead_mapData 'open' failed '/Users/apple/Library/Application Support/iPhone Simulator/6.1/Applications/16551664-4694-4742-85DC-2C3C0ADC5289/demo.app/circle_green.png'
error = 24 (Too many open files)
ImageIO: CGImageRead_mapData 'open' failed '/Users/apple/Library/Application Support/iPhone Simulator/6.1/Applications/16551664-4694-4742-85DC-2C3C0ADC5289/demo.app/circle_green.png'
error = 24 (Too many open files)
ImageIO: CGImageRead_mapData 'open' failed '/Users/apple/Library/Application Support/iPhone Simulator/6.1/Applications/16551664-4694-4742-85DC-2C3C0ADC5289/demo.app/shopping_cart_1-512.png'
error = 24 (Too many open files)
ImageIO: CGImageRead_mapData 'open' failed '/Users/apple/Library/Application Support/iPhone Simulator/6.1/Applications/16551664-4694-4742-85DC-2C3C0ADC5289/demo.app/shopping_cart_1-512.png'
error = 24 (Too many open files)
This code block is part of my app.
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
int UrunId = sqlite3_column_int(compiledStatement,0);
//NSString *urunNo= [NSString stringWithFormat:#"%i",UrunId];
UrunAdi = [NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement, 1)];
NSString *imageName= [NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement, 2)];
UIImageView *background = [[UIImageView alloc]initWithFrame:CGRectMake(column*197+30, row*350, 175, 280)];
NSString *filePathBackGround = [[NSBundle mainBundle] pathForResource:#"BackImage" ofType:#"png"];
NSData *myData = [NSData dataWithContentsOfFile:filePathBackGround];
UIImage *blackBackGround = [UIImage imageWithData:myData];
[background setImage:blackBackGround];
[scrollView addSubview:background];
NSString *filePathSepeteEkleButton = [[NSBundle mainBundle] pathForResource:#"sepeteEkleButtonImage" ofType:#"png"];
NSData *myDataButton = [NSData dataWithContentsOfFile:filePathSepeteEkleButton];
UIImage *sepeteEkleButtonImage = [UIImage imageWithData:myDataButton];
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(column*197+38 , row*350+8, 159, 45)];
[button setImage:sepeteEkleButtonImage forState:UIControlStateNormal];
[button addTarget:self
action:#selector(addToChart:)
forControlEvents:UIControlEventTouchUpInside];
button.tag = UrunId;
UILabel *buttonLabel=[[UILabel alloc]initWithFrame:CGRectMake(column*197+43,row*350+20,200,20)];
buttonLabel.textColor = [UIColor whiteColor];
buttonLabel.backgroundColor=[UIColor clearColor];
buttonLabel.text=UrunAdi;
[scrollView addSubview:button];
[scrollView addSubview:buttonLabel];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *filePath = [documentsPath stringByAppendingPathComponent:imageName];
NSData *pngData = [NSData dataWithContentsOfFile:filePath];
if (pngData == nil) {
NSString *filePathResimYok= [[NSBundle mainBundle] pathForResource:#"resimYok" ofType:#"jpeg"];
NSData *myDataResimYok= [NSData dataWithContentsOfFile:filePathResimYok];
image2 = [UIImage imageWithData:myDataResimYok];//horoda
}else{
image2 = [UIImage imageWithData:pngData];
}
image2=[image2 imageByScalingAndCroppingForSize:CGSizeMake(175, 210)];
//UIImage *urunDetayImage = [UIImage imageNamed:image2];
UIButton * urunDetayButton = [UIButton buttonWithType:UIButtonTypeCustom];
[urunDetayButton setFrame:CGRectMake(column*197+38 , row*350+58, 159, 170)];
[urunDetayButton setImage:image2 forState:UIControlStateNormal];
[urunDetayButton addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
urunDetayButton.tag = UrunId;
[scrollView addSubview:urunDetayButton];
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(column*197+38,row*350+245,200,20)];
label.textColor = [UIColor whiteColor];
label.backgroundColor=[UIColor clearColor];
label.text=UrunAdi;
[scrollView addSubview:label];
I am trying to fix for 3 days.Please help me. Thanks.
As far as I can see your code looks fine, and running in the simulator (where plenty of memory) this should be working. A few suggestions:
1) use dataWithContentsOfFile:options:error: instead of dataWithContentsOfFile:, use the NSDataReadingUncached option (to reduce memory pressure on the system), and test the return value - if nil log the error and update your question.
2) You can always dump the data image in a NSCache object, and if the system needs memory it will release cache items, and you'll have to re-read the image from the file system. You can use this technique even if you only pull a few images at a time.
3) You can use UIImage imageWithContentsOfFile: instead of getting the data then creating the image. In this case put the UIImage itself in the cache, with a key of its name.
From X-Code 10 Apple is not removing all NSCache instances dynamically as it done before. So as developer we need to Quit the simulator app and run the build your build be succeeded.

Data is not adding to Plist

Hi i created one plist in xcode (newfile->resourses->property list like this) and i try to add one NSDictionary to that plist but its not adding ,please help me here is my code
-(IBAction)Add:(id)sender {
_pdfbookmark = [NSMutableArray arrayWithArray:[[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"pdfBookmarksdata" ofType:#"plist"]] objectForKey:_bookID]];
NSMutableDictionary *bookmark = [NSMutableDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"pdfBookmarksdata" ofType:#"plist"]];
if(self.bookmarkIndicator.tintColor == [UIColor blueColor]){
//Already Bookmarked
[_pdfbookmark removeObjectAtIndex:currentBookmarkIndex];
self.bookmarkIndicator.tintColor = [UIColor whiteColor];
}else{
//Create Bookmark
[_pdfbookmark addObject:
#{#"deviceid": #"0",
#"page" : [NSNumber numberWithInt:[self getGlobalPageCount]],
#"fontsize" : [NSNumber numberWithInt:currentTextSize]}];
self.bookmarkIndicator.tintColor = [UIColor blueColor];
}
[bookmark setObject:_pdfbookmark forKey:_bookID];
[bookmark writeToFile:[[NSBundle mainBundle] pathForResource:#"pdfBookmarksdata" ofType:#"plist"] atomically:YES];
}
You are not allowed to write files to your main bundle. If you check the return of the writeToFile: method, you will see that it returns NO.

iOS - setting text of UITextView throws EXC_BAD_ACCESS

I have a UITextView which I have created programmatically in the following code:
NSLog(#"creating the first blurb");
blurb = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
[blurb setText:total];
blurb.backgroundColor = [UIColor redColor];
where blurb is defined as a UITextView in the #interface.
Here's where I'm having the problem:
total is an NSString that is defined to get the text from a .txt file from a specified domain.
It is mutated as:
NSString *url = #"https://sites.google.com/site/paloaltoapps/tbnappsource-password-blackr3d/Updates.txt";
NSURL *urlRequest = [NSURL URLWithString:url];
total = [NSString stringWithContentsOfURL:urlRequest encoding:NSUTF8StringEncoding error:nil];
However, when I run the code, I get an instance of EXC_BAD_ACCESS thrown at the setText: line of the top code block.
I have tried printing total out using an NSLog statement, and that seems to work just fine.
Can anyone see what I'm doing wrong? Thanks.
try this... May be it lose reference......
total = [[NSString stringWithContentsOfURL:urlRequest encoding:NSUTF8StringEncoding error:nil]copy];
Why not trying to create an NSString and assign the text to that. And then you can try to print the NSString and see if that works. Just a suggestion.

How to get file extension (compare)

i´m parsing the filedirectory from dropbox into a mutablearray, to show it in a table view.
how can i compare the file extension? (.doc, or .jpg,....)
if ([[NSString stringWithFormat:#"%#",[test objectAtIndex:indexPath.row]] isEqualToString:#"??????"] ) {
[cell.extensionView setImage:[UIImage imageNamed:#"word.png"]];
}
isEqualToWhat? is it possible to use wildcards?
Don't use stringWithFormat unless you actually have a format. Your code would be much cleaner if you did something like this:
NSString *filename = [text objectAtIndex:indexPath.row];
NSString *ext = [filename pathExtension];
if ([ext isEqualToString:#"doc"]) {
[cell.extensionView setImage:[UIImage imageNamed:#"word.png"]];
} else if ([ext isEqualToString:#".jpg"]) {
[cell.extensionView setImage:[UIImage imageNamed:#"jpeg.png"]];
}
There is a better way than setting up this big if-else block. I imagine you have lots of different extensions you wish to check. Setup a dictionary with the extensions and images. Something like:
NSDictionary *extensionThumbnails = #{
#"doc" : [UIImage imageNamed:#"word.png"],
#"xls" : [UIImage imageNamed:#"excel.png"],
#"jpg" : [UIImage imageNamed:#"jpeg.png"]
};
Add an entry for each extension and image you have. Then your original code (now using modern Objective-C syntax) becomes:
NSString *filename = text[indexPath.row];
NSString *ext = [filename pathExtension];
UIImage *thumbnail = extensionThumbnails[ext];
if (!thumbnail) {
thumbnail = [UIImage imageNamed:#"unknown.png"];
}
[cell.extensionView setImage:thumbnail];

Resources