/
UIApplication.swift
117 lines (107 loc) 路 4.25 KB
/
UIApplication.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
#if canImport(UIKit)
import UIKit
#endif
public extension UIApplication {
/// Application key window
static let keyWindow = UIApplication
.shared
.connectedScenes
.filter { $0.activationState == .foregroundActive }
.compactMap { $0 as? UIWindowScene }
.first?.windows
.filter { $0.isKeyWindow }
.first
/// Open application settings
func openSettings() {
if let url = URL(string: UIApplication.openSettingsURLString), canOpenURL(url) {
if #available(iOS 10.0, *) {
open(url, options: [:], completionHandler: nil)
} else {
openURL(url)
}
}
}
/// Opens application sheet for phonen number
func openPhone(calling number: String) {
if let url = URL(string: "tel://" + number) {
if #available(iOS 10.0, *) {
open(url, options: [:], completionHandler: nil)
} else {
openURL(url)
}
}
}
/// Find My Facebook ID: https://findmyfbid.com/
/// - Parameters:
/// - name: Facebook name
/// - id: Facebook ID
func openFacebook(name: String?, id: String?) {
if let facebookID = id,
let facebookURL = URL(string: "fb://profile/\(facebookID)"),
UIApplication.shared.canOpenURL(facebookURL) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(facebookURL, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(facebookURL)
}
}
else if let name = name {
UIViewController.getCurrentViewController()?.pushSafariViewController(urlString: "https://www.facebook.com/" + name)
}
}
/// Launches the Instagram app and loads the Instagram user
/// - Parameter name: Instagram username
func openInstagram(name: String?) {
if let ins = name {
if let instagramURL = URL(string: "instagram://user?username=\(ins)"),
UIApplication.shared.canOpenURL(instagramURL) {
if #available(iOS 10.0, *) {
open(instagramURL, options: [:], completionHandler: nil)
} else {
openURL(instagramURL)
}
} else {
UIViewController.getCurrentViewController()?.pushSafariViewController(urlString: "https://www.instagram.com/" + ins)
}
}
}
/// Launches the Instagram app and loads the Instagram user
/// - Parameter name: Instagram username
func openInstagram(media: String?) {
if let media = media {
if let instagramURL = URL(string: "instagram://media?id=\(media)"),
UIApplication.shared.canOpenURL(instagramURL) {
if #available(iOS 10.0, *) {
open(instagramURL, options: [:], completionHandler: nil)
} else {
openURL(instagramURL)
}
} else {
UIViewController.getCurrentViewController()?.pushSafariViewController(urlString: "https://www.instagram.com/" + media)
}
}
}
/// Open a map app with the given query. Orders: Google Map -> Apple Map
///
/// - Parameter query: The query to search on the map.
func openExternalMapApp(query: String) {
if let string = "comgooglemaps://?q=\(query)&zoom=13".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed),
let googleMapURL = URL(string: string),
UIApplication.shared.canOpenURL(googleMapURL) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(googleMapURL)
} else {
dprint("Error: opening google maps url with query: \(query)")
}
return
}
if let string = "https://maps.apple.com/?address=\(query)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed), let url = URL(string: string), UIApplication.shared.canOpenURL(url) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url)
} else {
dprint("Error: opening google maps url with query: \(query)")
}
return
}
}
}