Swift如何為設(shè)置中心添加常用功能
前言
在我們開發(fā)所有的應(yīng)用中,通常會提供包含多項功能的設(shè)置中心。這些功能可以包括,給用戶推薦自己的其他作品、邀請用戶好評、提供反饋通道、邀請用戶分享應(yīng)用、打開官網(wǎng)或某些其他地址。 這些功能雖然用戶使用頻率不高,但對于應(yīng)用的設(shè)置中心是必備的。
1.跳轉(zhuǎn)到AppStore,邀請好評或推薦其他應(yīng)用
2.提供系統(tǒng)郵件反饋通道
3.調(diào)取系統(tǒng)分享功能分享應(yīng)用
4.在應(yīng)用內(nèi)打開網(wǎng)頁,實現(xiàn)官方網(wǎng)址、應(yīng)用更新說明或打開其他網(wǎng)址
通常設(shè)置中心由TableView或CollectionView創(chuàng)建,在didSelectRowAt中添加不同的點擊反饋即可,這里就不再描述。
一、跳轉(zhuǎn)到AppStore
應(yīng)用內(nèi)跳轉(zhuǎn)到AppStore可以通過設(shè)置對應(yīng)的應(yīng)用地址即可,因此可以跳轉(zhuǎn)到其他應(yīng)用界面實現(xiàn)推薦應(yīng)用,也可以跳轉(zhuǎn)到自身應(yīng)用的地址邀請用戶好評。OneX系列產(chǎn)品都擁有推薦和評價的入口,兩種入口的實現(xiàn)方式也都是一樣的。 在不同的情況下我們只需要改變urlString末尾的ID即可,當(dāng)讓也可以封裝在某一個函數(shù)中,通過參數(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,在項目設(shè)置Build Phases的Link Binary With Libraries中添加MessageUI.framework。 第二,在使用郵件反饋功能的頁面文件中導(dǎo)入頭文件import MessageUI。 第三,給所在Controller加上協(xié)議MFMailComposeViewControllerDelegate。
完成以上步驟之后,我們就可以開始寫具體的使用代碼了。 發(fā)送反饋郵件時,為了方便我們收到郵件時辨別是用戶發(fā)來的反饋郵件,同時了解用戶的系統(tǒng)、版本等信息,我們在發(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的版本號 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ā)送檢測。
//郵件系統(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ā)送檢測 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) }
最后我們在調(diào)用郵件反饋的地方,需要先判斷是否能夠發(fā)送,如果不能發(fā)送通過提示信息告訴用戶失敗原因,如果可以發(fā)送將成功調(diào)取發(fā)送窗口。 在需要郵件反饋的地方:
if MFMailComposeViewController.canSendMail() { //注意這個實例要寫在if block里,否則無法發(fā)送郵件時會出現(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)址可以實現(xiàn)“官方網(wǎng)址”、“應(yīng)用更新說明”功能,更新說明我們可以通過更新Web內(nèi)容快速高速用戶更新列表。如果你的應(yīng)用需要比較多的教程,也可以通過網(wǎng)頁的形式展現(xiàn)。為了方便用戶反饋,我通常會增加一個微博入口,讓用戶打開微博地址快速與我聯(lián)系進(jìn)行反饋。
這個功能我們需要創(chuàng)建一個承載網(wǎng)頁內(nèi)容的Web頁面,因此需要先添加帶有WebView的Controller。 在其他頁面打開Web時,通過傳遞參數(shù)來告訴WebView具體呈現(xiàn)哪一個網(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è)置頁面中,我們開始打開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 } }
這樣就實現(xiàn)了所有相關(guān)網(wǎng)址的打開。實際在網(wǎng)頁加載頁面中還有一些特性和功能,將在下一期文章中詳細(xì)說明。 打開網(wǎng)址
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對我們的支持。
您可能感興趣的文章
- 01-11swift中defer幾個簡單的使用場景詳解
- 01-11Swift利用Decodable解析JSON的一個小問題詳解
- 01-11Swift中defer關(guān)鍵字推遲執(zhí)行示例詳解
- 01-11Swift中初始化init的方法小結(jié)
- 01-11Swift中定義單例的方法實例
- 01-11Swift利用純代碼實現(xiàn)時鐘效果實例代碼
- 01-11Swift中排序算法的簡單取舍詳解
- 01-11Swift Json實例詳細(xì)解析
- 01-11Swift利用指紋識別或面部識別為應(yīng)用添加私密保護(hù)功能
- 01-11Swift 4.0中如何引用3.0的第三方庫


閱讀排行
本欄相關(guān)
- 01-11Swift利用Decodable解析JSON的一個小問題
- 01-11swift中defer幾個簡單的使用場景詳解
- 01-11Swift中初始化init的方法小結(jié)
- 01-11Swift中defer關(guān)鍵字推遲執(zhí)行示例詳解
- 01-11Swift利用純代碼實現(xiàn)時鐘效果實例代碼
- 01-11Swift中定義單例的方法實例
- 01-11Swift中排序算法的簡單取舍詳解
- 01-11Swift Json實例詳細(xì)解析
- 01-11Swift如何為設(shè)置中心添加常用功能
- 01-11Swift利用指紋識別或面部識別為應(yīng)用添
隨機閱讀
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實例總結(jié)
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10使用C語言求解撲克牌的順子及n個骰子