내 앱에서 버튼을 클릭하면 로그 파일에 쓰고 싶습니다. UITextView
내 데이터를 표시하고 싶습니다. UITextView를 토글하는 방법을 알고 있지만 로컬 파일 시스템에서 로그 파일을 작성하고 업데이트하는 방법을 알지 못합니다. 어떤 도움을 주셔서 감사합니다.iOS 앱에서 로그 파일 만들기
11
A
답변
32
기본 아이디어는 파일을 만들고 새 줄을 기록 할 때마다 추가한다는 것입니다.
파일에 쓰기 : 당신은 다음과 같이 아주 쉽게 그것을 할 수
NSString *content = @"This is my log";
//Get the file path
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:@"myFileName.txt"];
//create file if it doesn't exist
if(![[NSFileManager defaultManager] fileExistsAtPath:fileName])
[[NSFileManager defaultManager] createFileAtPath:fileName contents:nil attributes:nil];
//append text to file (you'll probably want to add a newline every write)
NSFileHandle *file = [NSFileHandle fileHandleForUpdatingAtPath:fileName];
[file seekToEndOfFile];
[file writeData:[content dataUsingEncoding:NSUTF8StringEncoding]];
[file closeFile];
읽기 : 나는 거기에 클래스가 자동으로 생성 운 이후로 이렇게라고 생각
//get file path
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:@"myFileName.txt"];
//read the whole file as a single string
NSString *content = [NSString stringWithContentsOfFile:fileName encoding:NSUTF8StringEncoding error:nil];
1
내 자신의.
NSLogger는 iOS 버전 3.0 이상을위한 경량 클래스입니다. 개발자는 .txt 파일로 로컬에 저장된 여러 가지 '이벤트'를 쉽게 기록 할 수 있습니다.
관련 문제
- 1. iOS - 앱에서 로그 아웃
- 2. iOS 앱에서 프록시 만들기
- 3. iOS 앱에서 로그인하고 로그 아웃
- 4. ios 앱에서 트위터 로그 아웃이 가능합니까?
- 5. iOS 파일 만들기
- 6. 로그 파일 및 오류보고 만들기
- 7. 암호화 된 로그 파일 만들기
- 8. Java에서 XML 로그 파일 만들기
- 9. iOS 앱에서 문서 폴더 내에 폴더 만들기
- 10. iOS 앱에서 공유 버튼 만들기 (twitter facebook ...)
- 11. iOS 앱에서 양식에 연결하기위한 로그인 화면 만들기
- 12. ios 에뮬레이터에서 Mac 폴더의 파일 만들기
- 13. iOS - 서버에 plist 파일 만들기
- 14. iPhone 응용 프로그램에서 다중 로그 파일 만들기
- 15. log4j를 사용하여 다른 로그 파일 만들기
- 16. Log4Net을 사용하여 ReadOnly 로그 파일 만들기
- 17. 런타임에 log4j 로그 파일 이름 만들기
- 18. Linq to XML을 사용하여 로그 파일 만들기
- 19. log4j로 다른 내용의 여러 로그 파일 만들기
- 20. Windows Mobile 6에서 로그 파일 만들기
- 21. Visual Studio에서 빌드에 Subversion 로그 파일 만들기
- 22. SendSms 및 수신 SMS에 로그 파일 만들기
- 23. iOS 앱에서 AdMob 사용
- 24. iOS : 아이콘 만들기
- 25. iOS 앱에서 URL 검색
- 26. iOS 앱에서 STL 사용하기
- 27. iOS 앱에서 이메일 열기
- 28. iOS 앱에서 힘내 지원
- 29. iOS 앱에서 데이터베이스 액세스
- 30. iOS 앱에서 sFTP에 연결
그것은 좋아하지 않는는 '[NSFileHandle fileHandleForUpdatingAtPath는 : logPath에]'logPath'이 선언되지 않은 식별자를 사용한다는 것이다. 그것을 명확히 할 수 있겠습니까? –
나쁘다 - logPath를 fileName으로 대체하십시오. 잘못된 변수. – CrimsonDiego
감사합니다. 당신이 말한 모든 일을했지만, UITextView에 데이터를 추가하고 싶을 때,'logFile.text = [NSString stringWithContentsOfFile : fileName encoding : NSUTF8StringEncoding error : nil];'아무 것도 나타나지 않았습니다. 디버깅을 한 후 로그 파일 자체가 null 인 것을 확인했습니다. 왜 그런지 알고 있니? –