How do I pass In-App Purchase data to Watch? - swift

I have part of my Apple Watch app behind a paywall, but I am unable to extract information from the WCSession transferUserInfo session.
Also, how do I store that data on the Watch app as no NSUserDefaults are available on the Watch?
Here is my code, that does open the session, but does not pass data correctly:
ViewController
//This creates the dictionary that will be passed to the Watch
var WatchInAppPurchases = ["product1":Bool(), "product2":Bool(),]
func dictionaryCreator() {
if (UserDefaults.standard.value(forKey: "product1purchased") != nil) == true {
WatchInAppPurchases["product1"] = true
} else {
WatchInAppPurchases["product1"] = false
}
if (UserDefaults.standard.value(forKey: "product2purchased") != nil) == true {
WatchInAppPurchases["product2"] = true
} else {
WatchInAppPurchases["product2"] = false
}
}
//This runs both with the .purchased and .restored function.
func syncronizeWatch() {
if WCSession.isSupported() {
let session = WCSession.default
session.delegate = self
session.activate()
session.transferUserInfo(WatchInAppPurchases)
}
}
Interface controller:
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
NSLog("%#", "activationDidCompleteWith activationState:\(activationState) error:\(String(describing: error))")
}
var userinfo0 : Bool?
var userinfo1 : Bool?
func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any] = [:]) {
let userinfodic = userInfo
userinfo0 = userinfodic["product"]!
userinfo1 = userinfodic["product2"]!
}
After this runs, how am I supped store this data correctly?
if userInfo0 {
//show paid content?
}

Related

How to open a particular ViewController when notification received and Touch ID is enabled

