forked from firebase/quickstart-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppDelegate.swift
120 lines (105 loc) · 4.11 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
//
// Copyright (c) 2015 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import UIKit
// [START import]
import Firebase
// [END import]
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let customURLScheme = "dlscheme"
// [START didfinishlaunching]
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Set deepLinkURLScheme to the custom URL scheme you defined in your
// Xcode project.
FirebaseOptions.defaultOptions()?.deepLinkURLScheme = self.customURLScheme
FirebaseApp.configure()
return true
}
// [END didfinishlaunching]
// [START openurl]
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
return application(app, open: url, sourceApplication: nil, annotation: [:])
}
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
let dynamicLink = DynamicLinks.dynamicLinks()?.dynamicLink(fromCustomSchemeURL: url)
if let dynamicLink = dynamicLink {
// Handle the deep link. For example, show the deep-linked content or
// apply a promotional offer to the user's account.
// [START_EXCLUDE]
// In this sample, we just open an alert.
let message = generateDynamicLinkMessage(dynamicLink)
if #available(iOS 8.0, *) {
showDeepLinkAlertView(withMessage: message)
} else {
// Fallback on earlier versions
}
// [END_EXCLUDE]
return true
}
// [START_EXCLUDE silent]
// Show the deep link that the app was called with.
if #available(iOS 8.0, *) {
showDeepLinkAlertView(withMessage: "openURL:\n\(url)")
} else {
// Fallback on earlier versions
}
// [END_EXCLUDE]
return false
}
// [END openurl]
// [START continueuseractivity]
@available(iOS 8.0, *)
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
guard let dynamicLinks = DynamicLinks.dynamicLinks() else {
return false
}
let handled = dynamicLinks.handleUniversalLink(userActivity.webpageURL!) { (dynamiclink, error) in
// [START_EXCLUDE]
let message = self.generateDynamicLinkMessage(dynamiclink!)
self.showDeepLinkAlertView(withMessage: message)
// [END_EXCLUDE]
}
// [START_EXCLUDE silent]
if !handled {
// Show the deep link URL from userActivity.
let message = "continueUserActivity webPageURL:\n\(userActivity.webpageURL?.absoluteString ?? "")"
showDeepLinkAlertView(withMessage: message)
}
// [END_EXCLUDE]
return handled
}
// [END continueuseractivity]
func generateDynamicLinkMessage(_ dynamicLink: DynamicLink) -> String {
let matchConfidence: String
if dynamicLink.matchConfidence == .weak {
matchConfidence = "Weak"
} else {
matchConfidence = "Strong"
}
let message = "App URL: \(dynamicLink.url?.absoluteString ?? "")\nMatch Confidence: \(matchConfidence)\n"
return message
}
@available(iOS 8.0, *)
func showDeepLinkAlertView(withMessage message: String) {
let okAction = UIAlertAction.init(title: "OK", style: .default) { (action) -> Void in
print("OK")
}
let alertController = UIAlertController.init(title: "Deep-link Data", message: message, preferredStyle: .alert)
alertController.addAction(okAction)
self.window?.rootViewController?.present(alertController, animated: true, completion: nil)
}
}