Swift如何為設(shè)置中心添加常用功能
前言
在我們開發(fā)所有的應(yīng)用中,通常會(huì)提供包含多項(xiàng)功能的設(shè)置中心。這些功能可以包括,給用戶推薦自己的其他作品、邀請(qǐng)用戶好評(píng)、提供反饋通道、邀請(qǐng)用戶分享應(yīng)用、打開官網(wǎng)或某些其他地址。 這些功能雖然用戶使用頻率不高,但對(duì)于應(yīng)用的設(shè)置中心是必備的。
1.跳轉(zhuǎn)到AppStore,邀請(qǐng)好評(píng)或推薦其他應(yīng)用
2.提供系統(tǒng)郵件反饋通道
3.調(diào)取系統(tǒng)分享功能分享應(yīng)用
4.在應(yīng)用內(nèi)打開網(wǎng)頁(yè),實(shí)現(xiàn)官方網(wǎng)址、應(yīng)用更新說(shuō)明或打開其他網(wǎng)址
通常設(shè)置中心由TableView或CollectionView創(chuàng)建,在didSelectRowAt中添加不同的點(diǎn)擊反饋即可,這里就不再描述。
一、跳轉(zhuǎn)到AppStore
應(yīng)用內(nèi)跳轉(zhuǎn)到AppStore可以通過(guò)設(shè)置對(duì)應(yīng)的應(yīng)用地址即可,因此可以跳轉(zhuǎn)到其他應(yīng)用界面實(shí)現(xiàn)推薦應(yīng)用,也可以跳轉(zhuǎn)到自身應(yīng)用的地址邀請(qǐng)用戶好評(píng)。OneX系列產(chǎn)品都擁有推薦和評(píng)價(jià)的入口,兩種入口的實(shí)現(xiàn)方式也都是一樣的。 在不同的情況下我們只需要改變urlString末尾的ID即可,當(dāng)讓也可以封裝在某一個(gè)函數(shù)中,通過(guò)參數(shù)進(jìn)行改變具體的跳轉(zhuǎn)地址。
let urlString = "itms-apps://itunes.apple.com/app/id1250290965" if let url = URL(string: urlString) { //根據(jù)iOS系統(tǒng)版本,分別處理 if #available(iOS 10, *) { UIApplication.shared.open(url, options: [:], completionHandler: { (success) in }) } else { UIApplication.shared.openURL(url) } }
二、郵件反饋功能
第一,需要導(dǎo)入框架MessageUI.framework,在項(xiàng)目設(shè)置Build Phases的Link Binary With Libraries中添加MessageUI.framework。 第二,在使用郵件反饋功能的頁(yè)面文件中導(dǎo)入頭文件import MessageUI。 第三,給所在Controller加上協(xié)議MFMailComposeViewControllerDelegate。
完成以上步驟之后,我們就可以開始寫具體的使用代碼了。 發(fā)送反饋郵件時(shí),為了方便我們收到郵件時(shí)辨別是用戶發(fā)來(lái)的反饋郵件,同時(shí)了解用戶的系統(tǒng)、版本等信息,我們?cè)诎l(fā)送函數(shù)中設(shè)置好標(biāo)題與默認(rèn)正文。 mailComposeVC.setToRecipients中添加收件郵箱地址,mailComposeVC.setSubject中添加郵件標(biāo)題,mailComposeVC.setMessageBody設(shè)置正文內(nèi)容。
//郵件發(fā)送函數(shù) func configuredMailComposeViewController() -> MFMailComposeViewController { let mailComposeVC = MFMailComposeViewController() mailComposeVC.mailComposeDelegate = self //獲取設(shè)備信息 let deviceName = UIDevice.current.name // let deviceModel = UIDevice.current.model let systemVersion = UIDevice.current.systemVersion let deviceUUID = UIDevice.current.identifierForVendor?.uuidString //獲取APP信息 let infoDic = Bundle.main.infoDictionary // 獲取App的版本號(hào) let appVersion = infoDic?["CFBundleShortVersionString"] ?? "appVersion" // 獲取App的build版本 let appBuildVersion = infoDic?["CFBundleVersion"] ?? "appBuildVersion" // 獲取App的名稱 let appName = infoDic?["CFBundleDisplayName"] ?? "OneClock" //設(shè)置郵件地址、主題及正文 mailComposeVC.setToRecipients(["<xdehang@gmail.com>"]) mailComposeVC.setSubject("OneScreen "+String(describing: appVersion)+" - "+NSLocalizedString("FeedBack Mail From", comment: "FeedBack Mail From")+" "+deviceName) let content:String = "\n \n \n \n Device:\(deviceName)\n System:\(systemVersion)\n App Version:\(String(describing: appVersion))" mailComposeVC.setMessageBody(NSLocalizedString("<Start To Write Mail>", comment: "<Start To Write Mail>")+content, isHTML: false) return mailComposeVC }
再需要添加郵件系統(tǒng)提示和郵件發(fā)送檢測(cè)。
//郵件系統(tǒng)提示 func showSendMailErrorAlert() { let sendMailErrorAlert = UIAlertController(title: NSLocalizedString("Unable To Send", comment: "Unable To Send"), message: NSLocalizedString("Your device has not been set up, please set in the mail application and then try to send.", comment: "Your device has not been set up, please set in the mail application and then try to send."), preferredStyle: .alert) sendMailErrorAlert.addAction(UIAlertAction(title: NSLocalizedString("Confirm", comment: "Confirm action title"), style: .default) { _ in }) self.present(sendMailErrorAlert, animated: true){} } //郵件發(fā)送檢測(cè) func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { switch result.rawValue { case MFMailComposeResult.cancelled.rawValue: print("取消發(fā)送") case MFMailComposeResult.sent.rawValue: print("發(fā)送成功") default: break } self.dismiss(animated: true, completion: nil) }
最后我們?cè)谡{(diào)用郵件反饋的地方,需要先判斷是否能夠發(fā)送,如果不能發(fā)送通過(guò)提示信息告訴用戶失敗原因,如果可以發(fā)送將成功調(diào)取發(fā)送窗口。 在需要郵件反饋的地方:
if MFMailComposeViewController.canSendMail() { //注意這個(gè)實(shí)例要寫在if block里,否則無(wú)法發(fā)送郵件時(shí)會(huì)出現(xiàn)兩次提示彈窗(一次是系統(tǒng)的) let mailComposeViewController = configuredMailComposeViewController() self.present(mailComposeViewController, animated: true, completion: nil) } else { self.showSendMailErrorAlert() }
三、系統(tǒng)分享功能
分享前,我們需要設(shè)置好分享的信息:標(biāo)題、圖片、鏈接。
var webUrl:String = "https://itunes.apple.com/cn/app/id1355476695" var urlTitle:String = "OneScreen" var urlImage:UIImage = #imageLiteral(resourceName: "onescreen_icon")
這里使用了var,是為了在特殊情況下改變他們的值,具體的調(diào)用方式如下:
let shareVC:UIActivityViewController = UIActivityViewController(activityItems: [self.urlTitle,self.urlImage,self.webUrl], applicationActivities: nil) self.present(shareVC, animated: true, completion: { print("shareVC success") })
四、打開某些網(wǎng)址
打開網(wǎng)址可以實(shí)現(xiàn)“官方網(wǎng)址”、“應(yīng)用更新說(shuō)明”功能,更新說(shuō)明我們可以通過(guò)更新Web內(nèi)容快速高速用戶更新列表。如果你的應(yīng)用需要比較多的教程,也可以通過(guò)網(wǎng)頁(yè)的形式展現(xiàn)。為了方便用戶反饋,我通常會(huì)增加一個(gè)微博入口,讓用戶打開微博地址快速與我聯(lián)系進(jìn)行反饋。
這個(gè)功能我們需要?jiǎng)?chuàng)建一個(gè)承載網(wǎng)頁(yè)內(nèi)容的Web頁(yè)面,因此需要先添加帶有WebView的Controller。 在其他頁(yè)面打開Web時(shí),通過(guò)傳遞參數(shù)來(lái)告訴WebView具體呈現(xiàn)哪一個(gè)網(wǎng)址。
例如在OneDay的WebViewController中:
override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. switch webIndex { case 0: self.urlString = "https://weibo.com/bujidehang" case 1: self.urlString = "http://www.ohweonline.com/oneday" case 2: self.urlString = "http://www.ohweonline.com/oneday/updateCN.html" case 3: self.urlString = "http://www.ohweonline.com/oneday/updateEN.html" default: self.urlString = "http://www.ohweonline.com/oneday" } let urlobj = URL(string:self.urlString) let request = URLRequest(url:urlobj!) webView.loadRequest(request) print(webView.isLoading) }
在設(shè)置頁(yè)面中,我們開始打開Web:
print("to webview") self.webIndex = 1 self.performSegue(withIdentifier: "viewWebView", sender: self)
將WebIndex傳遞給WebViewController,以方便判斷具體的網(wǎng)址。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "viewWebView"{ let dest = segue.destination as! WebViewController dest.webIndex = self.webIndex } }
這樣就實(shí)現(xiàn)了所有相關(guān)網(wǎng)址的打開。實(shí)際在網(wǎng)頁(yè)加載頁(yè)面中還有一些特性和功能,將在下一期文章中詳細(xì)說(shuō)明。 打開網(wǎng)址
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Swift實(shí)現(xiàn)多個(gè)TableView側(cè)滑與切換效果
這篇文章主要為大家詳細(xì)介紹了Swift實(shí)現(xiàn)多個(gè)TableView側(cè)滑與切換效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11詳解Swift中的數(shù)據(jù)類型類型轉(zhuǎn)換
Swift中的類型轉(zhuǎn)換可以結(jié)合類的繼承等面向?qū)ο蟮木幊烫匦詠?lái)進(jìn)行,本文中我們就來(lái)詳解Swift中的數(shù)據(jù)類型類型轉(zhuǎn)換,需要的朋友可以參考下2016-07-07淺析Swift中struct與class的區(qū)別(匯編角度底層分析)
這篇文章主要介紹了Swift中struct與class的區(qū)別 ,本文從匯編角度分析struct與class的區(qū)別,通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03Objective-c代碼如何移植為Swift代碼 Objective-c代碼轉(zhuǎn)移到Swift過(guò)程介紹
這篇文章主要介紹了Objective-c代碼如何移植為Swift代碼,Objective-c代碼轉(zhuǎn)移到Swift過(guò)程介紹,需要的朋友可以參考下2014-07-07