2012-08-09 80 views
2

由於iPhone是沙盒應用程序,我無法訪問/bin/文件夾。所以我使用SSH連接從iPhone獲取/bin/date二進制文件,並將其包含在我的項目中。當我使用NSLog打印時,我的文件路徑是正確的:/var/mobile/Applications/95078888-DDA8-4C1E-93DC-1F9E0A26E70A/Documents/date。我遇到的問題列在下面。有誰知道我可以如何解決這個錯誤?注意:如果我在模擬器中運行它,並使用此代碼執行與Mac OSX兼容的任何二進制文件,但是當我嘗試使用iPhone二進制文件在設備上運行它時,它會導致出現問題。在越獄iPhone上執行二進制文件

錯誤:

2012-08-09 14:23:13.757 TestBinary[7891:707] Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'couldn't fork: errno 1'

First throw call stack: (0x359b388f 0x335d7259 0x359b3789 0x359b37ab 0x34deb915 0xda5af 0x3590d3fd 0x330cee07 0x330cedc3 0x330ceda1 0x330ceb11 0x330cf449 0x330cd92b 0x330cd319 0x330b3695 0x330b2f3b 0x336a522b 0x35987523 0x359874c5 0x35986313 0x359094a5 0x3590936d 0x336a4439 0x330e1cd5 0xd9ecd 0xd9e98)

terminate called throwing an exception

Program received signal: 「SIGABRT」. Data Formatters temporarily unavailable, will re-try after a 'continue'. (Can't find dlopen function, so it is not possible to load shared libraries.)

mi_cmd_stack_list_frames: Not enough frames in stack.

mi_cmd_stack_list_frames: Not enough frames in stack.

代碼調用號文件:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 

NSString *path = [NSString stringWithFormat:@"%@/date", documentsDirectory]; 

NSLog(@"%@", path); 

NSTask *task = [[NSTask alloc] init]; 
[task setLaunchPath: path]; 

NSPipe *pipe; 
pipe = [NSPipe pipe]; 
[task setStandardOutput: pipe]; 

NSFileHandle *file; 
file = [pipe fileHandleForReading]; 

[task launch]; 

NSData *data; 
data = [file readDataToEndOfFile]; 

NSString *string; 
string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; 

label.numberOfLines=0; 
label.text = string; 

[string release]; 
[task release]; 

我NSTask文件:

#import <Foundation/NSObject.h> 

@class NSString, NSArray, NSDictionary; 

@interface NSTask : NSObject 

- (id)init; 

- (void)setLaunchPath:(NSString *)path; 
- (void)setArguments:(NSArray *)arguments; 
- (void)setEnvironment:(NSDictionary *)dict; 
- (void)setCurrentDirectoryPath:(NSString *)path; 
- (void)setStandardInput:(id)input; 
- (void)setStandardOutput:(id)output; 
- (void)setStandardError:(id)error; 

- (NSString *)launchPath; 
- (NSArray *)arguments; 
- (NSDictionary *)environment; 
- (NSString *)currentDirectoryPath; 

- (id)standardInput; 
- (id)standardOutput; 
- (id)standardError; 

- (void)launch; 

- (void)interrupt; 
- (void)terminate; 

- (BOOL)suspend; 
- (BOOL)resume; 

- (int)processIdentifier; 
- (BOOL)isRunning; 

- (int)terminationStatus; 

@end 

@interface NSTask (NSTaskConveniences) 

+ (NSTask *)launchedTaskWithLaunchPath:(NSString *)path arguments:(NSArray *)arguments; 

- (void)waitUntilExit; 

@end 

FOUNDATION_EXPORT NSString * const NSTaskDidTerminateNotification; 
#endif 

編輯1:

我用下面的目錄解壓縮:https://github.com/samsoffes/ssziparchive

這是我的代碼來解壓縮並執行:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 

NSString *path2 = [[NSBundle mainBundle] pathForResource:@"date" ofType:@"zip"]; 
[SSZipArchive unzipFileAtPath: path2 toDestination:documentsDirectory]; 

NSString *path = [NSString stringWithFormat:@"%@/date", documentsDirectory]; 

NSFileManager* fileManager = [NSFileManager defaultManager]; 
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary:[fileManager attributesOfItemAtPath:path error:nil]]; 

