1

I'm trying to share file originally generated in watch App Extension but it's not working. I am using iOS 11.3 and Watch 4.3. I am also using Simulator.

I have done like this

  1. Enable App domain in Main app and Watch App Extension.

  2. App domain in selected in both with identifier.

REF : https://www.techotopia.com/index.php/Sharing_Data_Between_a_WatchKit_App_and_the_Containing_iOS_App

Code added:

In Watch App Extension:

    do {
        let fileManager = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: AppDomain)
        guard let url = fileManager?.appendingPathComponent("TabCareLogs.txt") else { return }
        try SOME_STRING.appendLineToURL(fileURL: url)
        print("WatchKit: FILE PATH \(url.path)")
        let result = try String(contentsOf: url, encoding: .utf8)
        debugPrint("############# FILE: \(result) #############")
    } catch let error {
        print("Could not write to file : \(error.localizedDescription)")
    }

extension String {

    func appendLineToURL(fileURL: URL) throws {
        try (self + "\n").appendToURL(fileURL: fileURL)
    }

    func appendToURL(fileURL: URL) throws {
        let data = self.data(using: .utf8)!
        try data.append(fileURL: fileURL)
    }
}

extension Data {
    func append(fileURL: URL) throws {
        if let fileHandle = FileHandle(forWritingAtPath: fileURL.path) {
            defer {
                fileHandle.closeFile()
            }
            fileHandle.seekToEndOfFile()
            fileHandle.write(self)
        }
        else {
            try write(to: fileURL, options: .atomic)
        }
    }
}

WatchKit: FILE PATH /Users/abhishek/Library/Developer/CoreSimulator/Devices/D515C513-3975-4C8F-B07D-6A0CCCD1A8EC/data/Containers/Shared/AppGroup/90DC2A1A-61D6-440B-A499-9EA608CE0C2E/TabCareLogs.txt

I'm getting file as well as its content.

In Main App:

 let fileManager = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: AppDomain)
        guard let url = fileManager?.appendingPathComponent("TabCareLogs.txt") else { return nil }
        print("FILE PATH \(url.path)")
        print("FILE PATH ABSOLUTE \(url.absoluteString)")
        let exists = FileManager.default.fileExists(atPath: url.path)
        print("FILE EXIST : \(exists)")

FILE PATH /Users/abhishek/Library/Developer/CoreSimulator/Devices/D5CED7C4-B674-4BF7-A72C-C063F733F4E7/data/Containers/Shared/AppGroup/77C1EAC2-2A86-4763-973F-33929C909619/TabCareLogs.txt

FILE PATH ABSOLUTE file:///Users/abhishek/Library/Developer/CoreSimulator/Devices/D5CED7C4-B674-4BF7-A72C-C063F733F4E7/data/Containers/Shared/AppGroup/77C1EAC2-2A86-4763-973F-33929C909619/TabCareLogs.txt

FILE EXIST : false

I have tried to print the path I'm getting path bit if I'm checking file exist then its is showing false.

Same issue : NSFileManager.defaultManager().fileExistsAtPath returns false instead of true

Also the paths are different I don't know why it is going to different path after using shared container?

halfer
  • 18,701
  • 13
  • 79
  • 158
Abhishek Thapliyal
  • 2,682
  • 3
  • 22
  • 55
  • @Dávid Pásztor : Can you help me here?? – Abhishek Thapliyal May 09 '18 at 12:46
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer May 09 '18 at 14:26
  • Sure @halfer i will keep this thing – Abhishek Thapliyal May 09 '18 at 14:34

0 Answers0