I am Newbie to iOS Swift. I have added LocalAuthentication(TouchID) feature in my app. When touch id is enabled I am redirecting user on Dashboard Page, on successful authentication. Now when push notification received if it is of category type blog, i wants to open blog page, if it is of category type payouts, i wants to open payout page and if it is news i wants to open notification page. But due to touch id is enable i am redirecting to Dashboard page and not on specific ViewController on notification received. Don't understand how to deal with this scenario.
What i am doing when received notification :
func navigateToView(userInfo: [AnyHashable: Any]) {
let userInfo = userInfo
if userInfo[AnyHashable("click_action")] != nil{
category = (userInfo[AnyHashable("click_action")] as? String)!
}
let userauthenticate = UserDefaults.standard.bool(forKey: "userauth")
print("USERINFOCOUNT: \(userInfo.count) \n CATEGORY : \(category) \n USERAUTH: \(userauthenticate)")
if(userauthenticate == true && category.isEmpty){
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewController(withIdentifier: "Dashboard") as UIViewController
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
} else if(userauthenticate == true && category == "notification"){
saveNewNotificationInBackground(userInfo: userInfo)
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "openNotifications"), object: nil)
defaults.set(userInfo, forKey: "userInfo")
defaults.synchronize()
}
}
My authentication function is as below :
import UIKit
import Foundation
import Firebase
import FirebaseMessaging
import UserNotifications
import FirebaseCore
import FirebaseInstanceID
import Alamofire
import SwiftyJSON
import SQLite3
import LocalAuthentication
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let gcmMessageIDKey = "gcm.message_id"
var userid : String = ""
var type : String = ""
var image : String = ""
var body : String = ""
var title : String = ""
var subtitle : String = ""
var category : String = ""
var badge : Int32 = 0
var badgecount : Int32 = 0
var aps: NSDictionary = NSDictionary()
var alert: NSDictionary = NSDictionary()
var usernamelen : Int = 0
var passwordlen : Int = 0
var notificationLists = [NotificationObj]()
var db: OpaquePointer?
let logevent = LogEvents()
var bridge: RCTBridge!
var forceUpdateCount : Int = 0
var reach: Reachability?
let defaults = UserDefaults.standard
var notificationType : String = ""
var notificationTitle : String = ""
var message : String = ""
var link : String = ""
var image_url : String = ""
var read_status : String = ""
var isfirstitmelaunch : Bool = true
var cemail : String = ""
var cpass : String = ""
var cid : String = ""
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]?) -> Bool {
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
Messaging.messaging().delegate = self
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
FirebaseApp.configure()
NotificationCenter.default.addObserver(self,
selector: #selector(self.tokenRefreshNotification),
name: NSNotification.Name(rawValue: "pushNotification"),
object: nil)
UserDefaults.standard.removeObject(forKey: "checkcomingfrom")
UserDefaults.standard.set(false, forKey: "checkcomingfrom")
cemail = defaults.string(forKey: "username") ?? ""
cpass = defaults.string(forKey: "password") ?? ""
cid = defaults.string(forKey: "id") ?? ""
if(cid != nil){
logevent.logFirebaseEvent(name: "app_launch", params: ["userid" : cid])
}
if(launchOptions != nil){
let userInfo = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification]
if userInfo != nil {
print("USERINFO \(String(describing: userInfo))")
}
}
if(cemail == nil){
usernamelen = 0
}else{
usernamelen = (cemail.count)
}
if(cpass == nil){
passwordlen = 0
}else{
passwordlen = (cpass.count)
}
let firstTime = UserDefaults.standard.object(forKey: "first_time") as? Bool // Here you look if the Bool value exists, if not it means that is the first time the app is opened
if(firstTime == nil){
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "NewCustomer", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewController(withIdentifier: "demoview") as UIViewController
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
defaults.set(true, forKey: "first_time")
defaults.synchronize()
}else if(usernamelen > 0 && passwordlen > 0){ // if remeber me set to true open dashboard directly
// IsFirstTimeLaunch = false
let userauthenticate = UserDefaults.standard.bool(forKey: "userauth")
print("userauthenticateforeground \(userauthenticate)")
if(userauthenticate == true){
authenticationWithTouchID()
}
else{
defaults.set(false, forKey: "first_time")
defaults.synchronize()
print("INITIALCONTROLLERISDASHBOARD")
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewController(withIdentifier: "Dashboard") as UIViewController
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
}
}else{ // else open login viewcontroller as a initial view controller
defaults.set(false, forKey: "first_time")
defaults.synchronize()
print("INITIALCONTROLLERISLOGIN")
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewController(withIdentifier: "NewLoginViewController") as UIViewController
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
}
return true
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
_ = userInfo["aps"] as? NSDictionary
NotificationCenter.default.post(name: NSNotification.Name("pushNotification"), object: nil, userInfo: userInfo)
}
//Called When Silent Push Notification Arrives
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
UIApplication.shared.applicationIconBadgeNumber = UIApplication.shared.applicationIconBadgeNumber + 1
_ = userInfo["aps"] as? NSDictionary
NotificationCenter.default.post(name: NSNotification.Name("pushNotification"), object: nil, userInfo: userInfo)
print("Message ID: \(userInfo["gcm.message_id"] ?? "")")
print("\(userInfo)")
let aps:NSDictionary = (userInfo[AnyHashable("aps")] as? NSDictionary)!
badge += 1
print("badgecount \(badge)")
if UIApplication.shared.applicationState == .inactive {
print("APPLICATIONIS INACTIVE")
//navigateToView(userInfo: userInfo)
//completionHandler(.newData)
}else if UIApplication.shared.applicationState == .background {
print("APPLICATIONIS BACKGROUND")
//navigateToView(userInfo: userInfo)
//completionHandler(UIBackgroundFetchResult.newData)
}else {
print("APPLICATIONIS FOREGROUND")
// completionHandler(UIBackgroundFetchResult.newData)
}
completionHandler(UIBackgroundFetchResult.newData)
}
func applicationReceivedRemoteMessage(_ remoteMessage: MessagingRemoteMessage) {
print("REMOTEDATA: \(remoteMessage.appData)")
}
// [END receive_message]
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
let statusBarTappedNotification = Notification(name: Notification.Name(rawValue: "statusBarTappedNotification"))
let statusBarRect = UIApplication.shared.statusBarFrame
guard let touchPoint = event?.allTouches?.first?.location(in: self.window) else { return }
if statusBarRect.contains(touchPoint) {
NotificationCenter.default.post(statusBarTappedNotification)
}
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Unable to register for remote notifications: \(error.localizedDescription)")
}
// This function is added here only for debugging purposes, and can be removed if swizzling is enabled.
// If swizzling is disabled then this function must be implemented so that the APNs token can be paired to
// the FCM registration token.
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("APNs token retrieved: \(deviceToken)")
//InstanceID.instanceID().setAPNSToken(deviceToken, type: InstanceIDAPNSTokenType.unknown)
// With swizzling disabled you must set the APNs token here.
Messaging.messaging().apnsToken = deviceToken as Data
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
print("DIDRESIGNACTIVE")
}
func applicationDidEnterBackground(_ application: UIApplication) {
print("DIDBACKGROUND")
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
Messaging.messaging().shouldEstablishDirectChannel = false
}
func applicationWillEnterForeground(_ application: UIApplication) {
print("applicationWillEnterForegroundcalled")
let forcemail : String = defaults.string(forKey: "username") ?? ""
let forcpass : String = defaults.string(forKey: "password") ?? ""
print("CUNAME : \(forcemail) \n CPASS : \(forcpass)")
// let userauthenticate = UserDefaults.standard.bool(forKey: "userauth")
// if(forcemail.count > 0 && forcpass.count > 0){
// if(userauthenticate == true){
// authenticationWithTouchID()
//
// }
// }
}
func applicationDidBecomeActive(_ application: UIApplication) {
print("DIDBECOMEACTIVE")
connectToFcm()
}
func applicationWillTerminate(_ application: UIApplication) {
}
func registerFcmToken(token : String, id : String, logintoken : String){
let systemVersion = UIDevice.current.systemVersion //11.4
let modelName = UIDevice.modelName //iPhone 6s plus
let systemName = UIDevice.current.systemName //iOS
let name = UIDevice.current.name //Sagar's iPhone
print("APPDELEGATEFCM : \(token)")
let url : String = "https://moneyfrog.in/register-token"
let params : [String : String] = ["user_id":id , "device_type":"ios" ,"token_no":token, "device_os_version" : systemVersion, "device_api_level" : "","device_name" : name, "device_model" : modelName, "device_product": UIDevice.current.model, "login_token" : logintoken]
Alamofire.request(url, method: .post, parameters: params).responseString{
response in
if response.result.isSuccess{
print("DEVICEINFO \(params)")
//let data : String = String(response.result.value!)
print("responseis : \(response) \n TOKENDATA ADDED")
}
}
}
// [START refresh_token]
#objc func tokenRefreshNotification(_ notification: Notification) {
if let refreshedToken = InstanceID.instanceID().token() {
print("InstanceID token: \(refreshedToken)")
}
}
// [END refresh_token]
func connectToFcm() {
// Won't connect since there is no token
guard InstanceID.instanceID().token() != nil else {
return;
}
Messaging.messaging().shouldEstablishDirectChannel = true
}
func authenticationWithTouchID() {
let localAuthenticationContext = LAContext()
localAuthenticationContext.localizedFallbackTitle = "Enter Passcode"
var _: AppDelegate? = (UIApplication.shared.delegate as? AppDelegate)
let backgrView = UIView(frame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(UIScreen.main.bounds.size.width), height: CGFloat(UIScreen.main.bounds.size.height)))
backgrView.backgroundColor = UIColor.black
//backgrView.alpha = 0.9
window?.addSubview(backgrView)
let blurEffect = UIBlurEffect(style: .light)
let blurVisualEffectView = UIVisualEffectView(effect: blurEffect)
blurVisualEffectView.frame = backgrView.bounds
backgrView.addSubview(blurVisualEffectView)
var authError: NSError?
let reasonString = "Please authenticate to use Moneyfrog"
if localAuthenticationContext.canEvaluatePolicy(.deviceOwnerAuthentication, error: &authError) {
localAuthenticationContext.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reasonString) { success, evaluateError in
if success {
DispatchQueue.main.async
{
blurVisualEffectView.removeFromSuperview()
self.navigateToView(userInfo: [:])
}
} else {
//TODO: User did not authenticate successfully, look at error and take appropriate action
guard let error = evaluateError else {
return
}
print(self.evaluateAuthenticationPolicyMessageForLA(errorCode: error._code))
//TODO: If you have choosen the 'Fallback authentication mechanism selected' (LAError.userFallback). Handle gracefully
}
}
} else {
guard let error = authError else {
return
}
//TODO: Show appropriate alert if biometry/TouchID/FaceID is lockout or not enrolled
print("APP LOCKED : \(self.evaluatePolicyFailErrorMessageForLA(errorCode: error.code))")
}
}
func evaluatePolicyFailErrorMessageForLA(errorCode: Int) -> String {
var message = ""
if #available(iOS 11.0, macOS 10.13, *) {
switch errorCode {
case LAError.biometryNotAvailable.rawValue:
message = "Authentication could not start because the device does not support biometric authentication."
case LAError.biometryLockout.rawValue:
message = "Authentication could not continue because the user has been locked out of biometric authentication, due to failing authentication too many times."
case LAError.biometryNotEnrolled.rawValue:
message = "Authentication could not start because the user has not enrolled in biometric authentication."
default:
message = "Did not find error code on LAError object"
}
} else {
switch errorCode {
case LAError.touchIDLockout.rawValue:
message = "Too many failed attempts."
case LAError.touchIDNotAvailable.rawValue:
message = "TouchID is not available on the device"
case LAError.touchIDNotEnrolled.rawValue:
message = "TouchID is not enrolled on the device"
default:
message = "Did not find error code on LAError object"
}
}
return message;
}
func evaluateAuthenticationPolicyMessageForLA(errorCode: Int) -> String {
var message = ""
switch errorCode {
case LAError.authenticationFailed.rawValue:
message = "The user failed to provide valid credentials"
case LAError.appCancel.rawValue:
message = "Authentication was cancelled by application"
case LAError.invalidContext.rawValue:
message = "The context is invalid"
case LAError.notInteractive.rawValue:
message = "Not interactive"
case LAError.passcodeNotSet.rawValue:
message = "Passcode is not set on the device"
case LAError.systemCancel.rawValue:
message = "Authentication was cancelled by the system"
case LAError.userCancel.rawValue:
message = "The user did cancel"
exit(0);
case LAError.userFallback.rawValue:
message = "The user chose to use the fallback"
default:
message = evaluatePolicyFailErrorMessageForLA(errorCode: errorCode)
}
return message
}
}
// [START ios_10_message_handling]
#available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
_ = notification.request.content.userInfo
//let userInfo = notification.request.content.userInfo
// navigateToView(userInfo: userInfo)
completionHandler([.alert, .badge, .sound])
print("NotificationCenter1 called")
}//userNotificationCenter
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: #escaping () -> Void) {
print("NotificationReceived \(response.notification.request.content.userInfo)")
print("STEP NOTIFICATIONCALLED")
let userInfo = response.notification.request.content.userInfo
navigateToView(userInfo: userInfo)
completionHandler()
}//userNotificationCenter2
/* navigate to specific view controller on notification received */
func navigateToView(userInfo: [AnyHashable: Any]) {
let userInfo = userInfo
if userInfo[AnyHashable("click_action")] != nil{
category = (userInfo[AnyHashable("click_action")] as? String)!
}
let userauthenticate = UserDefaults.standard.bool(forKey: "userauth")
print("USERINFOCOUNT: \(userInfo.count) \n CATEGORY : \(category) \n USERAUTH: \(userauthenticate)")
if(userauthenticate == true && category.isEmpty){
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewController(withIdentifier: "Dashboard") as UIViewController
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
}
// else if(userauthenticate == true && category == "payout_alerts"){ // if notification is payout alert open payout alerts controller
//
//
// // post a notification
//
// NotificationCenter.default.post(name: NSNotification.Name(rawValue: "openPayoutAlerts"), object: nil)
//
// self.logevent.logFirebaseEvent(name: "notification_payout", params: ["userid": self.userid])
// /*this logs database events in the system*/
// let params : [String : String] = ["action" : "notification_payout" , "page" : "payout_alerts_screen" , "user_id" : self.userid, "source" : "iOS"] //post params
// self.logevent.logDatabaseEvents(parameters: params)
//
//
//
// }else if(userauthenticate == true && category == "blog"){ // if notification is blogs alert open blogs controller
//
// print("OPENING BLOGS")
//
// NotificationCenter.default.post(name: NSNotification.Name(rawValue: "OpenBlogs"), object: nil)
//
// self.logevent.logFirebaseEvent(name: "notification_blog", params: ["userid": self.userid])
// /*this logs database events in the system*/
// let params : [String : String] = ["action" : "notification_blog" , "page" : "blogs_screen" , "user_id" : self.userid, "source" : "iOS"] //post params
// self.logevent.logDatabaseEvents(parameters: params)
//
// }else if(userauthenticate == true && category == "chat"){ // if notification is mypost alert open My post controller
//
// NotificationCenter.default.post(name: NSNotification.Name(rawValue: "openMypostContoller"), object: nil)
//
// self.logevent.logFirebaseEvent(name: "notification_mypost", params: ["userid": self.userid])
// /*this logs database events in the system*/
// let params : [String : String] = ["action" : "notification_mypost" , "page" : "mypost_screen" , "user_id" : self.userid, "source" : "iOS"] //post params
// self.logevent.logDatabaseEvents(parameters: params)
//
// }
else if(userInfo.count > 0 && category == "notification"){ // if notification is ingeneral notification open notification controller
// saveNewNotificationInBackground(userInfo: userInfo)
//
// NotificationCenter.default.post(name: NSNotification.Name(rawValue: "openNotifications"), object: nil)
//
//
// defaults.set(userInfo, forKey: "userInfo")
// defaults.synchronize()
if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Notification") as? Notifications {
if let navigator = self.window?.rootViewController as? UINavigationController {
navigator.pushViewController(controller, animated: true)
}
}
self.logevent.logFirebaseEvent(name: "notification_received", params: ["userid": self.userid])
/*this logs database events in the system*/
let params : [String : String] = ["action" : "notification_received" , "page" : "notification_screen" , "user_id" : self.userid, "source" : "iOS"] //post params
self.logevent.logDatabaseEvents(parameters: params)
}
}
/*this function save data in NotificationDatabase.sqlite database and Notifications table*/
func saveNewNotificationInBackground(userInfo: [AnyHashable: Any]) -> Void {
//save notification using sqlite data
}
}
// [END ios_10_message_handling]
extension AppDelegate : MessagingDelegate {
// [START refresh_token]
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebasetoken: \(fcmToken)")
Messaging.messaging().subscribe(toTopic: "blog") { error in
print("subscribed to blog topic")
}
Messaging.messaging().subscribe(toTopic: "news") { error in
print("subscribed to news topic")
}
let defaults = UserDefaults.standard
defaults.set(fcmToken, forKey: "token")
defaults.synchronize()
let dataDict:[String: String] = ["token": fcmToken]
NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
let id : String? = defaults.string(forKey: "id")
let logintoken : String? = defaults.string(forKey: "login_token")
if(id != nil && logintoken != nil){
registerFcmToken(token: fcmToken, id: id!, logintoken: logintoken!)
}
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
Messaging.messaging().shouldEstablishDirectChannel = true
print("ReceivedDataMessage: \(remoteMessage.appData)")
}
// [END ios_10_data_message]
}
Replace the code in success blog with this,
func goToBlogVC() {
DispatchQueue.main.async {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let mainView = storyboard.instantiateViewController(withIdentifier: "blogVC") as? blogVC
{
self.window = UIWindow(frame: UIScreen.main.bounds)
let nav1 = UINavigationController()
nav1.isNavigationBarHidden = true //Show or hide nav bar
nav1.viewControllers = [mainView]
self.window!.switchRootViewController(nav1)
self.window?.makeKeyAndVisible()
}
}
Will you brief me where you got struck, your code seems perfect but too vast the flow is Once you got a notification then trigger the authenticationWithTouchID on success go to the relevant controller[on failure let enter their passcode], below is my code
Navigation Flow
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: #escaping () -> Void)
{
if isblog {
self.jumpToScreen()
completionHandler()
}
}
func jumpToScreen() {
if let vc = self.window?.visibleViewController {
if vc.isKind(of: blogVC.self) {
return
}
else if vc.isKind(of: DashboardVC.self) {
self.goToBlogVC()
}
else {
print("Other Screens")
}
}

Getting image from AVCaptureMovieFileOutput without switching

I would like to prevent lags when the app switches between video-recording and photo-taking: by using only AVCaptureMovieFileOutput and getting a snapshot from it when captured an image.
Just like how SnapChat does.
Is it possible somehow? I haven't found any releated articles about this.
I don't want to switch between outputs, because it lags
The code:
#IBOutlet var cameraView: UIView!
#IBOutlet var cameraSwitchButton: UIButton!
#IBOutlet var captureButtonView: CaptureButton!
#IBOutlet var cameraFlashButton: UIButton!
var captureSession = AVCaptureSession()
let movieOutput = AVCaptureMovieFileOutput()
var activeInput: AVCaptureDeviceInput!
var previewLayer = AVCaptureVideoPreviewLayer()
var outputURL: URL!
var connection : AVCaptureConnection!
override func viewDidLoad() {
if setupSession() {
setupPreview()
startSession()
connection = movieOutput.connection(with: AVMediaType.video)
if (connection?.isVideoStabilizationSupported)! {
connection?.preferredVideoStabilizationMode = AVCaptureVideoStabilizationMode.off
}
}
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(captureButtonTapped))
let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(captureButtonLongPressed))
tapGesture.numberOfTapsRequired = 1
captureButtonView.addGestureRecognizer(tapGesture)
captureButtonView.addGestureRecognizer(longGesture)
}
#objc func captureButtonTapped(){
?? TAKE PHOTO HERE ??
}
var isRecordingVideo : Bool = false
#objc func captureButtonLongPressed(sender : UILongPressGestureRecognizer){
if sender.state == .began {
isRecordingVideo = true
startRecording()
captureButtonView.startTimer(duration: 10.0)
}
if sender.state == .ended || sender.state == .failed || sender.state == .cancelled {
captureButtonView.clear()
isRecordingVideo = false
stopRecording()
}
}
func setupPreview() {
// Configure previewLayer
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.frame = cameraView.bounds
previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
cameraView.layer.addSublayer(previewLayer)
}
//MARK:- Setup Camera
func setupSession() -> Bool {
captureSession.sessionPreset = AVCaptureSession.Preset.high
// Setup Camera
let camera = AVCaptureDevice.default(for: AVMediaType.video)
do {
let input = try AVCaptureDeviceInput(device: camera!)
if captureSession.canAddInput(input) {
captureSession.addInput(input)
activeInput = input
}
} catch {
print("Error setting device video input: \(error)")
return false
}
// Setup Microphone
let microphone = AVCaptureDevice.default(for: AVMediaType.audio)
do {
let micInput = try AVCaptureDeviceInput(device: microphone!)
if captureSession.canAddInput(micInput) {
captureSession.addInput(micInput)
}
} catch {
print("Error setting device audio input: \(error)")
return false
}
// Movie output
if captureSession.canAddOutput(movieOutput) {
captureSession.addOutput(movieOutput)
}
return true
}
func setupCaptureMode(_ mode: Int) {
}
//MARK:- Camera Session
func startSession() {
if !captureSession.isRunning {
videoQueue().async {
self.captureSession.startRunning()
}
}
}
func stopSession() {
if captureSession.isRunning {
videoQueue().async {
self.captureSession.stopRunning()
}
}
}
func videoQueue() -> DispatchQueue {
return DispatchQueue.main
}
func currentVideoOrientation() -> AVCaptureVideoOrientation {
var orientation: AVCaptureVideoOrientation
switch UIDevice.current.orientation {
case .portrait:
orientation = AVCaptureVideoOrientation.portrait
case .landscapeRight:
orientation = AVCaptureVideoOrientation.landscapeLeft
case .portraitUpsideDown:
orientation = AVCaptureVideoOrientation.portraitUpsideDown
default:
orientation = AVCaptureVideoOrientation.landscapeRight
}
return orientation
}
func startCapture() {
startRecording()
}
func tempURL() -> URL? {
let directory = NSTemporaryDirectory() as NSString
if directory != "" {
let path = directory.appendingPathComponent(NSUUID().uuidString + ".mp4")
return URL(fileURLWithPath: path)
}
return nil
}
func startRecording() {
if movieOutput.isRecording == false {
if (connection?.isVideoOrientationSupported)! {
connection?.videoOrientation = currentVideoOrientation()
}
let device = activeInput.device
if (device.isSmoothAutoFocusSupported) {
do {
try device.lockForConfiguration()
device.isSmoothAutoFocusEnabled = false
device.unlockForConfiguration()
} catch {
print("Error setting configuration: \(error)")
}
}
outputURL = tempURL()
movieOutput.startRecording(to: outputURL, recordingDelegate: self)
}
else {
stopRecording()
}
}
func stopRecording() {
if movieOutput.isRecording == true {
movieOutput.stopRecording()
}
}
func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
if (error != nil) {
print("Error recording movie: \(error!.localizedDescription)")
} else {
UISaveVideoAtPathToSavedPhotosAlbum(outputURL.path, nil, nil, nil)
_ = outputURL as URL
}
outputURL = nil
}
I wasn't able to find a way using only AVCaptureMovieFileOutput, however you can add an additional photo output and trigger photos without having to switch between the outputs.
I'm short on time at the moment but this should get you going till I can edit with more info.
(See EDIT with full implementation below, and limited force unwrapping)
First off setup an additional var for a photo output in your view controller
// declare an additional camera output var
var cameraOutput = AVCapturePhotoOutput()
// do this in your 'setupSession' func where you setup your movie output
cameraOutput.isHighResolutionCaptureEnabled = true
captureSession.addOutput(cameraOutput)
Declare a function to capture your photo using the cameraOutput:
func capturePhoto() {
// create settings for your photo capture
let settings = AVCapturePhotoSettings()
let previewPixelType = settings.availablePreviewPhotoPixelFormatTypes.first!
let previewFormat = [
kCVPixelBufferPixelFormatTypeKey as String: previewPixelType,
kCVPixelBufferWidthKey as String: UIScreen.main.bounds.size.width,
kCVPixelBufferHeightKey as String: UIScreen.main.bounds.size.height
] as [String : Any]
settings.previewPhotoFormat = previewFormat
cameraOutput.capturePhoto(with: settings, delegate: self)
}
and conform to the AVCapturePhotoCaptureDelegate.
I created a separate class called VideoFeed to manage the video capture session, so this sample is an extension of that class. I'll update with more info on this later.
The loadImage(data: Data) function calls a delegate with the image. You can ignore that call if you put this directly in your view controller, and save or do whatever you like with the generated photo:
extension VideoFeed: AVCapturePhotoCaptureDelegate {
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photoSampleBuffer: CMSampleBuffer?, previewPhoto previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) {
guard error == nil else {
print("Photo Error: \(String(describing: error))")
return
}
guard let sampleBuffer = photoSampleBuffer,
let previewBuffer = previewPhotoSampleBuffer,
let outputData = AVCapturePhotoOutput
.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: previewBuffer) else {
print("Oops, unable to create jpeg image")
return
}
print("captured photo...")
loadImage(data: outputData)
}
func loadImage(data: Data) {
let dataProvider = CGDataProvider(data: data as CFData)
let cgImageRef: CGImage! = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: .defaultIntent)
let image = UIImage(cgImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.right)
// do whatever you like with the generated image here...
delegate?.processVideoSnapshot(image)
}
}
EDIT:
Here's the complete implementation I used in my test project.
First I moved all the AVFoundation specific code into it's own VideoFeed class and created some callbacks to the view controller.
This separates concerns and limits the view controllers responsibilities to:
Adding the preview layer to the view
Triggering and handling the captured image/screenshot
Starting/stopping video file recording.
Here's the ViewController implementation:
ViewController.swift
import UIKit
import AVFoundation
class ViewController: UIViewController, VideoFeedDelegate {
#IBOutlet var cameraView: UIView!
var videoFeed: VideoFeed?
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// end session
videoFeed?.stopSession()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// request camera access
AVCaptureDevice.requestAccess(for: AVMediaType.video) { [weak self] granted in
guard granted != false else {
// TODO: show UI stating camera cannot be used, update in settings app...
print("Camera access denied")
return
}
DispatchQueue.main.async {
if self?.videoFeed == nil {
// video access was enabled so setup video feed
self?.videoFeed = VideoFeed(delegate: self)
} else {
// video feed already available, restart session...
self?.videoFeed?.startSession()
}
}
}
}
// MARK: VideoFeedDelegate
func videoFeedSetup(with layer: AVCaptureVideoPreviewLayer) {
// set the layer size
layer.frame = cameraView.layer.bounds
// add to view
cameraView.layer.addSublayer(layer)
}
func processVideoSnapshot(_ image: UIImage?) {
// validate
guard let image = image else {
return
}
// SAVE IMAGE HERE IF DESIRED
// for now just showing in a lightbox/detail view controller
let storyboard = UIStoryboard(name: "Main", bundle: Bundle(for: AppDelegate.self))
let vc = storyboard.instantiateViewController(withIdentifier: "LightboxViewController") as! LightboxViewController
vc.previewImage = image
navigationController?.pushViewController(vc, animated: true)
}
#IBAction func captureButtonTapped(_ sender: Any){
// trigger photo capture from video feed...
// this will trigger a callback to the function above with the captured image
videoFeed?.capturePhoto()
}
}
Here's the full implementation of the VideoFeed class.
Using this approach allows you to reuse the video functionality in other projects more easily without having it tightly coupled to the view controller.
VideoFeed.swift
import UIKit
import AVFoundation
/// Defines callbacks associated with the VideoFeed class. Notifies delegate of significant events.
protocol VideoFeedDelegate: class {
/// Callback triggered when the preview layer for this class has been created and configured. Conforming objects should set and maintain a strong reference to this layer otherwise it will be set to nil when the calling function finishes execution.
///
/// - Parameter layer: The video preview layer associated with the active captureSession in the VideoFeed class.
func videoFeedSetup(with layer: AVCaptureVideoPreviewLayer)
/// Callback triggered when a snapshot of the video feed has been generated.
///
/// - Parameter image: <#image description#>
func processVideoSnapshot(_ image: UIImage?)
}
class VideoFeed: NSObject {
// MARK: Variables
/// The capture session to be used in this class.
var captureSession = AVCaptureSession()
/// The preview layer associated with this session. This class has a
/// weak reference to this layer, the delegate (usually a ViewController
/// instance) should add this layer as a sublayer to its preview UIView.
/// The delegate will have the strong reference to this preview layer.
weak var previewLayer: AVCaptureVideoPreviewLayer?
/// The output that handles saving the video stream to a file.
var fileOutput: AVCaptureMovieFileOutput?
/// A reference to the active video input
var activeInput: AVCaptureDeviceInput?
/// Output for capturing frame grabs of video feed
var cameraOutput = AVCapturePhotoOutput()
/// Delegate to receive callbacks about significant events triggered by this class.
weak var delegate: VideoFeedDelegate?
/// The capture connection associated with the fileOutput.
/// Set when fileOutput is created.
var connection : AVCaptureConnection?
// MARK: Public accessors
/// Public initializer. Accepts a delegate to receive callbacks with the preview layer and any snapshot images.
///
/// - Parameter delegate: A reference to an object conforming to VideoFeedDelegate
/// to receive callbacks for significant events in this class.
init(delegate: VideoFeedDelegate?) {
self.delegate = delegate
super.init()
setupSession()
}
/// Public accessor to begin a capture session.
public func startSession() {
guard captureSession.isRunning == false else {
return
}
captureSession.startRunning()
}
/// Public accessor to end the current capture session.
public func stopSession() {
// validate
guard captureSession.isRunning else {
return
}
// end file recording if the session ends and we're currently recording a video to file
if let isRecording = fileOutput?.isRecording, isRecording {
stopRecording()
}
captureSession.stopRunning()
}
/// Public accessor to begin file recording.
public func startRecording() {
guard fileOutput?.isRecording == false else {
stopRecording()
return
}
configureVideoOrientation()
disableSmoothAutoFocus()
guard let url = tempURL() else {
print("Unable to start file recording, temp url generation failed.")
return
}
fileOutput?.startRecording(to: url, recordingDelegate: self)
}
/// Public accessor to end file recording.
public func stopRecording() {
guard fileOutput?.isRecording == true else {
return
}
fileOutput?.stopRecording()
}
/// Public accessor to trigger snapshot capture of video stream.
public func capturePhoto() {
// create settings object
let settings = AVCapturePhotoSettings()
// verify that we have a pixel format type available
guard let previewPixelType = settings.availablePreviewPhotoPixelFormatTypes.first else {
print("Unable to configure photo capture settings, 'availablePreviewPhotoPixelFormatTypes' has no available options.")
return
}
let screensize = UIScreen.main.bounds.size
// setup format configuration dictionary
let previewFormat: [String : Any] = [
kCVPixelBufferPixelFormatTypeKey as String: previewPixelType,
kCVPixelBufferWidthKey as String: screensize.width,
kCVPixelBufferHeightKey as String: screensize.height
]
settings.previewPhotoFormat = previewFormat
// trigger photo capture
cameraOutput.capturePhoto(with: settings, delegate: self)
}
// MARK: Setup functions
/// Handles configuration and setup of the session, inputs, video preview layer and outputs.
/// If all are setup and configured it starts the session.
internal func setupSession() {
captureSession.sessionPreset = AVCaptureSession.Preset.high
guard setupInputs() else {
return
}
setupOutputs()
setupVideoLayer()
startSession()
}
/// Sets up capture inputs for this session.
///
/// - Returns: Returns true if inputs are successfully setup, else false.
internal func setupInputs() -> Bool {
// only need access to this functionality within this function, so declare as sub-function
func addInput(input: AVCaptureInput) {
guard captureSession.canAddInput(input) else {
return
}
captureSession.addInput(input)
}
do {
if let camera = AVCaptureDevice.default(for: AVMediaType.video) {
let input = try AVCaptureDeviceInput(device: camera)
addInput(input: input)
activeInput = input
}
// Setup Microphone
if let microphone = AVCaptureDevice.default(for: AVMediaType.audio) {
let micInput = try AVCaptureDeviceInput(device: microphone)
addInput(input: micInput)
}
return true
} catch {
print("Error setting device video input: \(error)")
return false
}
}
internal func setupOutputs() {
// only need access to this functionality within this function, so declare as sub-function
func addOutput(output: AVCaptureOutput) {
if captureSession.canAddOutput(output) {
captureSession.addOutput(output)
}
}
// file output
let fileOutput = AVCaptureMovieFileOutput()
captureSession.addOutput(fileOutput)
if let connection = fileOutput.connection(with: .video), connection.isVideoStabilizationSupported {
connection.preferredVideoStabilizationMode = .off
self.connection = connection
}
cameraOutput.isHighResolutionCaptureEnabled = true
captureSession.addOutput(cameraOutput)
}
internal func setupVideoLayer() {
let layer = AVCaptureVideoPreviewLayer(session: captureSession)
layer.videoGravity = AVLayerVideoGravity.resizeAspectFill
delegate?.videoFeedSetup(with: layer)
previewLayer = layer
}
// MARK: Helper functions
/// Creates a url in the temporary directory for file recording.
///
/// - Returns: A file url if successful, else nil.
internal func tempURL() -> URL? {
let directory = NSTemporaryDirectory() as NSString
if directory != "" {
let path = directory.appendingPathComponent(NSUUID().uuidString + ".mp4")
return URL(fileURLWithPath: path)
}
return nil
}
/// Disables smooth autofocus functionality on the active device,
/// if the active device is set and 'isSmoothAutoFocusSupported'
/// is supported for the currently set active device.
internal func disableSmoothAutoFocus() {
guard let device = activeInput?.device, device.isSmoothAutoFocusSupported else {
return
}
do {
try device.lockForConfiguration()
device.isSmoothAutoFocusEnabled = false
device.unlockForConfiguration()
} catch {
print("Error disabling smooth autofocus: \(error)")
}
}
/// Sets the current AVCaptureVideoOrientation on the currently active connection if it's supported.
internal func configureVideoOrientation() {
guard let connection = connection, connection.isVideoOrientationSupported,
let currentOrientation = AVCaptureVideoOrientation(rawValue: UIApplication.shared.statusBarOrientation.rawValue) else {
return
}
connection.videoOrientation = currentOrientation
}
}
// MARK: AVCapturePhotoCaptureDelegate
extension VideoFeed: AVCapturePhotoCaptureDelegate {
// iOS 11+ processing
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
guard error == nil, let outputData = photo.fileDataRepresentation() else {
print("Photo Error: \(String(describing: error))")
return
}
print("captured photo...")
loadImage(data: outputData)
}
// iOS < 11 processing
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photoSampleBuffer: CMSampleBuffer?, previewPhoto previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) {
if #available(iOS 11.0, *) {
// use iOS 11-only feature
// nothing to do here as iOS 11 uses the callback above
} else {
guard error == nil else {
print("Photo Error: \(String(describing: error))")
return
}
guard let sampleBuffer = photoSampleBuffer,
let previewBuffer = previewPhotoSampleBuffer,
let outputData = AVCapturePhotoOutput
.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: previewBuffer) else {
print("Image creation from sample buffer/preview buffer failed.")
return
}
print("captured photo...")
loadImage(data: outputData)
}
}
/// Creates a UIImage from Data object received from AVCapturePhotoOutput
/// delegate callback and sends to the VideoFeedDelegate for handling.
///
/// - Parameter data: Image data.
internal func loadImage(data: Data) {
guard let dataProvider = CGDataProvider(data: data as CFData), let cgImageRef: CGImage = CGImage(jpegDataProviderSource: dataProvider, decode: nil, shouldInterpolate: true, intent: .defaultIntent) else {
return
}
let image = UIImage(cgImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.right)
delegate?.processVideoSnapshot(image)
}
}
extension VideoFeed: AVCaptureFileOutputRecordingDelegate {
func fileOutput(_ output: AVCaptureFileOutput, didStartRecordingTo fileURL: URL, from connections: [AVCaptureConnection]) {
print("Video recording started: \(fileURL.absoluteString)")
}
func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
guard error == nil else {
print("Error recording movie: \(String(describing: error))")
return
}
UISaveVideoAtPathToSavedPhotosAlbum(outputFileURL.path, nil, nil, nil)
}
}
For anyone else making use of this, don't forget to add permissions to your info.plist for access to the camera, photo library and microphone.
<key>NSCameraUsageDescription</key>
<string>Let us use your camera</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>save to images</string>
<key>NSMicrophoneUsageDescription</key>
<string>for sound in video</string>

