iOS8不允许创建目录操作

时间:2014-11-26 02:45:37

标签: objective-c ios8 ota

这是一个奇怪的问题需要帮助。需要在iOS8上安装多个应用程序,它们具有相同的源代码和资源,唯一的区别是这些应用程序的CFBundleIdentifier和CFBundleDisplayName。

在iOS8设备上安装两个ipa,之前的应用程序将崩溃,最新的工作正常!!

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    [self createStorage];
    //ASIDownloadCache *chache = [ASIDownloadCache sharedCache];
    return YES;
}

- (NSString*)storagePath
{
    return [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"ASIHTTPRequestCache"];
}

- (void)clearStorage
{
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    BOOL isDirectory = NO;
    BOOL exists = [fileManager fileExistsAtPath:[self storagePath] isDirectory:&isDirectory];
    if (!exists || !isDirectory) {
        return;
    }
    NSError *error = nil;
    NSArray *cacheFiles = [fileManager contentsOfDirectoryAtPath:[self storagePath] error:&error];
    if (error) {
        NSLog(@"[CLEAR]IOS8 ERROR:%@", error);
        [NSException raise:@"FailedToTraverseCacheDirectory" format:@"Listing cache directory failed at path '%@'", [self storagePath]];
    }
}

// If install two versions app on iOS8. previous app will CRASH and latest works FINE.
- (void)createStorage
{
    [self clearStorage];
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    BOOL isDirectory = NO;
    BOOL exists = [fileManager fileExistsAtPath:[self storagePath] isDirectory:&isDirectory];
    if (exists && !isDirectory) {
        [NSException raise:@"FileExistsAtCachePath" format:@"Cannot create a directory for the cache at '%@', because a file already exists", [self storagePath]];
    }

    NSError *error = nil;
    [fileManager createDirectoryAtPath:[self storagePath] withIntermediateDirectories:NO attributes:nil error:&error];
    if ( error )
    {
        NSLog(@"error for create directory: %@", error); 
        // previous app will crash. Organizer--->My Device--->Console output: \
                    error for create directory: Error Domain=NSCocoaErrorDomain Code=513 "The operation couldn’t be completed. (Cocoa error 513.)" UserInfo=0x178075140 {NSFilePath=/var/mobile/Containers/Data/Application/CC220B9D-4E02-4D78-A1DB-3E4A3DA3EEB7/Library/Caches/ASIHTTPRequestCache, NSUnderlyingError=0x178048f40 "The operation couldn’t be completed. Operation not permitted"}
        [NSException raise:@"FailedToCreateCacheDirectory" format:@"Failed to create a directory for the cache at '%@'", [self storagePath]];
    }
}

我写了一个shell脚本,它可以重写CFBundleIdentifier和CFBundleDisplayName,调用xcrun生成新的ipa文件。我已经将项目和shell脚本提交给github。 github link

0 个答案:

没有答案
相关问题