0

I want to create a .json file and .text file public so that it can be read by NSItemProvider. I want to create file programmatically.

  • Refer [creating-ios-app-extension](http://swiftiostutorials.com/tutorial-creating-ios-app-extension-ios-8-perform-custom-actions-safari-content/) – Paresh Navadiya Oct 30 '15 at 07:09

2 Answers2

0

Can you refer this link, that will be helpful to play around file creation, deletion etc...

http://www.ios-developer.net/iphone-ipad-programmer/development/file-saving-and-loading/using-the-document-directory-to-store-files

Vijay
  • 791
  • 1
  • 8
  • 23
  • i want to make it public so that NSItemProvider can read it, can i give permission while creating file or any other suggestions. – Sukhmeet Hora Oct 30 '15 at 07:09
  • @SukhmeetHora Is this will be helpful to you, http://stackoverflow.com/questions/31981893/content-blocker-extension-with-a-string-instead-of-a-file – Vijay Oct 30 '15 at 07:18
0

Use the following code to write/create a .txt file in your app's Documents directory :

NSError *error;
NSString *stringToWrite = @"hello>!!new file created..";
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"myfile.txt"];
[stringToWrite writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];

To read/fetch the text file:

NSString *str = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
NSLog(@"%@", str);
Rohan
  • 2,939
  • 5
  • 36
  • 65