I'm studying iOS push notifications (user facing). I have done the following steps:
- Requested User's permission to show notification
- Registered for remote notifications and obtained the device token.
- I'm using the command line way to push notifications to the device.
- The notification service extension gets invoked first. Here, you can 'decode' the data in notification payload to show the actual notification.
- Decode is successful and I'm able to see the actual notification in my testing device.
The problem is logging. The log file is stored in shared container, since the extension cannot access the Documents directory in the app container.
class Logger {
static var sFileHandle: FileHandle? = nil
static var TAG: String = "Empty"
init(tag pTAG: String) {
Logger.TAG = pTAG
let file_manager: FileManager = FileManager.default
// In case of an app with extensions, use the shared container directory
// (add capability in Xcode project settings) to store logs.
// Get the URL to the shared container directory.
guard let shared_container: URL = file_manager.containerURL(forSecurityApplicationGroupIdentifier:"group.iospushnotifications") else {
NSLog(Logger.TAG + "Error in obtaining URL to shared container directory!")
return
}
let file_path: URL = shared_container.appending(component: "Logs.txt")
// Check if file exists. If not, create it..
if(!file_manager.fileExists(atPath: file_path.path())) {
NSLog(Logger.TAG + "Logs.txt doesn't exist")
NSLog(Logger.TAG + "Creating %@...", file_path.path())
if(!file_manager.createFile(atPath: file_path.path(), contents: nil)) {
NSLog(Logger.TAG + "Error in creating file: %@", file_path.path())
return
} else {
NSLog(Logger.TAG + "%@ created!!", file_path.path())
}
} else {
NSLog(Logger.TAG + "%@ exists!", file_path.path())
}
// Initialize FileHandle object for writing to file.
do {
try TWLogger.sFileHandle = FileHandle(forWritingTo: file_path)
} catch {
NSLog(Logger.TAG + "Error in initializing file handle!\n error.localizedDescription = %@", error.localizedDescription)
return
}
}
deinit {
// Delete the file handle
}
func Log(_ pLogString: String) -> Void {
// Print to stdout (Xcode console & Console app)
// Write to file using FileHandle object
}
}
When the app logs to file, it works. But when extension logs to file, it fails with the following error:
Error in obtaining URL to shared container directory!
This means, the URL was nil, which can only happen when the group id is invalid, which is not possible here since app and extension use the same code.
Before implementing the notification service extension, I download the app container, and check the log file in Documents directory in my mac. But now, I can't do that since the logs will be dumped to the shared container.
How do I download this shared container to view the logs? Is there any other alternative?
Update1: I'm now able to write to the shared container from the extension (It was a silly error - I had missed to set the AppGroup capability in the extension target). But still dunno how to download the shared container to view the log file.