Move data (.app) to jailbroken phones /Applications dictionary? - objective-c

I'm making an app (for Cydia) that allows the user to download other .app's and install them. Everything downloads fine but when I get to moving the file I hit a brick wall.
Code:
//Start Moving
NSString *str = textDownload.text;
str = [str stringByReplacingOccurrencesOfString:#".zip"
withString:#".app"];
NSString *placeToMoveFile = [NSString stringWithFormat:#"mv %#/%# /Applications/", documentsDirectory, str];
NSLog(placeToMoveFile);
const char *runCommand = [placeToMoveFile UTF8String];
system(runCommand);
I started getting creative after a bit (LOL)
I think the problem lays inside of system(). System doesn't even give an output to the console in the simulator. Just as a note the app does run as root so I shouldn't get permission errors with trying to move the file to /Applications.

Related

iOS app crashes after adding ScanAPISDK

I am trying to integrate socketscan SDK with my objective C iOS app. I downloaded scanapisdk, made a copy of scanapisdk inside my project main folder. I Added the files and library references to the Xcode project. compiled and build the app. run it on a device. The following code has been working with no problem for couple of years now.
I have a constant NSString declared as global variable in an m file
NSString *const kSymbology = #"Symbology";
and it is declared as an extern in an h file
extern NSString *const kSymbology;
Then in a database class there is this code fragment
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
const char *Symbology = (const char*)sqlite3_column_text(statement, 0);
strSymbology = [NSString stringWithUTF8String:Symbology];
[dict setValue:strSymbology forKey:kSymbology];
[symbologies addObject:dict];
[dict release];
The code crashes on this line
[dict setValue:strSymbology forKey:kSymbology];
When I change it to
[dict setValue:strSymbologyAlias forKey:#"Symbology"];
The app doesn't crash.
It doesn't make sense to me. it seems that the error is somewhere else and it is showing here by accident. The only new thing I have added to my project are the references to the socketScan files and libraries. I don't even call any of the methods in the SDK. I commented out all the methods in ScanApiHelper.mm file, now the app doesn't crash. I started to put back some of the methods in ScanApiHelper.mm and I discovered that when any line that has SktClassFactory mentioned for example (for example [SktClassFactory createScanObject]) is alive, the app crashes.
Can that be related to not using CocoaPods to install the sdk?
It looks like a name space collision. The libScanApiCore.a has a variable named kSymbology and my global variable is also named kSymbology. Once I changed the variable name to ksymbology (lower case s) my app no longer crashes.

Airstash with iOS

Does anyone know of tutorials or existing projects for Airstash SDK iOS integration? I have the frameworks in my project, but the existing comments inside the header files aren't incredibly helpful for initial setup. I've been googling for one, but I get a deluge of tech announcement news instead of developer resources.
-Background-
The Airstash is going to be used with an already-developed iPad application that sets equipment preferences. The targeted equipment has already been developed and has no wireless connectivity, but does have USB capability. The proposed solution is to wirelessly upload files from the iPad to an Airstash connected to the equipment.
In the SDK release there are two directories: AirStashSDK and sdk-demo. The sdk-demo directory contains an XCode project that demonstrates usage of the SDK.
The AirStashSDK folder contains the AirStash.framework to include in your project, and a Resources folder that contains a couple xib files that you should include in your project and may customize. If you plan to customize these files you may want to copy them to a different directory so your changes are not lost if you update to a newer release of the SDK. The xib files are used to display progress while getting a file from the AirStash, or activity when saving a file to the AirStash.
To save a file to an AirStash, look at the saveFileAction: method in sdk-demo/AirStashSDK Demo/RootViewController.m.
- (void)saveFileAction:(NSString*)filename
{
NSLog(#"Save a file to AirStash. filename: %#", filename);
NSURL *docDir = [self getDocumentsDirectory];
NSString *filepath = [[docDir URLByAppendingPathComponent:filename] path];
airstash = [[AirStash alloc] init];
// Save is very simple.
[airstash saveFileToAirStash:filepath
presentingFrom:self
successBlock:^(void){
NSString *msg = [NSString stringWithFormat:#"Success saving file to AirStash: original filename: %#", filename];
NSLog(#"%#", msg);
[self presentAlertWithMessage:msg];
self.airstash = nil;
}
errorBlock:^(AirStashStatus errorCode, NSString *reason) {
NSString *msg = [NSString stringWithFormat:#"Problem saving file to AirStash: (%d) %#", errorCode, reason];
NSLog(#"%#", msg);
[self presentAlertWithMessage:msg];
self.airstash = nil;
}];
}
The demo app presents a list of the files in the app's documents directory. If the user taps on a file, it calls the saveFileAction: method to save the selected file to an AirStash. An app can allocate an AirStash object and make multiple calls to its methods, or as in this case, it just makes the one call and then releases it. (The demo app's presentAlertWithMessage: method just pops up a UIAlertView with the given message and an OK button so you can see that the action is complete.)
The demo app has a couple other buttons, one to get a file from an AirStash (and save it to the app's documents directory), and the other to get the URL of a file on an AirStash. The method used by the second button would be useful for apps that want to stream a file rather than download the whole thing at once.

NSFileManager works when built in Xcode as release, but not when ran as standalone OS X Cocoa app

I have the following function written to randomly pick a file from a directory. It works totally fine when I build the project in Xcode for release with the application that automatically opens. However, if I open the application from finder, pressing the button that triggers this function will cause my program to freeze then crash. The only thing I could think of was changing the argument to contentsOfDirectoryAtPath: to not have the ./, but either version has the same exact issue.
Looking at Console tells me that my program exited abnormally with a Floating Point Exception, but I have no idea what's causing it. Is there something jumping out to you guys that I'm not seeing? I only started learning/using objective-C and cocoa about a week ago, so this is all fairly new to me.
Thanks for taking a look at this...
- (NSMutableString*)setFilePathRandom{
NSArray* files;
NSFileManager* fileManager;
fileManager = [[NSFileManager alloc] init];
files = [fileManager contentsOfDirectoryAtPath:#"./Random Trippy Pics" error:NULL];
NSString* directoryPath = (NSMutableString*)[fileManager currentDirectoryPath];
NSString* fileName;
do{
fileName = [files objectAtIndex:(arc4random()%[files count])];
}while([fileName isEqualToString:#".DS_Store"]);
filePath = [NSString stringWithFormat:#"%#/Random Trippy Pics/%#",directoryPath,fileName];
[fileManager release];
return filePath;
}
When an OS X application is run from Xcode, its current directory is the path to the build folder. When run "normally", the current directory is /. So your program is looking for a directory at /Random Trippy Pics, which almost certainly doesn't exist. Where is that directory normally?
Edit:
You could get the directory in which the application is currently stored with this bit of code:
NSString *currentStoragePath = [[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent];
However, if the Random Trippy Pics directory is required by the application, you should store it in a known location -- preferably the application's Resource directory. Then you can get the contents with:
NSArray *files = [[NSBundle mainBundle] pathsForResourceOfType:nil inDirectory:#"Random Trippy Pics"];

Get list of installed apps on iPhone

Is there a way (some API) to get the list of installed apps on an iPhone device.
While searching for similar questions, I found some thing related to url registration, but I think there must be some API to do this, as I don't want to do any thing with the app, I just want the list.
No, apps are sandboxed and Apple-accepted APIs do not include anything that would let you do that.
You can, however, test whether a certain app is installed:
if the app is known to handle URLs of a certain type
by using [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"thisapp://foo"]
You can get a list of apps and URL schemes from here.
For jailbroken devices you can use next snipped of code:
-(void)appInstalledList
{
static NSString* const path = #"/private/var/mobile/Library/Caches/com.apple.mobile.installation.plist";
NSDictionary *cacheDict = nil;
BOOL isDir = NO;
if ([[NSFileManager defaultManager] fileExistsAtPath: path isDirectory: &isDir] && !isDir)
{
cacheDict = [NSDictionary dictionaryWithContentsOfFile: path];
NSDictionary *system = [cacheDict objectForKey: #"System"]; // First check all system (jailbroken) apps
for (NSString *key in system)
{
NSLog(#"%#",key);
}
NSDictionary *user = [cacheDict objectForKey: #"User"]; // Then all the user (App Store /var/mobile/Applications) apps
for (NSString *key in user)
{
NSLog(#"%#",key);
}
return;
}
NSLog(#"can not find installed app plist");
}
for non jailbroken device, we can use third party framework which is called "ihaspp", also its free and apple accepted. Also they given good documentation how to integrate and how to use. May be this would be helpful to you. Good luck!!
https://github.com/danielamitay/iHasApp
You could do this by using the following:
Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace");
SEL selector = NSSelectorFromString(#"defaultWorkspace");
NSObject* workspace = [LSApplicationWorkspace_class performSelector:selector];
SEL selectorALL = NSSelectorFromString(#"allApplications");
NSMutableArray *Allapps = [workspace performSelector:selectorALL];
NSLog(#"apps: %#", Allapps);
And then by accessing each element and splitting it you can get your app name, and even the Bundle Identifier, too.
Well, not sure if this was available back when the last answer was given or not (Prior to iOS 6)
Also this one is time intensive, yet simple:
Go into settings > Gen. >usage. The first category under usage at least right now is Storage.
It will show a partial list of apps. At the bottom of this partial list is a button that says "show all apps".
Tap that and you'll have to go through screen by screen, and take screenshots (Quick lock button and home button takes a screenshot).
I'm doing this now and I have hundreds of apps on my iPhone. So it's going to take me a while. But at least at the end of the process I'll have Images of all my apps.

Why does my data successfully write to a file on the iPhone simulator, but not my device?

I have the following code that saves the users sketch data to a file...
//Auto Save the sketch
NSString *filename = [NSString stringWithFormat:#"%#.png", sketchID];
CGImageRef imageRef = CGBitmapContextCreateImage(paintView.canvas.mBitmapContext);
UIImage* image = [[UIImage alloc] initWithCGImage:imageRef];
NSData* imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:filename atomically:YES];
CGImageRelease(imageRef);
[image release];
the SketchID is a unique alphanumeric value, so an example of the filename I'm saving to is "p1.png". The code I use to read the file is...
//Load the sketch where the user left off, if there is one
if(fileName != nil)
{
UIImage* image = [UIImage imageWithContentsOfFile:fileName];
if(image != nil)
{
.
.
This code seems to work fine when running on the simulator, but when I run it on the device, the fails to load the image. I new to iOS development and I'm still learning how files are stored. My questions are...
Why would saving/loading the file work on the simulator, but not the device?
When I create the filename that I want to save the data to, do I need to also include a path to a specific directory on the iPhone where the data should be properly stored? Or should "p1.png" work fine when I call the writeToFile: method? And what about the imageWithContentsOfFile: method?
Why would saving/loading the file work
on the simulator, but not the device?
There are tons of reasons, but the most common is trying to write to a location that isn't writable on the device, and the most common reason for that is writing to the application bundle itself.
All iPhone apps are "sandboxed" on the device, meaning they cannot access the filesystem outside of the directory the application is installed into.
When I create the filename that I want
to save the data to, do I need to also
include a path to a specific directory
on the iPhone where the data should be
properly stored? Or should "p1.png"
work fine when I call the writeToFile:
method? And what about the
imageWithContentsOfFile: method?
You can write to your application's temp directory (if you just need a temporarily place to put something during a run) or the Documents directory (if you need to store something more permanently, and also have it backed up by iTunes).
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
I found that the simulator is very forgiving in writing files but the devices of course are not. My problem was that I was creating filePath incorrectly that the simulator did not catch. Simulator should have caught this one.
Here was my wrong and correct swift code
docsDir!+"filename.dat" // wrong does not have path separator between dir and file
docsDir!.stringByAppendingPathComponent("filename.dat") // works since it puts the "/" in between the two.