[attributes setValue:[NSNumber numberWithShort: 0777] 
       forKey:NSFilePosixPermissions]; 
NSError* err; 
[fileManager setAttributes: attributes ofItemAtPath: path error: &err]; 

NSTask *task = [[NSTask alloc] init]; 
[task setLaunchPath: path]; 

NSPipe *pipe; 
pipe = [NSPipe pipe]; 
[task setStandardOutput: pipe]; 

NSFileHandle *file; 
file = [pipe fileHandleForReading]; 

[task launch]; 

NSData *data; 
data = [file readDataToEndOfFile]; 

NSString *string; 
string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; 

label.numberOfLines=0; 
label.text = string; 

[string release]; 
[task release]; 
+0

這沒有回答我的問題。首先,我的設備已經越獄了,這是答案的一半,並且他們明確指出,如果您移植了頭文件,那麼它應該適用於iOS,儘管它不受Apple支持。我可以使用這個NSTask在其他文件上設備,他們工作正常。它只是我遇到的問題。 – MrHappyAsthma 2012-08-09 19:25:28

回答

1

我不知道究竟爲什麼正在發生的事情,但是當Xcode的構建,它以某種方式破壞date二進制文件。如果你看一下原來的date文件(項目文件夾下),然後找到Xcode的構建目錄下的同一個文件(例如):

/Users/myusername/Library/Developer/Xcode/DerivedData/HelloJB-gsokzlpnejddadbccgrfkxnumkyl/Build/Products/Release-iphoneos/HelloJB.app 

你可以在這兩個文件運行diff命令,它會告訴你這些二進制文件不同。這足以導致這個問題。

由於某種原因,Xcode正在尋找這種類型的資源,併爲此做了一些事情。例如,我知道它試圖在您的應用程序的png資源上使用某種pngcrush實用程序。也許這是相似的。

無論如何,我發現解決這個問題的一種方法是在您的Mac上壓縮date文件。然後,在項目中包含date.zip作爲捆綁資源。清理並建立。 Xcode不會破壞壓縮文件。現在

,當你的應用程序啓動時,它需要從它的捆綁資源解壓date.zip文件,並存儲解壓縮後的版本文件,或緩存,或任何你想要它。您可能還需要記住設置執行權限。

See this for programmatically unzipping files

更新:

之後,你可能需要設置該文件具有可執行權限。像這樣:

NSFileManager* fileManager = [NSFileManager defaultManager]; 
NSMutableDictionary* attributes = [[NSMutableDictionary alloc] init]; 
NSNumber* permission = [NSNumber numberWithLong: 0755]; 
[attributes setObject:permission forKey: NSFilePosixPermissions]; 
NSError* err; 
[fileManager setAttributes: attributes ofItemAtPath: filePath error: &err]; 
+0

我使用該鏈接的zip函數,並且解壓縮的很好,但我認爲該文件在解壓縮時不可執行。如何從應用內更改文件權限? (我只是將它解壓縮到文檔目錄中)。 – MrHappyAsthma 2012-08-15 17:15:01

+0

@MHHappyAsthma,請參閱我上面的更新。 – Nate 2012-08-17 02:05:16

+0

這確實更改了權限。它導致文件根據終端「ls -al」可執行,但是我仍然得到以下錯誤:「由於未捕獲異常'NSInternalInconsistencyException',原因:'could not fork:errno 1'」終止應用程序「。 *注意:這是在設備上。 – MrHappyAsthma 2012-08-19 15:56:45