麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Swift > 正文

Swift如何為設置中心添加常用功能

2020-03-09 17:41:52
字體:
來源:轉載
供稿:網友

前言

在我們開發所有的應用中,通常會提供包含多項功能的設置中心。這些功能可以包括,給用戶推薦自己的其他作品、邀請用戶好評、提供反饋通道、邀請用戶分享應用、打開官網或某些其他地址。 這些功能雖然用戶使用頻率不高,但對于應用的設置中心是必備的。

1.跳轉到AppStore,邀請好評或推薦其他應用

2.提供系統郵件反饋通道

3.調取系統分享功能分享應用

4.在應用內打開網頁,實現官方網址、應用更新說明或打開其他網址

通常設置中心由TableView或CollectionView創建,在didSelectRowAt中添加不同的點擊反饋即可,這里就不再描述。

一、跳轉到AppStore

應用內跳轉到AppStore可以通過設置對應的應用地址即可,因此可以跳轉到其他應用界面實現推薦應用,也可以跳轉到自身應用的地址邀請用戶好評。OneX系列產品都擁有推薦和評價的入口,兩種入口的實現方式也都是一樣的。 在不同的情況下我們只需要改變urlString末尾的ID即可,當讓也可以封裝在某一個函數中,通過參數進行改變具體的跳轉地址。

let urlString = "itms-apps://itunes.apple.com/app/id1250290965"if let url = URL(string: urlString) { //根據iOS系統版本,分別處理 if #available(iOS 10, *) { UIApplication.shared.open(url, options: [:],     completionHandler: {     (success) in }) } else { UIApplication.shared.openURL(url) }}

swift,跳轉,appstore

二、郵件反饋功能

第一,需要導入框架MessageUI.framework,在項目設置Build Phases的Link Binary With Libraries中添加MessageUI.framework。 第二,在使用郵件反饋功能的頁面文件中導入頭文件import MessageUI。 第三,給所在Controller加上協議MFMailComposeViewControllerDelegate。

完成以上步驟之后,我們就可以開始寫具體的使用代碼了。 發送反饋郵件時,為了方便我們收到郵件時辨別是用戶發來的反饋郵件,同時了解用戶的系統、版本等信息,我們在發送函數中設置好標題與默認正文。 mailComposeVC.setToRecipients中添加收件郵箱地址,mailComposeVC.setSubject中添加郵件標題,mailComposeVC.setMessageBody設置正文內容。

//郵件發送函數func configuredMailComposeViewController() -> MFMailComposeViewController { let mailComposeVC = MFMailComposeViewController() mailComposeVC.mailComposeDelegate = self //獲取設備信息 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" //設置郵件地址、主題及正文 mailComposeVC.setToRecipients(["<[email protected]>"]) 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}

再需要添加郵件系統提示和郵件發送檢測。

//郵件系統提示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){}}//郵件發送檢測func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { switch result.rawValue { case MFMailComposeResult.cancelled.rawValue: print("取消發送") case MFMailComposeResult.sent.rawValue: print("發送成功") default: break } self.dismiss(animated: true, completion: nil)}

最后我們在調用郵件反饋的地方,需要先判斷是否能夠發送,如果不能發送通過提示信息告訴用戶失敗原因,如果可以發送將成功調取發送窗口。 在需要郵件反饋的地方:

if MFMailComposeViewController.canSendMail() { //注意這個實例要寫在if block里,否則無法發送郵件時會出現兩次提示彈窗(一次是系統的) let mailComposeViewController = configuredMailComposeViewController() self.present(mailComposeViewController, animated: true, completion: nil)} else { self.showSendMailErrorAlert()}

swift,跳轉,appstore

三、系統分享功能

分享前,我們需要設置好分享的信息:標題、圖片、鏈接。

var webUrl:String = "https://itunes.apple.com/cn/app/id1355476695"var urlTitle:String = "OneScreen"var urlImage:UIImage = #imageLiteral(resourceName: "onescreen_icon")

這里使用了var,是為了在特殊情況下改變他們的值,具體的調用方式如下:

let shareVC:UIActivityViewController = UIActivityViewController(activityItems: [self.urlTitle,self.urlImage,self.webUrl], applicationActivities: nil)self.present(shareVC, animated: true, completion: { print("shareVC success")})

swift,跳轉,appstore

四、打開某些網址

打開網址可以實現“官方網址”、“應用更新說明”功能,更新說明我們可以通過更新Web內容快速高速用戶更新列表。如果你的應用需要比較多的教程,也可以通過網頁的形式展現。為了方便用戶反饋,我通常會增加一個微博入口,讓用戶打開微博地址快速與我聯系進行反饋。

這個功能我們需要創建一個承載網頁內容的Web頁面,因此需要先添加帶有WebView的Controller。 在其他頁面打開Web時,通過傳遞參數來告訴WebView具體呈現哪一個網址。

swift,跳轉,appstore

例如在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)}

在設置頁面中,我們開始打開Web:

print("to webview")self.webIndex = 1self.performSegue(withIdentifier: "viewWebView", sender: self)

將WebIndex傳遞給WebViewController,以方便判斷具體的網址。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "viewWebView"{  let dest = segue.destination as! WebViewController  dest.webIndex = self.webIndex }}

這樣就實現了所有相關網址的打開。實際在網頁加載頁面中還有一些特性和功能,將在下一期文章中詳細說明。 打開網址

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網的支持。


注:相關教程知識閱讀請移步到swift教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 久久久久久久久久亚洲 | 色就操 | 国内精品久久久久久久影视红豆 | 日日噜噜噜噜久久久精品毛片 | chinesehdxxxx实拍| 毛片一级免费看 | 国产91久久精品一区二区 | 日本欧美一区二区 | 成年免费网站 | 手机黄色小视频 | 国产精品久久久久国产精品三级 | 成人午夜免费在线观看 | 免费国产自久久久久三四区久久 | 亚洲成人午夜精品 | 亚洲欧美一区二区三区在线观看 | 久久久久久久91 | 56av国产精品久久久久久久 | 毛片免费视频观看 | 国产一区免费在线 | 特色一级黄色片 | 国产福利视频在线观看 | 久久精品一区视频 | 国产精品免费视频观看 | 最近中文字幕一区二区 | 国产又粗又爽又深的免费视频 | 国产电影精品久久 | 91久久国产综合久久91精品网站 | 欧美亚洲综合网 | 精品一区二区三区欧美 | 精品一区二区三区在线播放 | 国产免费午夜 | www.三区| 最新中文字幕在线视频 | 欧美一级精品 | 毛片大全免费看 | 国产午夜精品久久久久 | 久久久国产精品成人免费 | 色婷婷久久久亚洲一区二区三区 | 国产午夜精品一区二区三区视频 | japan护士性xxxⅹhd| 福利免费在线 |