1

質問1.端末内にcsv形式で保存したログを、PC等に抜き出すにはどうすればいいでしょうか?
 info.plist に Application supports iTunes file sharing を作って値を YES にはしてみましたが、PCにはログができてる様子がありません。

@interface ViewController ()

@property (nonatomic, nullable) NSTimer *timer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                  target:self
                                                selector:@selector(writeLog:)
                                                userInfo:nil
                                                 repeats:YES];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.timer invalidate];
    self.timer = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    [self.timer invalidate];
    self.timer = nil;
}

- (NSInteger)batteryCapacity {
    __block NSInteger capacity = 0;
    [[[UIApplication sharedApplication] valueForKeyPath:@"statusBar.foregroundView.subviews"]
     enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
         if([obj isKindOfClass:[NSClassFromString(@"UIStatusBarBatteryItemView") class]]) {
             capacity = [[obj valueForKey:@"capacity"] integerValue];
             *stop = YES;
         }
     }];

    return capacity;
}

-(void) writeLog:(NSTimer *)timer
{
    @synchronized (self) {

        NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true) lastObject];
        NSURL *filePath = [[[NSURL URLWithString:documentDirectory]
                            URLByAppendingPathComponent:@"batteryCapacity"]
                           URLByAppendingPathExtension:@"csv"];

        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
        formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"ja_JP"];
        formatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"JST"];
        formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss ZZZ";

        NSString *wirteData = [NSString stringWithFormat:@"%@, %zd\r\n", [formatter stringFromDate:[NSDate date]], [self batteryCapacity]];

        NSFileManager *fileManager = [NSFileManager defaultManager];
        if(![fileManager fileExistsAtPath:filePath.absoluteString]){
            [fileManager createFileAtPath:filePath.absoluteString contents:nil attributes:nil];
        }

        NSError *error = nil;
        NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingToURL:filePath error:&error];
        if(error){
            // error handling
            NSLog(@"%@", [error localizedDescription]);
            return;
        }
        [fileHandle seekToEndOfFile];
        [fileHandle writeData:[wirteData dataUsingEncoding:NSUTF8StringEncoding]];
        [fileHandle closeFile];
    }
}

@end

0

この質問に回答するには、ログインする必要があります。

のタグが付いた他の質問を参照する。