Global Notifications
VideoKitCore offers a variety of global notifications sent through NSNotification that give you the flexibility to react on certain events.
.VKSessionStateChanged
This notification is fired after the session state changed. This could be the case after an authentication attempt or logout.
Register an observer for session state changes:
NotificationCenter.default.addObserver(self, selector: #selector(self.stateChanged(_:)), name: .VKAccountStateChanged, object: nil)
React on changed states:
func stateChanged(_ notification: NSNotification? = nil) {
DispatchQueue.main.async {
if let sessionData = notification?.userInfo?["sessionData"] as? [String: Any] {
print(sessionData)
}
if VKSession.current.state == .connected {
// self.sessionConnected()
}
else if VKSession.current.state == .disconnected {
// self.sessionDisconnected()
}
else if VKSession.current.state == .error {
// self.sessionError()
}
else if VKSession.current.state == .connecting {
// self.sessionConnecting()
}
}
}
.VKVideoUploadsUpdated
Triggered after video uploading has made some progress (upload started, progress, or finished). Userinfo is empty.
You can access all uploads through VKUploads.shared.uploads
.
NotificationCenter.default.addObserver(self, selector: #selector(self.handleVideoUploadsUpdate(_:)), name: .VKVideoUploadsUpdated, object: nil)
@objc func handleVideoUploadsUpdate(notification: Notification) {
let info = VKVideoUploads.shared.uploads.map({ upload in
return "\(upload.progress) \(upload.isCompressed)"
})
print("Upload progress: \(info)")
}
Use this notification if your app can have multiple uploads at the same time and you want to handle their progress simultaneously.
.VKVideoUploadProgress
Retrieve uploading progress for each video upload individually:
NotificationCenter.default.addObserver(forName: .VKVideoUploadProgress, object: self, queue: nil) { [weak self] (notification) in
let userInfo = notification.userInfo
if let upload: VKUpload as userInfo["upload"] as? VKUpload {
print(upload.videoID)
print(upload.progress)
}
}
Notification.userInfo
[
"upload": VKUpload
]
.VKVideoUploadComplete
Upload is complete:
NotificationCenter.default.addObserver(forName: .VKVideoUploadComplete, object: self, queue: nil) { [weak self] (notification) in
let userInfo = notification.userInfo
if let upload: VKUpload as userInfo["upload"] as? VKUpload {
print("Video has been uploaded: ")
print(upload.videoID)
print(upload.progress)
}
}
Notification.userInfo
[
"upload": VKUpload
]
.VKVideoUploadFailed
NotificationCenter.default.addObserver(forName: .VKVideoUploadFailed, object: self, queue: nil) { [weak self] (notification) in
let userInfo = notification.userInfo
if let videoID as userInfo["videoID"] as? String {
print("Uploading failed for video \(videoID).")
}
}
Notification.userInfo
[
"videoID": String,
"error": Error
]
.VKVideoUploadCanceled
NotificationCenter.default.addObserver(forName: .VKVideoUploadCanceled, object: self, queue: nil) { [weak self] (notification) in
let userInfo = notification.userInfo
if let videoID as userInfo["videoID"] as? String {
print("Uploading cancelled for video \(videoID).")
}
}
Notification.userInfo
[
"videoID": String
]
.VKVideoIsUploaded
NotificationCenter.default.addObserver(forName: .VKVideoIsUploaded, object: self, queue: nil) { [weak self] (notification) in
let userInfo = notification.userInfo
if let videoID as userInfo["videoID"] as? String {
print("Video \(videoID) was uploaded.")
}
}
Notification.userInfo
[
"videoID": String
]
.VKVideoUploadStarted
NotificationCenter.default.addObserver(forName: .VKVideoUploadStarted, object: self, queue: nil) { [weak self] (notification) in
let userInfo = notification.userInfo
if let videoID as userInfo["videoID"] as? String {
print("Video \(videoID) started uploading..")
}
}
Notification.userInfo
[
"videoID": String
]
.VKVideoRecordingFailed
NotificationCenter.default.addObserver(self, selector: #selector(self.recordingFailed(_:)), name: .VKVideoRecordingFailed, object: nil)
.VKPlaylistUpdated
Called if any of our Playlists (VKPlaylist
) are updated with new videos.
NotificationCenter.default.addObserver(forName: .VKPlaylistUpdated, object: self, queue: nil) { [weak self] (notification) in
let userInfo = notification.userInfo
if let playlist = userInfo["playlist"] as? VKPlaylist {
print("Updated playlist has \(playlist.count) videos.")
}
}
Notification.userInfo
[
"playlist": self,
"newIndexes": [Int],
"oldIndexes" : [Int],
"newCount": Int,
"localVideo": Bool
]
.VKVideoPlaybackFailed
Called if VKPlayerViewController
has trouble playing the video.
NotificationCenter.default.addObserver(forName: .VKVideoPlaybackFailed, object: self, queue: nil) { [weak self] (notification) in
let userInfo = notification.userInfo
if let error = userInfo["error"] as? Error, let videoId = userInfo["videoID"] as? String {
print("There was an error playing video with id \(videoId).")
print(error.localizedDescription)
}
}
Notification.userInfo
[
"error": Error,
"videoID": String
]
.VKMetadataValidationError
Triggered if you change the metadata for VKVideo and validation failed.
NotificationCenter.default.addObserver(self, selector: #selector(self.metadataValidationError(_:)), name: .VKMetadataValidationError, object: nil)
.VKVideoClipsWereMerged
Triggered after the recorder merged your video clips.
Only triggered if auto merging is active, not if you manually trigger mergeClips()
NotificationCenter.default.addObserver(forName: .VKVideoClipsWereMerged, object: self, queue: nil) { [weak self] (notification) in
let userInfo = notification.userInfo
if let url = userInfo["url"] as? URL {
print("Merged video is located at \(url.absoluteString).")
}
}
Notification.userInfo
[
"url": URL
]
.VKStreamCreated
Triggered after the stream was created
NotificationCenter.default.addObserver(forName: .VKStreamCreated, object: self, queue: nil) { (notification) in
let userInfo = notification.userInfo
if let stream = userInfo?["stream"] as? VKStream {
print("Stream with id \(stream.id) and push url \(stream.input[.url] ?? "unknown") was created.")
print(stream)
}
}
Notification.userInfo
[
"stream": VKStream
]
.VKStreamDeleted
Triggered after the stream was removed
NotificationCenter.default.addObserver(forName: .VKStreamDeleted, object: self, queue: nil) { (notification) in
let userInfo = notification.userInfo
if let streamId = userInfo?["streamId"] as? String {
print("Stream with id \(streamId) was removed.")
}
}
Notification.userInfo
[
"streamId": String
]