swift 3 facebook integration

trying to get Facebook connect working from a swift project.
Have been trying to follow along the following youtube video: https://www.youtube.com/watch?v=I6rTmfLp9aY
which unfortunately for me is in German.
so this is what I have so far:
I have my Facebook app with IOS enabled enabled and I planted my bundleID there.
Downloaded latest iOS framework and added to project
to the AppDelegate file I added:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FBSDKApplicationDelegate.sharedInstance()
return true
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
FBSDKAppEvents.activateApp()
}
and this is the ViewController file
import UIKit
import FBSDKCoreKit
import FBSDKLoginKit
import FBSDKShareKit
class ViewController: UIViewController, FBSDKAppInviteDialogDelegate, FBSDKLoginButtonDelegate{
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if (FBSDKAccessToken.current() != nil)
{
let content = FBSDKAppInviteContent()
content.appLinkURL = NSURL(string: "{Facebook link to app}") as URL!
FBSDKAppInviteDialog.show(from: self, with: content, delegate: self)
}
else
{
let loginView : FBSDKLoginButton = FBSDKLoginButton()
self.view.addSubview(loginView)
loginView.center = CGPoint(x: self.view.frame.midX, y: self.view.frame.midY + 100)
loginView.readPermissions = ["public_profile", "email"]
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func appInviteDialog (_ appInviteDialog: FBSDKAppInviteDialog!, didCompleteWithResults results: [AnyHashable : Any]!)
{
}
func appInviteDialog (_ appInviteDialog: FBSDKAppInviteDialog!, didFailWithError error: Error!) {
print("Error took place in appInviteDialog \(error)")
}
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
if ((error) != nil)
{
//process error
}
else if result.isCancelled {
//handle cancelation
}
else {
let content = FBSDKAppInviteContent()
content.appLinkURL = NSURL(string: "{Facebook link to app}") as URL!
FBSDKAppInviteDialog.show(from: self, with: content, delegate: self)
if result.grantedPermissions.contains("email")
{
//do work
}
}
}
func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
}
}
No errors and no alerts. When i run simulator I get an empty screen. Must be doing something right cause I get the following msg:
SystemGroup/systemgroup.com.apple.configurationprofiles
2017-06-04 00:42:02.351876+0300 facebook_login[4569:144075] [MC] Reading from private effective user settings.
also, if I just paste in viewDidLoad the following lines from the code:
let loginView : FBSDKLoginButton = FBSDKLoginButton()
self.view.addSubview(loginView)
loginView.center = CGPoint(x: self.view.frame.midX, y: self.view.frame.midY + 100)
loginView.readPermissions = ["public_profile", "email"]
I get a beautiful Facebook button in simulator that of course crashes when i press it.
any help to work will be greatly appreciated
Facebook has a Swift SDK you might find easier to use than the Objective-C one (which they just call iOS). Try looking around the documentation here:
https://developers.facebook.com/docs/swift
Also, follow the steps described in the (other) iOS SDK to get started:
https://developers.facebook.com/docs/ios/getting-started/
This is the minimal app delegate I could get to work (notice the Swift SDK is missing the FB prefixes that exist in the iOS SDK):
import UIKit
import FacebookCore
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
return true
}
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
return SDKApplicationDelegate.shared.application(app, open: url, options: options)
}
}
And be sure to add all the required keys in your Info.plist or else you won't get authentication to work at all.
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith
result:FBSDKLoginManagerLoginResult!, error: Error!) {
if ((error) != nil) {
// Process error
print("Error! : \(error.localizedDescription)")
return
} else if result.isCancelled {
// Handle cancellations
print("Success! : user cancel login request")
return
} else {
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields" : "id, email, name,picture.type(large)"])
graphRequest.start(completionHandler: { (connection, result, error) -> Void in
if ((error) != nil) {
print("Error: \(error)")
} else {
// Do work in app.
let dataDict:NSDictionary = result as! NSDictionary
if let token = FBSDKAccessToken.current().tokenString {
print("tocken: \(token)")
let userDefult = UserDefaults.standard
userDefult.setValue(token, forKey: "access_tocken")
userDefult.synchronize()
}
if let user : NSString = dataDict.object(forKey: "name") as! NSString? {
print("user: \(user)")
}
if let id : NSString = dataDict.object(forKey: "id") as? NSString {
print("id: \(id)")
}
if let email : NSString = (result! as AnyObject).value(forKey: "email") as? NSString {
print("email: \(email)")
}
if let pictureData:NSDictionary = dataDict.object(forKey: "picture") as? NSDictionary{
if let data:NSDictionary = pictureData.object(forKey: "data") as? NSDictionary{
if let strPictureURL: String = data.object(forKey: "url") as? String{
self.imageviewUser.image = UIImage(data: NSData(contentsOf: NSURL(string: strPictureURL)! as URL)! as Data)
}
}
}
}
})
}
}
func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!)
{
FBSDKAccessToken.setCurrent(nil)
FBSDKProfile.setCurrent(nil)
let manager = FBSDKLoginManager()
manager.logOut()
}

