-
Notifications
You must be signed in to change notification settings - Fork 27
/
AppDelegate.swift
178 lines (147 loc) · 8.62 KB
/
AppDelegate.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import UIKit
import Parse
import Atlas
import SVProgressHUD
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var layerClient: LYRClient!
var controller: ViewController!
// MARK TODO: Before first launch, update LayerAppIDString, ParseAppIDString or ParseClientKeyString values
// TODO:If LayerAppIDString, ParseAppIDString or ParseClientKeyString are not set, this app will crash"
let LayerAppIDString: NSURL! = NSURL(string: "layer:///apps/staging/5e594efe-ded1-11e4-8ee4-e3d900000e81")
let ParseAppIDString: String = "9us9WVAOeboE3QSgRuXPwe0enLGgnaBGxoqNsL6R"
let ParseClientKeyString: String = "pc0AD9flDK8XTlFuktkBmNQuCE0L3xr90i48vCtv"
//Please note, You must set `LYRConversation *conversation` as a property of the ViewController.
var conversation: LYRConversation!
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
setupParse()
setupLayer()
// Show View Controller
controller = ViewController()
controller.layerClient = layerClient
// Register for push
self.registerApplicationForPushNotifications(application)
self.window!.rootViewController = UINavigationController(rootViewController: controller)
self.window!.backgroundColor = UIColor.whiteColor()
self.window!.makeKeyAndVisible()
return true
}
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.
}
func applicationDidEnterBackground(application: UIApplication) {
// 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.
}
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
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.
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
// MARK:- Push Notification Registration
func registerApplicationForPushNotifications(application: UIApplication) {
// Set up push notifications
// For more information about Push, check out:
// https://developer.layer.com/docs/guides/ios#push-notification
// Register device for iOS8
let notificationSettings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound], categories: nil)
application.registerUserNotificationSettings(notificationSettings)
application.registerForRemoteNotifications()
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
// Store the deviceToken in the current installation and save it to Parse.
let currentInstallation: PFInstallation = PFInstallation.currentInstallation()
currentInstallation.setDeviceTokenFromData(deviceToken)
currentInstallation.saveInBackground()
// Send device token to Layer so Layer can send pushes to this device.
// For more information about Push, check out:
// https://developer.layer.com/docs/ios/guides#push-notification
assert(self.layerClient != nil, "The Layer client has not been initialized!")
do {
try self.layerClient.updateRemoteNotificationDeviceToken(deviceToken)
print("Application did register for remote notifications: \(deviceToken)")
} catch let error as NSError {
print("Failed updating device token with error: \(error)")
}
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
if userInfo["layer"] == nil {
PFPush.handlePush(userInfo)
completionHandler(UIBackgroundFetchResult.NewData)
return
}
let userTappedRemoteNotification: Bool = application.applicationState == UIApplicationState.Inactive
var conversation: LYRConversation? = nil
if userTappedRemoteNotification {
SVProgressHUD.show()
conversation = self.conversationFromRemoteNotification(userInfo)
if conversation != nil {
self.navigateToViewForConversation(conversation!)
}
}
let success: Bool = self.layerClient.synchronizeWithRemoteNotification(userInfo, completion: { (changes, error) in
completionHandler(self.getBackgroundFetchResult(changes, error: error))
if userTappedRemoteNotification && conversation == nil {
// Try navigating once the synchronization completed
self.navigateToViewForConversation(self.conversationFromRemoteNotification(userInfo))
}
})
if !success {
// This should not happen?
completionHandler(UIBackgroundFetchResult.NoData)
}
}
func getBackgroundFetchResult(changes: [AnyObject]!, error: NSError!) -> UIBackgroundFetchResult {
if changes?.count > 0 {
return UIBackgroundFetchResult.NewData
}
return error != nil ? UIBackgroundFetchResult.Failed : UIBackgroundFetchResult.NoData
}
func conversationFromRemoteNotification(remoteNotification: [NSObject : AnyObject]) -> LYRConversation {
let layerMap = remoteNotification["layer"] as! [String: String]
let conversationIdentifier = NSURL(string: layerMap["conversation_identifier"]!)
return self.existingConversationForIdentifier(conversationIdentifier!)!
}
func navigateToViewForConversation(conversation: LYRConversation) {
if self.controller.conversationListViewController != nil {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(2 * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), {
SVProgressHUD.dismiss()
if (self.controller.navigationController!.topViewController as? ConversationViewController)?.conversation != conversation {
self.controller.conversationListViewController.presentConversation(conversation)
}
});
} else {
SVProgressHUD.dismiss()
}
}
func existingConversationForIdentifier(identifier: NSURL) -> LYRConversation? {
let query: LYRQuery = LYRQuery(queryableClass: LYRConversation.self)
query.predicate = LYRPredicate(property: "identifier", predicateOperator: LYRPredicateOperator.IsEqualTo, value: identifier)
query.limit = 1
do {
return try self.layerClient.executeQuery(query).firstObject as? LYRConversation
} catch {
// This should never happen?
return nil
}
}
func setupParse() {
// Enable Parse local data store for user persistence
Parse.enableLocalDatastore()
Parse.setApplicationId(ParseAppIDString, clientKey: ParseClientKeyString)
// Set default ACLs
let defaultACL: PFACL = PFACL()
defaultACL.setPublicReadAccess(true)
PFACL.setDefaultACL(defaultACL, withAccessForCurrentUser: true)
}
func setupLayer() {
layerClient = LYRClient(appID: LayerAppIDString)
layerClient.autodownloadMIMETypes = NSSet(objects: ATLMIMETypeImagePNG, ATLMIMETypeImageJPEG, ATLMIMETypeImageJPEGPreview, ATLMIMETypeImageGIF, ATLMIMETypeImageGIFPreview, ATLMIMETypeLocation) as? Set<String>
}
}