OneSwift – iOS Tips Based On Swift

In all the applications we develop, we usually provide a multi-functional setup center. These features can include recommending other games to users, inviting users to comment on them, providing feedback channels, inviting users to share apps, opening the official website or some other address. These features are infrequently used by users, but are essential to the application’s setup center.

1. Jump to the AppStore and invite reviews or recommend other apps

2. Provide a system email feedback channel

3. Access the sharing application of the system sharing function

4. Open a web page within the app to implement the official url, app update instructions, or open other urls

Usually the setup center is created by the TableView or the CollectionView, so just add different click-feedback in the didSelectRowAt, which I won’t describe here.

Jump to the AppStore

To go to the AppStore within an app, you only need to set the corresponding app address. Therefore, you can go to other app interfaces to recommend apps, or go to your own app address to invite users to comment on your app. The OneX family of products all have recommendation and review portals, and both portals are implemented in the same way. In different cases, we only need to change the ID at the end of urlString. When we change the ID, we can also encapsulate it in a function and change the specific jump address by parameter.

let urlString = "itms-apps://itunes.apple.com/app/id1250290965"
if letUrl = url (string: urlString) {// Perform the operations based on the iOS versionif #available(iOS 10, *) {
        UIApplication.shared.open(url, options: [:],
                                  completionHandler: {
                                    (success) in})}else {
        UIApplication.shared.openURL(url)
    }
}
Copy the code

2. Email feedback function

First, you need to import messageui. framework and add messageuI. framework to the Link Binary With Libraries setting Build Phases. Second, import the header import MessageUI in the page file that uses email feedback. Third, put the Controller combined with MFMailComposeViewControllerDelegate agreement.

After completing the above steps, we can start to write concrete usage code. When sending feedback emails, we set the title and default body in the sending function in order to facilitate us to identify the feedback emails sent by users when receiving them, and to learn about the user’s system and version. MailComposeVC. SetToRecipients, receives an email address is added to the mailComposeVC. Add mail title, setSubject mailComposeVC. SetMessageBody set body content.

/ / email function func configuredMailComposeViewController () - > MFMailComposeViewController {letMailComposeVC = MFMailComposeViewController () mailComposeVC. MailComposeDelegate = self / / equipment informationlet deviceName = UIDevice.current.name
    //        let deviceModel = UIDevice.current.model
    let systemVersion = UIDevice.current.systemVersion
    letdeviceUUID = UIDevice.current.identifierForVendor? UuidString // Obtain APP informationletInfoDic = Bundle. Main. InfoDictionary / / get the App version numberletappVersion = infoDic? ["CFBundleShortVersionString"]????"appVersion"// Get the build of the AppletappBuildVersion = infoDic? ["CFBundleVersion"]????"appBuildVersion"// Get the App nameletappName = infoDic? ["CFBundleDisplayName"]????"OneClock"/ / set the email address, subject and text mailComposeVC. SetToRecipients (["<[email protected]>"])
    mailComposeVC.setSubject("OneScreen "+String(describing: appVersion)+"-"+NSLocalizedString("FeedBack Mail From", comment: "FeedBack Mail From") +""+deviceName)

    let content:String = \n \n \ Device: \(deviceName)\n System: \(systemVersion)\n App Version: \(String(describing: appVersion))" \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

}
Copy the code

You also need to add mail system prompts and mail delivery detection.

// The email system prompts funcshowSendMailErrorAlert() {

    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) {}} / / email test func mailComposeController (_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { switch result.rawValue {case MFMailComposeResult.cancelled.rawValue:
        print("Unsend")
    case MFMailComposeResult.sent.rawValue:
        print("Sent successfully")
    default:
        break
    }
    self.dismiss(animated: true, completion: nil)

}
Copy the code

Finally, in the place of calling email feedback, we need to judge whether it can be sent first. If it can not be sent, the user will be informed of the failure reason through a prompt message. If it can be sent, the sending window will be successfully retrieved. Where email feedback is required:

if MFMailComposeViewController.canSendMail() {// Note that this instance is written inifIn the block, there will be two pop-ups (one for the system) when you can't send an email otherwise.let mailComposeViewController = configuredMailComposeViewController()
    self.present(mailComposeViewController, animated: true, completion: nil)

} else {
    self.showSendMailErrorAlert()
}
Copy the code

3. System sharing function

Before sharing, we need to set up the information to share: title, picture, link.

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

Var is used to change their values in special cases, as follows:

let shareVC:UIActivityViewController = UIActivityViewController(activityItems: [self.urlTitle,self.urlImage,self.webUrl], applicationActivities: nil)

self.present(shareVC, animated: true, completion: {
    print("shareVC success")})Copy the code

Open some web sites

Opening the website can realize the functions of “official website” and “application update description”. Update description We can update the list of users at a high speed by updating the Web content. If your app needs more tutorials, you can also present them on the web. In order to facilitate user feedback, I usually add a micro blog entry so that users can quickly contact me with their micro blog address for feedback.

To do this, we need to create a Web page that hosts the Web content, so we need to add a Controller with a WebView first. When other pages open the Web, pass parameters to tell the WebView which url to render.

For example, in OneDay’s 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)

}
Copy the code

In the Settings page, we start to open the Web:

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

Copy the code

Pass the WebIndex to the WebViewController to determine the specific URL.

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

  }
}
Copy the code

In this way, all relevant urls are opened. There are actually some features and capabilities in the web page load page that will be detailed in the next article.

GitHub: OneSwift – iOS Tips Based On Swift

Weibo: xDEHANG