2013年4月18日木曜日

ローカルにファイル保存


    //Get Directory path
    NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier];
    NSFileManager *fm = [NSFileManager defaultManager];
    NSURL *dirPath = nil;
    
    NSArray *appSupportDir = [fm URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask];
    
    if ([appSupportDir count ] >0)
    {
        dirPath= [[appSupportDir objectAtIndex:0]URLByAppendingPathComponent:bundleID];
        NSError *theErr = nil;
        if (![fm createDirectoryAtURL:dirPath withIntermediateDirectories:YES attributes:nil error:&theErr])
        {
            //error process
        }
    }
    
    //Write
    dirPath = [dirPath URLByAppendingPathComponent:@"aaa.txt"];
    
    BOOL result = [str writeToURL:dirPath atomically:YES encoding:NSUTF8StringEncoding error:&err];
    
    if (result) NSLog(@"Success write");
    
    //Read
    NSString *str1 = [NSString stringWithContentsOfURL:dirPath encoding:NSUTF8StringEncoding error:&err];
    
    NSLog(@"Read: %@",str1);

0 件のコメント:

コメントを投稿