@@ -19,46 +19,42 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1919 // Override point for customization after application launch.
2020
2121 let versionNumber : AnyObject ? = NSBundle . mainBundle ( ) . infoDictionary ? [ " CFBundleVersion " ]
22- println ( " version \( versionNumber!) " )
22+ print ( " version \( versionNumber!) " )
2323
24- var item = NotificationData ( )
24+ let item = NotificationData ( )
2525 item. alert = " Swift Push ( \( versionNumber!) ) starting on " + UIDevice. currentDevice ( ) . name
2626 notifications. insert ( item, atIndex: 0 )
2727
2828 switch ( application. applicationState) {
2929 case UIApplicationState . Active:
30- println ( " didFinishLaunchingWithOptions - active " )
30+ print ( " didFinishLaunchingWithOptions - active " )
3131 case UIApplicationState . Inactive:
32- println ( " didFinishLaunchingWithOptions - inactive " )
32+ print ( " didFinishLaunchingWithOptions - inactive " )
3333 case UIApplicationState . Background:
34- println ( " didFinishLaunchingWithOptions - background " )
35- default :
36- println ( " didFinishLaunchingWithOptions - unknown application state " )
34+ print ( " didFinishLaunchingWithOptions - background " )
3735 }
3836
39- var types : UIUserNotificationType =
40- UIUserNotificationType . Alert |
41- UIUserNotificationType . Badge |
42- UIUserNotificationType . Sound
37+ let types : UIUserNotificationType =
38+ [ UIUserNotificationType . Alert, UIUserNotificationType . Badge, UIUserNotificationType . Sound]
4339
44- var settings : UIUserNotificationSettings = UIUserNotificationSettings ( forTypes: types, categories: nil )
40+ let settings : UIUserNotificationSettings = UIUserNotificationSettings ( forTypes: types, categories: nil )
4541
4642 application. registerUserNotificationSettings ( settings )
4743 application. registerForRemoteNotifications ( )
4844
4945 // screenshot notification
5046 let mainQueue = NSOperationQueue . mainQueue ( )
5147 NSNotificationCenter . defaultCenter ( ) . addObserverForName ( UIApplicationUserDidTakeScreenshotNotification, object: nil , queue: mainQueue) { notification in
52- println ( " screenshot taken " )
48+ print ( " screenshot taken " )
5349 }
5450 return true
5551 }
5652
5753
5854 func application( application: UIApplication , didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData ) {
59- let existingToken : AnyObject ? = NSUserDefaults . standardUserDefaults ( ) . objectForKey ( " deviceToken " )
55+ // let existingToken: AnyObject? = NSUserDefaults.standardUserDefaults().objectForKey("deviceToken")
6056
61- println ( " device token is " + deviceToken. description)
57+ print ( " device token is " + deviceToken. description)
6258 NSUserDefaults . standardUserDefaults ( ) . setObject ( deviceToken. description as String , forKey: " deviceToken " )
6359 NSUserDefaults . standardUserDefaults ( ) . synchronize ( )
6460
@@ -67,52 +63,52 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
6763 let versionNumber : AnyObject ? = NSBundle . mainBundle ( ) . infoDictionary ? [ " CFBundleVersion " ]
6864
6965 // register device token with push service
70- var request = NSMutableURLRequest ( URL: NSURL ( string: " http://www.trease.eu/ibeacon/swiftpush/ " ) !)
71- var session = NSURLSession . sharedSession ( )
66+ let request = NSMutableURLRequest ( URL: NSURL ( string: " http://www.trease.eu/ibeacon/swiftpush/ " ) !)
67+ // var session = NSURLSession.sharedSession()
7268 request. HTTPMethod = " POST "
7369 var bodyData = " token= \( deviceToken. description) "
7470 bodyData += " &device= \( UIDevice . currentDevice ( ) . name) "
7571 bodyData += " &mode= \( mode!) "
7672 bodyData += " &version= \( versionNumber!) "
7773 request. HTTPBody = bodyData. dataUsingEncoding ( NSUTF8StringEncoding)
78- var connection = NSURLConnection ( request: request, delegate: self , startImmediately: false )
74+ let connection = NSURLConnection ( request: request, delegate: self , startImmediately: false )
7975 connection? . start ( )
8076 }
8177
8278
8379 func application( application: UIApplication , didFailToRegisterForRemoteNotificationsWithError error: NSError ) {
84- println ( " Failed to register device token " )
85- println ( error. localizedDescription )
80+ print ( " Failed to register device token " )
81+ print ( error. localizedDescription )
8682 }
8783
8884 func application( application: UIApplication , didReceiveRemoteNotification userInfo: [ NSObject : AnyObject ] , fetchCompletionHandler completionHandler: ( UIBackgroundFetchResult ) -> Void ) {
8985 switch ( application. applicationState) {
9086 case UIApplicationState . Active:
91- println ( " notification received by AppDeligate whilst active " )
87+ print ( " notification received by AppDeligate whilst active " )
9288 case UIApplicationState . Inactive:
93- println ( " notification received by AppDeligate whilst inactive " )
89+ print ( " notification received by AppDeligate whilst inactive " )
9490 case UIApplicationState . Background:
95- println ( " notification received by AppDeligate whilst in background " )
91+ print ( " notification received by AppDeligate whilst in background " )
9692 default :
97- println ( " notification received by AppDeligate with unknown application state " )
93+ print ( " notification received by AppDeligate with unknown application state " )
9894 }
99- println ( userInfo)
95+ print ( userInfo)
10096
101- var item = NotificationData ( )
97+ let item = NotificationData ( )
10298
10399 if let aps = userInfo [ " aps " ] as? NSDictionary {
104100 if let alert = aps [ " alert " ] as? String {
105101 item. alert = alert
106- println ( " alert: \( alert) " )
102+ print ( " alert: \( alert) " )
107103 }
108104 }
109105 if let messageID = userInfo [ " messageID " ] as? Int {
110106 item. messageID = messageID
111- println ( " messageID: \( messageID) " )
107+ print ( " messageID: \( messageID) " )
112108 }
113109 if let timeStamp = userInfo [ " timestamp " ] as? NSTimeInterval {
114110 item. timeStampSent = NSDate ( timeIntervalSince1970: timeStamp)
115- println ( " timestamp: \( timeStamp) " )
111+ print ( " timestamp: \( timeStamp) " )
116112 }
117113
118114 notifications. insert ( item, atIndex: 0 )
@@ -146,16 +142,14 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
146142
147143 func applicationWillTerminate( application: UIApplication ) {
148144 // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
149- // Saves changes in the application's managed object context before the application terminates.
150- self . saveContext ( )
151145 }
152146
153147 // MARK: - Core Data stack
154148
155149 lazy var applicationDocumentsDirectory : NSURL = {
156150 // The directory the application uses to store the Core Data store file. This code uses a directory named "Trease.Swift_Push_3" in the application's documents Application Support directory.
157151 let urls = NSFileManager . defaultManager ( ) . URLsForDirectory ( . DocumentDirectory, inDomains: . UserDomainMask)
158- return urls [ urls. count- 1 ] as! NSURL
152+ return urls [ urls. count- 1 ] as NSURL
159153 } ( )
160154
161155 lazy var managedObjectModel : NSManagedObjectModel = {
@@ -171,7 +165,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
171165 let url = self . applicationDocumentsDirectory. URLByAppendingPathComponent ( " Swift_Push_3.sqlite " )
172166 var error : NSError ? = nil
173167 var failureReason = " There was an error creating or loading the application's saved data. "
174- if coordinator!. addPersistentStoreWithType ( NSSQLiteStoreType, configuration: nil , URL: url, options: nil , error: & error) == nil {
168+ do {
169+ try coordinator!. addPersistentStoreWithType ( NSSQLiteStoreType, configuration: nil , URL: url, options: nil )
170+ } catch var error1 as NSError {
171+ error = error1
175172 coordinator = nil
176173 // Report any error we got.
177174 let dict = NSMutableDictionary ( )
@@ -183,6 +180,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
183180 // 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.
184181 NSLog ( " Unresolved error \( error) , \( error!. userInfo) " )
185182 abort ( )
183+ } catch {
184+ fatalError ( )
186185 }
187186
188187 return coordinator
@@ -199,19 +198,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
199198 return managedObjectContext
200199 } ( )
201200
202- // MARK: - Core Data Saving support
203-
204- func saveContext ( ) {
205- if let moc = self . managedObjectContext {
206- var error : NSError ? = nil
207- if moc. hasChanges && !moc. save ( & error) {
208- // Replace this implementation with code to handle the error appropriately.
209- // 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.
210- NSLog ( " Unresolved error \( error) , \( error!. userInfo) " )
211- abort ( )
212- }
213- }
214- }
215201
216202}
217203
0 commit comments