Delivering WCSession messages using NSNotifications on Apple Watch

I have NSNotifications using Kugel that are working great on the watch simulator and both the iPhone and iPhone simulator to deliver messages to update the UI/state but these are failing to deliver on the watch side when testing on the devices.
The issue I believe is that the NSNotifications are triggered based on a WCSession message from the iPhone. Everything works fine on the simulator and iPhone side possibly because the connection and notifications are always delivered since the sim keep the watch app active all the time and the iPhone has full session support. On the watch there is the potential for failure of both the session and possibly the notification based on the state of the watch.
Debugging on the watch is painfully slow. It's taking 5-10 minutes just to start the debug process!
Can someone point me to some reading on how best to ensure a phone message is received on the watch and the watch app informed of the need to update based on a message? Or maybe some good debugging code that can log WCSession and NSNotification information that I can review later?
My code is fairly straightforward but still a work in progress ....
On both sides I create a singleton to manage the session, here is the phone side code:
import WatchConnectivity
import UIKit
// This class manages messaging between the Watch and iPhone
class PhoneSession: NSObject, WCSessionDelegate
{
static let manager = PhoneSession()
private var appDelegate: AppDelegate!
private let session: WCSession? = WCSession.isSupported() ? WCSession.defaultSession() : nil
private var validSession: WCSession?
{
if let session = session where session.reachable
{
return session
}
return nil
}
func startSession()
{
session?.delegate = self
session?.activateSession()
appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
}
func isEditing() -> Bool
{
if (UIApplication.sharedApplication().applicationState == .Active)
{
if (appDelegate.mainView.visible && appDelegate.mainView.currentDay.isToday())
{
return false
}
return true
}
return false
}
}
extension PhoneSession
{
func sendEditing()
{
if session!.reachable
{
sendMessage([Keys.UpdateType : PhoneUpdateType.Editing.rawValue])
}
}
func sendDoneEditing()
{
if session!.reachable
{
sendMessage([Keys.UpdateType : PhoneUpdateType.DoneEdit.rawValue])
}
}
func sendTable()
{
let tableInfo: WatchWorkout = PhoneData().buildWatchTableData()
let archivedTable: NSData = NSKeyedArchiver.archivedDataWithRootObject(tableInfo)
if session!.reachable
{
sendMessage([Keys.UpdateType : PhoneUpdateType.TableInfo.rawValue, Keys.Workout: archivedTable])
}
else
{
do
{
try updateApplicationContext([Keys.UpdateType : PhoneUpdateType.TableInfo.rawValue, Keys.Workout: archivedTable])
}
catch
{
print("error sending info: \(error)")
}
}
}
func sendRowDone(row: Int, done: Bool)
{
if session!.reachable
{
sendMessage([Keys.UpdateType : PhoneUpdateType.RowDone.rawValue,
Keys.RowIndex: row, Keys.Done: done])
}
else
{
let tableInfo: WatchWorkout = PhoneData().buildWatchTableData()
let archivedTable: NSData = NSKeyedArchiver.archivedDataWithRootObject(tableInfo)
do
{
try updateApplicationContext( [Keys.UpdateType : PhoneUpdateType.TableInfo.rawValue,
Keys.Workout: archivedTable])
}
catch
{
print("error sending info: \(error)")
}
}
}
func receivedRowDone(info: [String : AnyObject])
{
let row: Int = info[Keys.Row] as! Int
let done: Bool = info[Keys.Done] as! Bool
PhoneData.manager.updateInfoFromWatch(row, done: done)
}
func receivedRowInfo(info: [String : AnyObject])
{
let row: Int = info[Keys.Row] as! Int
let rest: Int = info[Keys.Rest] as! Int
let reps: Int = info[Keys.Reps] as! Int
let force: Double = info[Keys.Force] as! Double
PhoneData.manager.updateSetInfoFromWatch(row, rest: rest, reps: reps, force: force)
}
func receivedTableDone(info: [String : AnyObject])
{
let date: Int = info[Keys.Date] as! Int
let dones: [Bool] = info[Keys.TableDones] as! [Bool]
PhoneData.manager.updateDones(dones, forDate: date)
Kugel.publish(PhoneNotificationKeys.ReloadTable)
}
func receivedTableComplete()
{
Kugel.publish(PhoneNotificationKeys.ReloadTable)
}
func receivedStartRest()
{
Kugel.publish(PhoneNotificationKeys.StartRest)
}
func receivedInfo(info: [String : AnyObject]) -> NSData?
{
let messageString: String = info[Keys.UpdateType] as! String
let updateType: WatchUpdateType = WatchUpdateType.getType(messageString)
switch (updateType)
{
case .RowInfo:
receivedRowInfo(info)
case .TableDone:
receivedTableDone(info)
case .RowDone:
receivedRowDone(info)
case .TableComplete:
receivedTableComplete()
case .StartRest:
receivedStartRest()
case .RequestUpdate:
let tableInfo: WatchWorkout = PhoneData().buildWatchTableData()
let archivedTable: NSData = NSKeyedArchiver.archivedDataWithRootObject(tableInfo)
return archivedTable
case .Ignore:
print("Opps")
}
return nil
}
}
// MARK: Interactive Messaging
extension PhoneSession
{
// Sender
func sendMessage(message: [String : AnyObject], replyHandler: (([String : AnyObject]) -> Void)? = nil, errorHandler: ((NSError) -> Void)? = nil)
{
validSession!.sendMessage(message,
replyHandler:
{
(returnMessage: [String : AnyObject]) -> Void in
if let theMessage = returnMessage[Keys.MessageStatus]
{
print("Return Message from Watch: \(theMessage)")
}
},
errorHandler:
{
(error) -> Void in
print("Error Message during transfer to Watch: \(error)")
}
)
}
func session(session: WCSession, didReceiveMessage message: [String : AnyObject])
{
self.receivedInfo(message)
}
// Receiver
func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void)
{
let returnMessage = self.receivedInfo(message)
if (returnMessage != nil)
{
if let archivedTable: NSData = returnMessage!
{
let replyValues = [Keys.UpdateType : PhoneUpdateType.TableInfo.rawValue, Keys.Workout: archivedTable] // Data to be returned
replyHandler(replyValues)
}
}
}
}
// MARK: Application Context
// use when your app needs only the latest information, if the data was not sent, it will be replaced
extension PhoneSession
{
// Sender
func updateApplicationContext(applicationContext: [String : AnyObject]) throws
{
if ((session) != nil)
{
do
{
try session!.updateApplicationContext(applicationContext)
}
catch let error
{
throw error
}
}
}
// Receiver
func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject])
{
self.receivedInfo(applicationContext)
}
}
and this is the watch side:
import WatchConnectivity
class WatchSession: NSObject, WCSessionDelegate
{
static let manager = WatchSession()
private let session: WCSession? = WCSession.isSupported() ? WCSession.defaultSession() : nil
private var validSession: WCSession?
{
if let session = session where session.reachable
{
return session
}
return nil
}
func startSession()
{
session?.delegate = self
session?.activateSession()
}
}
extension WatchSession
{
func sendRowInfo(row:Int, rest: Int, reps: Int, force: Double)
{
if session!.reachable
{
let message: [String: AnyObject] = [Keys.UpdateType : WatchUpdateType.RowInfo.rawValue,
Keys.Row : row,
Keys.Rest : rest,
Keys.Reps : reps,
Keys.Force : force]
sendMessage(message)
print("sent row done to Phone: \(message)")
}
else
{
sendTableDone()
print("failed to connect to Phone, sent table done context to Phone")
}
}
func sendRowDone(row:Int, done: Bool)
{
if session!.reachable
{
let message: [String: AnyObject] = [Keys.UpdateType : WatchUpdateType.RowDone.rawValue,
Keys.Row : row,
Keys.Done : done]
sendMessage(message)
print("sent row done to Phone: \(message)")
}
else
{
sendTableDone()
print("failed to connect to Phone, sent table done context to Phone")
}
}
func sendTableDone()
{
let tableDones: [Bool] = WatchData.manager.watchTableDone()
let date: Int = WatchData.manager.date()
do
{
try updateApplicationContext( [Keys.UpdateType : WatchUpdateType.TableDone.rawValue,
Keys.Date : date, Keys.TableDones: tableDones])
}
catch _
{
print("error trying to send TableDones")
}
}
func sendTableComplete()
{
if session!.reachable
{
sendMessage([Keys.UpdateType : WatchUpdateType.TableComplete.rawValue])
}
else
{
let date: Int = WatchData.manager.date()
do
{
try updateApplicationContext( [Keys.UpdateType : WatchUpdateType.TableComplete.rawValue,
Keys.Date : date])
}
catch _
{
print("error trying to send TableComplete")
}
}
}
func sendRest() -> Bool
{
var sent: Bool = false
if session!.reachable
{
sendMessage([Keys.UpdateType : WatchUpdateType.StartRest.rawValue])
sent = true
}
return sent
}
func requestUpdate() -> Bool
{
var sent: Bool = false
if session!.reachable
{
print("requesting update reply")
sendMessage([Keys.UpdateType : WatchUpdateType.RequestUpdate.rawValue])
sent = true
}
return sent
}
func receivedUpdateReply(info: [String : AnyObject])
{
}
func receiveRowDone(info: [String : AnyObject])
{
let row: Int = info[Keys.RowIndex] as! Int
let done: Bool = info[Keys.Done] as! Bool
WatchData.manager.updateWatchTable(row, done: done)
Kugel.publish(WatchNotificationKeys.UpdateRow)
}
func receivedTable(archivedTable: NSData)
{
let workout: WatchWorkout = NSKeyedUnarchiver.unarchiveObjectWithData(archivedTable) as! WatchWorkout
WatchData.manager.updateWatchWorkout(workout)
Kugel.publish(WatchNotificationKeys.ReloadTable)
}
func receivedStartEditStatus()
{
Kugel.publish(WatchNotificationKeys.StartEdit)
}
func receivedDoneEditStatus()
{
WatchData.manager.retrieveWorkout()
Kugel.publish(WatchNotificationKeys.DoneEdit)
}
func receivedStopRest()
{
Kugel.publish(WatchNotificationKeys.StopRest)
}
func receivedInfo(info: [String : AnyObject])
{
let messageString: String = info[Keys.UpdateType] as! String
let updateType: PhoneUpdateType = PhoneUpdateType.getType(messageString)
switch (updateType)
{
case .TableInfo:
receivedTable(info[Keys.Workout] as! NSData)
case .Editing:
receivedStartEditStatus()
case .DoneEdit:
receivedDoneEditStatus()
case .RowDone:
receiveRowDone(info)
case .StopRest:
receivedStopRest()
case .Ignore:
print("Opps")
}
}
func receivedReply(info: [String : AnyObject])
{
if let replyString: String = info[Keys.ReplyType] as? String
{
let replyType: ReplyType = ReplyType.getType(replyString)
switch (replyType)
{
case .Table:
print("received Reply Table")
receivedTable(info[Keys.Workout] as! NSData)
case .NoData:
print("Opps ... nodata in reply")
case .Ignore:
print("Opps replyType message error")
}
}
}
}
// MARK: Interactive Messaging
extension WatchSession
{
// Sender
func sendMessage(message: [String : AnyObject], replyHandler: (([String : AnyObject]) -> Void)? = nil, errorHandler: ((NSError) -> Void)? = nil)
{
validSession!.sendMessage(message,
replyHandler:
{
(replyMessage: [String : AnyObject]) -> Void in
if let typeMessage: String = replyMessage[Keys.ReplyType] as? String
{
self.receivedReply(replyMessage)
print("Return Message from Phone: \(typeMessage)")
}
},
errorHandler:
{
(error) -> Void in
print("Error Message during transfer to Phone: \(error)")
}
)
}
// Receiver
func session(session: WCSession, didReceiveMessage message: [String : AnyObject])
{
self.receivedInfo(message)
}
func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void)
{
self.receivedInfo(message)
let replyValues = [Keys.MessageStatus : "Watch received message"] // Data to be returned
replyHandler(replyValues)
}
}
// MARK: Application Context
extension WatchSession
{
// Sender
func updateApplicationContext(applicationContext: [String : AnyObject]) throws
{
if let session = validSession
{
do
{
try session.updateApplicationContext(applicationContext)
}
catch let error
{
throw error
}
}
}
// Receiver
func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject])
{
// handle receiving application context
receivedInfo(applicationContext)
}
}
I create the singleton in my AppDelegate on the iPhone side and the ExtensionDelegate on the watch side, here is the phone side:
var phoneSession: PhoneSession!
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
self.phoneSession = PhoneSession()
PhoneSession.manager.startSession()
The basic logic when sending a message is it looks if the other side is reachable, if it is sendMessage is used, if it is not reachable then sendApplicationContext is used.
When the message is received on the phone side there is some extra logic to see if the app is in the foreground or background, if in foreground it will push a notification onto the main thread, if in background just the info is updated. On the watch side it always pushes onto the main thread since my understanding is messages will not be received in the background.

Swift Ensembles Set Up & ubiquityContainerIdentifier

The book states,
“An ensemble identifier is used to match stores across devices. It is
important that this be the same for each store in the ensemble.”
let ensembleFileSystem = CDEICloudFileSystem(ubiquityContainerIdentifier: "???")
Does this need to be unique for all users ? or just for my application?
If anyone has a Swift version of how the set up Ensembles that would be great.
What I have so far, is this all that is needed?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let ensembleFileSystem = CDEICloudFileSystem(ubiquityContainerIdentifier: "???")
let modelURL = NSBundle.mainBundle().URLForResource("DataModel", withExtension: "momd")!
let url = applicationDocumentsDirectory.URLByAppendingPathComponent("SingleViewCoreData.sqlite")
let ensemble = CDEPersistentStoreEnsemble(ensembleIdentifier: "mainstore", persistentStoreURL: url, managedObjectModelURL: modelURL, cloudFileSystem: ensembleFileSystem!)
if !ensemble.leeched {
ensemble.leechPersistentStoreWithCompletion { (error) -> Void in
if error != nil {
print("cannot leech")
print(error!.localizedDescription)
}
}
}
NSNotificationCenter.defaultCenter().addObserver(self, selector: "syncWithCompletion:", name: CDEMonitoredManagedObjectContextDidSaveNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "syncWithCompletion:", name: CDEICloudFileSystemDidDownloadFilesNotification, object: nil)
return true
}
func syncWithCompletion(notification:NSNotification) {
print("synced \(notification)")
managedObjectContext.mergeChangesFromContextDidSaveNotification(notification)
}
Something is missing Im getting this error log
User is not logged into iCloud
Despite being logged in as evident
print(NSFileManager.defaultManager().ubiquityIdentityToken)
Not being nil
Got it to work in the end - found example apps in 1.0 Git
I belive I was leeching to fast - not giving enough time for the set up process to complete.
Support this framework - buy ensembles 2, if you like ver 1.
Update .. easier way
I just use the normal core data stack apple provides.
Here is the extras to get ensembles working.
var ensemble:CDEPersistentStoreEnsemble!
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let file = CDEICloudFileSystem(ubiquityContainerIdentifier: nil)
let modelURL = NSBundle.mainBundle().URLForResource("DataModel", withExtension: "momd")!
let storeurl = self.applicationDocumentsDirectory.URLByAppendingPathComponent("store.sqlite")
ensemble = CDEPersistentStoreEnsemble(ensembleIdentifier: "MyStoreName", persistentStoreURL: storeurl, managedObjectModelURL: modelURL, cloudFileSystem: file)
ensemble.delegate = self
NSNotificationCenter.defaultCenter().addObserver(self, selector: "localSaveOccurred:", name: CDEMonitoredManagedObjectContextDidSaveNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "cloudDataDidDownload:", name: CDEICloudFileSystemDidDownloadFilesNotification, object: nil)
syncWithCompletion { completed in
if completed {
print("SUCCESSS")
}
else {
print("FAIL")
}
}
return true
}
// MARK: - Sync
func applicationDidEnterBackground(application: UIApplication) {
print("Did Enter Background Save from App Delegate")
let identifier = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler(nil)
saveContext()
syncWithCompletion { (completed) -> Void in
if completed {
UIApplication.sharedApplication().endBackgroundTask(identifier)
}
}
}
func applicationWillEnterForeground(application: UIApplication) {
syncWithCompletion { (completed) -> Void in
}
}
func localSaveOccurred(note:NSNotification) {
syncWithCompletion { (completed) -> Void in
}
}
func cloudDataDidDownload(note:NSNotification) {
syncWithCompletion { (completed) -> Void in
print("items from iCloud arrived")
}
}
func syncWithCompletion(completion:(completed:Bool) -> Void) {
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
if !ensemble.leeched {
ensemble.leechPersistentStoreWithCompletion(nil)
}
else {
ensemble.mergeWithCompletion{ error in
if error != nil {
print("cannot merge \(error!.localizedDescription)")
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
completion(completed: false)
}
else {
print("merged")
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
completion(completed: true)
}
}
}
}
// MARK: - Ensemble Delegate Methods
func persistentStoreEnsemble(ensemble: CDEPersistentStoreEnsemble!, didSaveMergeChangesWithNotification notification: NSNotification!) {
managedObjectContext.performBlockAndWait { () -> Void in
self.managedObjectContext.mergeChangesFromContextDidSaveNotification(notification)
}
}
func persistentStoreEnsemble(ensemble: CDEPersistentStoreEnsemble!, globalIdentifiersForManagedObjects objects: [AnyObject]!) -> [AnyObject]! {
return (objects as NSArray).valueForKeyPath("uniqueIdentifier") as! [AnyObject]
}
My First Way
Here it is in Swift, with a few extras
var ensemble:CDEPersistentStoreEnsemble!
var cloudFileSystem:CDEICloudFileSystem!
var managedObjectContext: NSManagedObjectContext!
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
setUpCoreData()
let modelURL = NSBundle.mainBundle().URLForResource("YourDataModel", withExtension: "momd")!
cloudFileSystem = CDEICloudFileSystem(ubiquityContainerIdentifier:"USE_YOUR_APPS_REVERSE DOMAIN NAME HERE")
From the developer: RE ubiquityContainerIdentifier
This is not part of Ensembles per se. It is from iCloud. Every app
using iCloud has to have a ubiquity container id. You can find it in
your app settings when you enable iCloud. It is unique per app, and we
only use it if you are choosing for iCloud (eg not Dropbox).
ensemble = CDEPersistentStoreEnsemble(ensembleIdentifier: "store", persistentStoreURL: storeURL(), managedObjectModelURL: modelURL, cloudFileSystem: cloudFileSystem!)
ensemble.delegate = self
NSNotificationCenter.defaultCenter().addObserver(self, selector: "localSaveOccurred:", name: CDEMonitoredManagedObjectContextDidSaveNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "cloudDataDidDownload:", name: CDEICloudFileSystemDidDownloadFilesNotification, object: nil)
syncWithCompletion { completed in
if completed {
print("SUCCESSS")
}
else {
print("FAIL")
}
}
return true
}
// MARK: - Core Data Stack
func setUpCoreData() {
let modelURL = NSBundle.mainBundle().URLForResource("DataModel", withExtension: "momd")!
guard let model = NSManagedObjectModel(contentsOfURL: modelURL) else { fatalError("cannot use model") }
do {
try NSFileManager.defaultManager().createDirectoryAtURL(storeDirectoryURL(), withIntermediateDirectories: true, attributes: nil)
}
catch {
fatalError("cannot create dir")
}
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: model)
//NSDictionary *options = #{NSMigratePersistentStoresAutomaticallyOption: #YES, NSInferMappingModelAutomaticallyOption: #YES};
let failureReason = "There was an error creating or loading the application's saved data."
do {
try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeURL(), options: nil)
managedObjectContext = NSManagedObjectContext.init(concurrencyType: .MainQueueConcurrencyType)
managedObjectContext.persistentStoreCoordinator = coordinator
managedObjectContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
} catch {
// Report any error we got.
var dict = [String: AnyObject]()
dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
dict[NSLocalizedFailureReasonErrorKey] = failureReason
dict[NSUnderlyingErrorKey] = error as NSError
let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
// Replace this with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog("Unresolved error \(wrappedError), \(wrappedError.userInfo)")
abort()
}
}
func storeDirectoryURL() -> NSURL {
let directoryURL = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true)
return directoryURL
}
func storeURL() -> NSURL {
let url = storeDirectoryURL().URLByAppendingPathComponent("store.sqlite")
return url
}
// MARK: - Sync
func applicationDidEnterBackground(application: UIApplication) {
print("Did Enter Background Save from App Delegate")
let identifier = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler(nil)
saveContext()
syncWithCompletion { (completed) -> Void in
if completed {
UIApplication.sharedApplication().endBackgroundTask(identifier)
}
}
}
func applicationWillEnterForeground(application: UIApplication) {
syncWithCompletion { (completed) -> Void in
}
}
func localSaveOccurred(note:NSNotification) {
syncWithCompletion { (completed) -> Void in
}
}
func cloudDataDidDownload(note:NSNotification) {
syncWithCompletion { (completed) -> Void in
}
}
func syncWithCompletion(completion:(completed:Bool) -> Void) {
if !ensemble.leeched {
ensemble.leechPersistentStoreWithCompletion { error in
if error != nil {
print("cannot leech \(error!.localizedDescription)")
completion(completed: false)
}
else {
print("leached!!")
completion(completed: true)
}
}
}
else {
ensemble.mergeWithCompletion{ error in
if error != nil {
print("cannot merge \(error!.localizedDescription)")
completion(completed: false)
}
else {
print("merged!!")
completion(completed: true)
}
}
}
}
// MARK: - Ensemble Delegate Methods
func persistentStoreEnsemble(ensemble: CDEPersistentStoreEnsemble!, didSaveMergeChangesWithNotification notification: NSNotification!) {
print("did merge changes with note")
managedObjectContext.mergeChangesFromContextDidSaveNotification(notification)
}
func persistentStoreEnsemble(ensemble: CDEPersistentStoreEnsemble!, globalIdentifiersForManagedObjects objects: [AnyObject]!) -> [AnyObject]! {
return (objects as NSArray).valueForKeyPath("uniqueIdentifier") as! [AnyObject]
}