Skip to content

Instantly share code, notes, and snippets.

@AmatsuZero
Created December 17, 2018 03:10
Show Gist options
  • Save AmatsuZero/815516315a8b284079c16abfb6558031 to your computer and use it in GitHub Desktop.
Save AmatsuZero/815516315a8b284079c16abfb6558031 to your computer and use it in GitHub Desktop.
iOS 检查推送是否打开&跳转到设置页面
//
// UIApplication+Extensions.swift
//
// Created by Jiang,Zhenhua on 2018/12/17.
// Copyright © 2018 Daubert. All rights reserved.
//
import UIKit
import UserNotifications
extension UIApplication {
static func isPushNotificationEnabled(completionHandler: @escaping ((Bool) -> Void)) {
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().getNotificationSettings {
completionHandler($0.authorizationStatus == .authorized)
}
} else {
completionHandler(shared.currentUserNotificationSettings?.types != .none)
}
}
static func isPushNotificationEnabledSync() -> Bool {
var ret = false
let sema = DispatchSemaphore(value: 0)
isPushNotificationEnabled { ret = $0 }
_ = sema.wait(timeout: .distantFuture)
return ret
}
static func openNotificationSetting(completionHandler: ((Bool) -> Void)? = nil) {
if #available(iOS 10.0, *) {
if let url = URL(string: UIApplicationOpenSettingsURLString) {
shared.open(url, options: [:], completionHandler: completionHandler)
}
} else if let id = Bundle.main.infoDictionary?["CFBundleIdentifier"] as? String,
let url = URL(string: "prefs:root=\(id)") {
completionHandler?(shared.openURL(url))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment