這篇文章主要在吐槽bug莽使,還有直接上代碼的凑懂,
如果你剛要用腥椒,請(qǐng)轉(zhuǎn)至簡(jiǎn)友入門(mén)基礎(chǔ)篇:http://www.reibang.com/p/c9aa0a0ae2c4
然后再來(lái)看這篇哦!
有些第三方庫(kù)并不是完美的敌蚜,甚至還有很多坑在等著你桥滨,比如 PermissionScope!
但雖然不完善,原理還有開(kāi)源是非常值得我們?nèi)W(xué)習(xí)的弛车!
先 pod 'PermissionScope',然后 ...
不多說(shuō)了齐媒,直接上馬了,先上二張圖纷跛!
首先千千萬(wàn)萬(wàn)記得在 //using weak var by apiapia on 07/08/2017
不然喻括,資源一下子就會(huì)節(jié)節(jié)往上的喔!
FIXME: on PermissionScope -> PermissionScope.swift
weak var weakSelf = self
viewControllerForAlerts = weakSelf
第一次裝完APP贫奠,并進(jìn)入通知設(shè)置viewController
如果用戶(hù)不同意唬血,下次再進(jìn)行設(shè)置時(shí),直接引導(dǎo)用戶(hù)到設(shè)置頁(yè)面
用戶(hù)點(diǎn)擊稍后唤崭,原生是nil的拷恨,下面是代碼補(bǔ)充!
let alert = UIAlertController(title: "前往開(kāi)啟\(permission.prettyDescription)權(quán)限".localized,
message: "請(qǐng)點(diǎn)擊·前往設(shè)置·開(kāi)啟 \(permission.prettyDescription) 權(quán)限".localized,
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "稍后".localized,style: .cancel,
handler: { action in
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "NSNotificationNameAlert"), object: nil, userInfo: ["permission":permission.prettyDescription])
}))
接收通知
// 觀察者
weak var weakSelf = self
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "NSNotificationNameAlert"), object: nil, queue: OperationQueue.main) { (key) in
let permission = key.userInfo?["permission"] as! String
print ("permission",permission)
if permission == "Notifications" {
print("關(guān)閉推送")
weakSelf?.swtichAnnounce.isOn = false
}else {
print("關(guān)閉地理位置")
weakSelf?.switchK3.isOn = false
}
}
記得移除通知
deinit {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "NSNotificationNameAlert"), object: nil)
}
以下是代碼
import UIKit
import MJRefresh
import SVProgressHUD
import Kingfisher
import PermissionScope
import CoreLocation
class MeVC:UITableViewController,ShareProtocol{
fileprivate var permissionScopeManager:PermissionScope!
override func viewDidLoad() {
super.viewDidLoad()
setTableView()
// 觀察者
weak var weakSelf = self
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "NSNotificationNameAlert"), object: nil, queue: OperationQueue.main) { (key) in
let permission = key.userInfo?["permission"] as! String
print ("permission",permission)
if permission == "Notifications" {
print("關(guān)閉推送")
weakSelf?.swtichAnnounce.isOn = false
}else {
print("關(guān)閉地理位置")
weakSelf?.switchK3.isOn = false
}
}
}
@IBOutlet weak var swtichAnnounce: UISwitch!
@IBOutlet weak var switchK3: UISwitch!
// 推送設(shè)置
@IBAction func switchAnounceTap(_ sender: UISwitch) {
weak var weakSelf = self
// print("switch推送位置開(kāi)關(guān):\(sender)")
if sender.isOn {
/// 請(qǐng)求通知權(quán)限
permissionScopeManager.viewControllerForAlerts = weakSelf
permissionScopeManager.requestNotifications()
/// 監(jiān)控通知權(quán)限谢肾,一旦用戶(hù)點(diǎn)擊允許或者拒絕腕侄,都會(huì)執(zhí)行以下方法
permissionScopeManager.onAuthChange = { (finished, results) in
print("通知:",weakSelf?.permissionScopeManager.statusNotifications() ?? "")
let status = "\((weakSelf?.permissionScopeManager.statusNotifications())!)"
switch status {
case "Unauthorized" :
weakSelf?.swtichAnnounce.isOn = false
weakSelf?.swtichAnnounce.isEnabled = true
case "Authorized" :
weakSelf?.swtichAnnounce.isOn = true
weakSelf?.swtichAnnounce.isEnabled = false
default :
print("")
}
weakSelf?.permissionScopeManager.viewControllerForAlerts?.dismiss(animated: false, completion: nil)
}
/// 獲取當(dāng)前的發(fā)送通知的權(quán)限狀態(tài)
let status = permissionScopeManager.statusNotifications()
permissionScopeManager.onDisabledOrDenied = { results in
print ("拒絕時(shí)候的results:\(results)")
print ("通知權(quán)限被拒絕:\(status.description)")
weakSelf?.swtichAnnounce.isOn = false
weakSelf?.swtichAnnounce.isEnabled = true
}
if status == .authorized {
weakSelf?.swtichAnnounce.isOn = true
weakSelf?.swtichAnnounce.isEnabled = false
UIApplication.shared.registerForRemoteNotifications()
}
}
else {
// FIXME:調(diào)用系統(tǒng)設(shè)置 關(guān)閉權(quán)限
}
}
// 地址位置分享
@IBAction func switchK3Tap(_ sender: UISwitch) {
weak var weakSelf = self
// print("switch地址位置開(kāi)關(guān):\(sender)")
if sender.isOn {
permissionScopeManager.viewControllerForAlerts = weakSelf
permissionScopeManager.requestLocationAlways()
permissionScopeManager.onAuthChange = { (finished,results) in
print ("地理位置:\(weakSelf?.permissionScopeManager.statusLocationAlways())")
let status = "\((weakSelf?.permissionScopeManager.statusLocationAlways())!)"
switch status {
case "Unauthorized" :
weakSelf?.switchK3.isOn = false
weakSelf?.switchK3.isEnabled = true
case "Authorized" :
weakSelf?.switchK3.isOn = true
weakSelf?.switchK3.isEnabled = false
default :
print("返回地理位置")
}
weakSelf?.permissionScopeManager.viewControllerForAlerts?.dismiss(animated: false, completion: nil)
}
let status = permissionScopeManager.statusLocationAlways()
if status == .authorized {
weakSelf?.switchK3.isOn = true
weakSelf?.switchK3.isEnabled = false
}
permissionScopeManager.onDisabledOrDenied = { result in
weakSelf?.switchK3.isOn = false
weakSelf?.switchK3.isEnabled = true
print ("地理位置權(quán)限被拒絕:\(status.description)")
}
}else {
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "mine_settingIcon2", highlightedImage: "mine_settingIcon2_press", target: self, action: #selector(MeVC.settingClick))
permissionScopeManager = PermissionScope.init(backgroundTapCancels:true)
/// 設(shè)置彈出提示框的底層視圖控制器
initNotifications()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "NSNotificationNameAlert"), object: nil)
}
// deinit {
// NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "NSNotificationNameAlert"), object: nil)
// }
// 通告 權(quán)限
func initNotifications() {
/// 初始化權(quán)限管理對(duì)象
// 判斷通知是否已開(kāi)啟
switch permissionScopeManager.statusNotifications() {
case .unknown:
swtichAnnounce.isOn = false
swtichAnnounce.isEnabled = true
case .unauthorized,.disabled:
swtichAnnounce.isOn = false
swtichAnnounce.isEnabled = true
//return
case .authorized:
swtichAnnounce.isOn = true
swtichAnnounce.isEnabled = false
//return
}
print ("PermissionScope().statusNotifications():\(permissionScopeManager.statusNotifications())")
// 判斷地理位置是否已開(kāi)啟
switch permissionScopeManager.statusLocationAlways() {
case .unknown :
switchK3.isOn = false
switchK3.isEnabled = true
case .unauthorized,.disabled:
switchK3.isOn = false
switchK3.isEnabled = true
// return
case .authorized :
switchK3.isOn = true
switchK3.isEnabled = false
//return
}
print ("PermissionScope().statusLocationAlways():\(permissionScopeManager.statusLocationAlways())")
}
// MARK: - 設(shè)置tableView
func setTableView() {
self.tableView.showsHorizontalScrollIndicator = false
self.tableView.showsVerticalScrollIndicator = false
self.tableView.sectionHeaderHeight = 0
self.tableView.sectionFooterHeight = 10
self.tableView.contentInset = UIEdgeInsetsMake(-25, 0, 0, 0)
self.tableView.tableFooterView = UIView(frame: .zero)
}
// MARK: - 點(diǎn)擊設(shè)置按鈕
func settingClick() {
let settingVC = UIStoryboard(name: "SettingVC", bundle: nil).instantiateInitialViewController()!
navigationController?.pushViewController(settingVC, animated: true)
}
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let section = indexPath.section
let row = indexPath.row
if section == 0 {
// getAuthWithUserInfo(.sina) // print("新浪登陸")
getAuthWithUserInfo(.QQ) // 成功獲取
//getAuthWithUserInfo(UMSocialPlatformType.wechatSession)
} else if section == 1 {
if row == 0 {
print("意見(jiàn)反饋")
} else if row == 1 {
// FIXME: 接入環(huán)信客服,服務(wù)器會(huì)保留信息芦疏,直接用電話(huà)兜挨? /* 聯(lián)系客服*/
print("聯(lián)系客服")
let nextVC = UIStoryboard(name: "MySB", bundle: nil).instantiateViewController(withIdentifier: "KFSB")
navigationController?.pushViewController(nextVC)
}
} else if section == 2 {
/*protocoal 邀請(qǐng)好友*/
if row == 0 { self.umengShareToAnother() }
if row == 1 {print("商務(wù)合作")}
if row == 2 {print("關(guān)于布袋")}
} else if section == 3 {
}
}
}
// FIXME: - 第三方登錄需把APP上傳到官方進(jìn)行審核,才可以接入眯分,待營(yíng)業(yè)執(zhí)照來(lái)進(jìn)行修復(fù)!
extension MeVC {
func getAuthWithUserInfo(_ platformType:UMSocialPlatformType){
UMSocialManager.default().getUserInfo(with: platformType, currentViewController: self) { (result, error) in
if (error != nil) {
print(error ?? "")
}else{
let resp: UMSocialUserInfoResponse? = result as! UMSocialUserInfoResponse?
let uid = resp?.uid
let username = resp?.name
let iconurl = resp?.iconurl
let gender = resp?.gender
print("第三方平臺(tái)\(platformType),唯一ID:\(uid)昵稱(chēng):\(username)頭像:\(iconurl)性別\(gender)")
}
}
}
}
重要的事情說(shuō)三遍柒桑,最重要的一個(gè)BUG弊决,最重要的一個(gè)BUG,最重要的一個(gè)BUG就是:有時(shí)點(diǎn)擊后跳到設(shè)置頁(yè)面魁淳,打開(kāi)通知飘诗,返回APP ,會(huì)自動(dòng)啟動(dòng)頁(yè)廣告界逛,然后到了APP首頁(yè)昆稿!
上面這個(gè)還沒(méi)有修復(fù)...