@brucelay
I am posting back here as I want others to be able to use this if they want to.
Looking at the Xcode it is very basic.
I have change it a bit.
The main changes are how the Hype page operates with the App.
I have set it up so when the Hype mail button is clicked it sends the App the details.
The App then presents the in App mail composer.
I did it this way because although I got the original mailto open Url to work, it takes you out of your app and into to mail but once the mail is completed it does not take you back to your app.
this way you never leave your app.
The code in the View controller now looks like this.
//
// ViewController.swift
// speakNcomm
//
// Created by Mark Hunte 2018.
// Copyright © 2018 Mark Hunte All rights reserved.
//
import UIKit
import WebKit
import MessageUI
class ViewController: UIViewController, WKNavigationDelegate ,WKScriptMessageHandler, MFMailComposeViewControllerDelegate{
//-- declare a WKWebView
var webView: WKWebView!
//-- declare a mail vars
var addr :String = ""
var subj :String = ""
var body :String = ""
//-- webkit message handler delegate //-- handles messages from hype page
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
//-- messages from Hype page
if message.name == "address"{
addr = message.body as! String
}
if message.name == "subject"{
subj = message.body as! String
}
if message.name == "body"{
body = message.body as! String
}
if message.name == "send"{
//-- compose Mail in App view
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients([addr])
mail.setSubject(subj )
mail.setMessageBody(body , isHTML: true)
present(mail, animated: true, completion: nil)
} else {
// show failure alert
}
}
}
//-- mail didFinishWith delegate
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
//-- configure the WKWebView
//-- init config and controller
let wconfiguration = WKWebViewConfiguration()
let wcontroller = WKUserContentController()
//-- We must add the webkit scripts we expect to get from the Hype Page to the controller
wcontroller.add(self, name: "address")
wcontroller.add(self, name: "subject")
wcontroller.add(self, name: "body")
wcontroller.add(self, name: "send")
//-- attach controller, config to WKWebview
wconfiguration.userContentController = wcontroller;
webView = WKWebView(frame: .zero, configuration: wconfiguration)
let htmlPath = Bundle.main.path(forResource: "speakNcomm_iPad", ofType: "html")
let folderPath = Bundle.main.bundlePath
let baseUrl = URL(fileURLWithPath: folderPath, isDirectory: true)
do {
let htmlString = try NSString(contentsOfFile: htmlPath!, encoding: String.Encoding.utf8.rawValue)
webView.loadHTMLString(htmlString as String, baseURL: baseUrl)
} catch {
// catch error
}
webView.navigationDelegate = self
view = webView
}
}
The Hype Javascript also needs to change.
I did not have the original function but this should make sense in the parts you need to replace in your function.
var results =itemValues.toString().replace(",","<br>")
window.webkit.messageHandlers.address.postMessage(mailto_); // post the to address
window.webkit.messageHandlers.subject.postMessage("Report"); // post the subject
window.webkit.messageHandlers.body.postMessage(results); // post the result
window.webkit.messageHandlers.send.postMessage(true); // post the the send //-- which triggers the compose window in the App
–
I tested this on my iPhone and all works.
Note : you must use a correctly structured email address when testing or that field will be blank in the composer.
The mail controller validates it…
(The code is as is. It is only as sound as my research in to helping you getting something going goes. In other words it works but i have not put a lot of effort to check or track any bugs that may spring up.
You will need to put you error checks, bounds and catches in yourself if they are needed… )