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)頁,實(shí)現(xiàn)官方網(wǎng)址、應(yīng)用更新說明或打開其他網(wǎng)址
通常設(shè)置中心由TableView或CollectionView創(chuàng)建,在didSelectRowAt中添加不同的點(diǎn)擊反饋即可,這里就不再描述。
一、跳轉(zhuǎn)到AppStore
應(yīng)用內(nèi)跳轉(zhuǎn)到AppStore可以通過設(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ù)中,通過參數(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。 第二,在使用郵件反饋功能的頁面文件中導(dǎo)入頭文件import MessageUI。 第三,給所在Controller加上協(xié)議MFMailComposeViewControllerDelegate。
完成以上步驟之后,我們就可以開始寫具體的使用代碼了。 發(fā)送反饋郵件時(shí),為了方便我們收到郵件時(shí)辨別是用戶發(fā)來的反饋郵件,同時(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ā)送通過提示信息告訴用戶失敗原因,如果可以發(fā)送將成功調(diào)取發(fā)送窗口。 在需要郵件反饋的地方:
if MFMailComposeViewController.canSendMail() {
//注意這個(gè)實(shí)例要寫在if block里,否則無法發(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)用更新說明”功能,更新說明我們可以通過更新Web內(nèi)容快速高速用戶更新列表。如果你的應(yīng)用需要比較多的教程,也可以通過網(wǎng)頁的形式展現(xiàn)。為了方便用戶反饋,我通常會(huì)增加一個(gè)微博入口,讓用戶打開微博地址快速與我聯(lián)系進(jìn)行反饋。
這個(gè)功能我們需要?jiǎng)?chuàng)建一個(gè)承載網(wǎng)頁內(nèi)容的Web頁面,因此需要先添加帶有WebView的Controller。 在其他頁面打開Web時(shí),通過傳遞參數(shù)來告訴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è)置頁面中,我們開始打開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)頁加載頁面中還有一些特性和功能,將在下一期文章中詳細(xì)說明。 打開網(wǎng)址
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)我們的支持。
您可能感興趣的文章
- 01-11swift中defer幾個(gè)簡(jiǎn)單的使用場(chǎng)景詳解
- 01-11Swift利用Decodable解析JSON的一個(gè)小問題詳解
- 01-11Swift中defer關(guān)鍵字推遲執(zhí)行示例詳解
- 01-11Swift中初始化init的方法小結(jié)
- 01-11Swift中定義單例的方法實(shí)例
- 01-11Swift利用純代碼實(shí)現(xiàn)時(shí)鐘效果實(shí)例代碼
- 01-11Swift中排序算法的簡(jiǎn)單取舍詳解
- 01-11Swift Json實(shí)例詳細(xì)解析
- 01-11Swift利用指紋識(shí)別或面部識(shí)別為應(yīng)用添加私密保護(hù)功能
- 01-11Swift 4.0中如何引用3.0的第三方庫(kù)


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-11Swift利用Decodable解析JSON的一個(gè)小問題
- 01-11swift中defer幾個(gè)簡(jiǎn)單的使用場(chǎng)景詳解
- 01-11Swift中初始化init的方法小結(jié)
- 01-11Swift中defer關(guān)鍵字推遲執(zhí)行示例詳解
- 01-11Swift利用純代碼實(shí)現(xiàn)時(shí)鐘效果實(shí)例代碼
- 01-11Swift中定義單例的方法實(shí)例
- 01-11Swift中排序算法的簡(jiǎn)單取舍詳解
- 01-11Swift Json實(shí)例詳細(xì)解析
- 01-11Swift如何為設(shè)置中心添加常用功能
- 01-11Swift利用指紋識(shí)別或面部識(shí)別為應(yīng)用添
隨機(jī)閱讀
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子


