Unit tests with Rocket.rs - unit-testing
Im trying to write some unit tests for the 3 routes of my Rocket app. So far the get index function works fine but the two post requests arent working
Im getting confused because i keep getting a 404 request and i don't really know why
tests.rs:
use super::rocket;
use rocket::local::blocking::Client;
use rocket::http::Status;
use rocket::uri;
#[test]
fn index() {
let client = Client::tracked(rocket()).expect("valid rocket instance");
let response = client.get(uri!(super::index)).dispatch();
assert_eq!(response.status(), Status::Ok);
}
#[test]
fn add() {
let client = Client::tracked(rocket()).expect("valid rocket instance");
let response = client.post(uri!(super::add))
.body("note=This is a test")
.dispatch();
assert_eq!(response.status(), Status::Ok);
}
#[test]
fn delete() {
let client = Client::tracked(rocket()).expect("valid rocket instance");
let response = client.post(uri!(super::delete))
.body("noteid=1")
.dispatch();
assert_eq!(response.status(), Status::Ok);
}
main.rs (some code omitted for space reasons):
#[cfg(test)] mod tests;
use curl::easy::{Easy, List};
use rocket::form::Form;
use rocket::response::content::RawHtml;
use rocket::response::Redirect;
use rocket::{response::content::RawCss, *};
use rocket_dyn_templates::tera;
use rusqlite::{Connection, Result};
use std::io::Read;
#[derive(FromForm)]
struct NoteForm {
note: String,
}
#[derive(Debug)]
struct Note {
id: i32,
note: String,
}
#[derive(FromForm)]
struct NoteID {
noteid: i32,
}
#[get("/")]
fn index() -> RawHtml<String> {
let mut html: String = r#"
<link rel="stylesheet" href="style.css">
<h1>Rust Notebook</h1>
<form method='POST' action='/add'>
<label>Note: <input name='note' value=''></label>
<button>Add</button>
</form>
<ul class='notes'>"#
.to_owned();
let notes = get_notes().unwrap();
for note in notes {
let noteid: String = note.id.to_string();
html += "<li class='notes'>";
html += &tera::escape_html(¬e.note);
html += "<form method='POST' action='/delete'> <button name='noteid' value='";
html += ¬eid;
html += "' style='float: right;'>Delete</button></form></li>";
}
html += "</ul>";
RawHtml(html)
}
#[post("/delete", data = "<noteid>")]
fn delete(noteid: Form<NoteID>) -> Redirect {
let conn = Connection::open("notes.db").unwrap();
conn.execute(
"DELETE FROM notes WHERE rowid = ?",
&[noteid.noteid.to_string().as_str()],
)
.unwrap();
Redirect::to("/")
}
#[post("/add", data = "<note>")]
fn add(note: Form<NoteForm>) -> Redirect {
let conn = Connection::open("notes.db").unwrap();
conn.execute("INSERT INTO notes (note) VALUES (?)", &[note.note.as_str()])
.unwrap();
log_notes(¬e.note.as_str());
Redirect::to("/")
}
#[launch]
fn rocket() -> _ {
sqlite();
rocket::build().mount("/", routes![index, add, serve_css, delete])
}
any help would be great :)
Related
How to test Contacts Framework
hi i want to test CNContacts Store since this is my first time doing test, i don't have any idea how to conduct a unit test. This the code i want to test. private func fetchContacts() { var contacts: [Contact] = [] let keys: [CNKeyDescriptor] = [CNContactFormatter.descriptorForRequiredKeys(for: .fullName), CNContactPhoneNumbersKey as CNKeyDescriptor] let request = CNContactFetchRequest(keysToFetch: keys) do { try contactStore.enumerateContacts(with: request) { (contact, stop) in let name: String = CNContactFormatter.string(from: contact, style: .fullName) ?? contact.nickname contacts.append(contentsOf: contact.phoneNumbers.compactMap({ phoneNumber in let phoneNumberString: String = phoneNumber.value.stringValue return .init(name: name, phoneNumber: phoneNumberString) })) } allContacts = contacts isContactsFetched = true filterContacts() } catch { print("unable to fetch contacts") } } I'm using sourcery to generate mock from CNContactStore this is the enumerated mock i generate using sorcery //MARK: - enumerateContacts var enumerateContactsWithUsingBlockThrowableError: Error? var enumerateContactsWithUsingBlockCallsCount = 0 var enumerateContactsWithUsingBlockCalled: Bool { return enumerateContactsWithUsingBlockCallsCount > 0 } var enumerateContactsWithUsingBlockReceivedArguments: (fetchRequest: CNContactFetchRequest, block: (CNContact, UnsafeMutablePointer<ObjCBool>) -> Void)? var enumerateContactsWithUsingBlockReceivedInvocations: [(fetchRequest: CNContactFetchRequest, block: (CNContact, UnsafeMutablePointer<ObjCBool>) -> Void)] = [] var enumerateContactsWithUsingBlockClosure: ((CNContactFetchRequest, #escaping (CNContact, UnsafeMutablePointer<ObjCBool>) -> Void) throws -> Void)? func enumerateContacts(with fetchRequest: CNContactFetchRequest, usingBlock block: #escaping (CNContact, UnsafeMutablePointer<ObjCBool>) -> Void) throws { if let error = enumerateContactsWithUsingBlockThrowableError { throw error } enumerateContactsWithUsingBlockCallsCount += 1 enumerateContactsWithUsingBlockReceivedArguments = (fetchRequest: fetchRequest, block: block) enumerateContactsWithUsingBlockReceivedInvocations.append((fetchRequest: fetchRequest, block: block)) try enumerateContactsWithUsingBlockClosure?(fetchRequest, block) } what i did so far for unit test is this it("should fetch contacts") { let contact = CNContact() let stop = UnsafeMutablePointer<ObjCBool>.allocate(capacity: 1) stop[0] = true // When viewModel.onViewDidAppear() // Then mockContactStore.enumerateContactsWithUsingBlockClosure = { (_, args) in args(contact, stop) expect(mockContactStore.enumerateContactsWithUsingBlockCallsCount).to(equal(1)) } } Please help
if you want to test this -> let request = CNContactFetchRequest(keysToFetch: keys) you can do like this -> protocol CNContactFetchRequestProtocol { } extension CNContactFetchRequest: CNContactFetchRequestProtocol { } let request: CNContactFetchRequestProtocol = CNContactFetchRequest(keysToFetch: keys) and finally you can create mock class MockContact: CNContactFetchRequestProtocol { } and then you can tests like this: let request: CNContactFetchRequestProtocol = MockContact()
Rust Unit Test - Function call hangs at the very end and does not return
I am writing some unit tests for my Rust http server handlers. But when I am running one of the tests it get stuck at the end of the inner function. Here is relevant part of the code: async fn generate(request: Request<Body>) -> Result<Response<Body>, hyper::Error> { let result = process_request(request).await; println!("This message doesn't get printed!!"); let (spec, override) = match result { Ok((s, o)) => (s, o), Err(process_error) => { return Ok(Response::new(Body::from(format!("{}", process_error)))); }, }; ... Ok(Response::new(Body::from(format!("{}", response)))) } async fn process_request(request: Request<Body>) -> Result<(Spec, Option<Config>), Error> { let body = body::to_bytes(request.into_body()).await; let payload: Payload = serde_json::from_slice(&body.unwrap().to_vec()).unwrap(); let spec_str = payload.spec.to_owned(); ... println!("Function runs to this point and prints this message"); Ok((spec, override)) } #[tokio::test] async fn test_gen() { let payload = Payload { spec: a_spec(), }; let payload_json = serde_json::to_string_pretty(&payload).unwrap(); let request = Request::builder().body(Body::from(payload_json)); let result = generate(request.unwrap()).await.unwrap(); // Some asserts ... } I am wondering what I am doing wrong?
Looks like the inner function starts another thread, so the solution was to decorate the test with: #[tokio::test(flavor = "multi_thread", worker_threads = 2)] This resolved the issue for my unit tests.
Holoview chart won't appear in Django site
i know there is probably something simple i am doing wrong, but i don't know where else to get an answer. I created a django site and the following function returns holoview html: from django.shortcuts import render from django.http import HttpResponseRedirect from charts.models import Ord from IPython.display import display_html import pandas as pd import holoviews as hv hv.extension('bokeh') renderer = hv.renderer('bokeh') # Create your views here. def displayChart(request): df = pd.DataFrame(list(Ord.objects.using('DB').all().values('ordtyp')[:500])) df = df.groupby([df.ordtyp]).size().reset_index(name='counts') bars = hv.Bars(df, kdims=[('ordtyp', 'Order Type')], vdims=[('counts', 'Count of Orders')]) hv.Store.registry['bokeh'][hv.Bars] html = renderer.html(bars) return render(request, 'charts/charts.html', {'html': html}) i put a block in the charts.html file as: {{ html |safe }} and all i get is a blank page. i then took the raw html that the renderer is returning and tried to copy and paste it directly into my html file, and got the same thing. the html is below. Also, the chart does work in Jupyter Notebook... can you tell me what i am doing wrong? charts.html: > <!DOCTYPE html> > <html> > <head> > <title>Charts</title> > </head> > <body> > {{html|safe}} > </body> > </html> raw html that the renderer returned: <div style='display: table; margin: 0 auto;'> <div class="bk-root"> <div class="bk-plotdiv" id="0dd69ef6-4d30-48f5-a95a-1201437920de"></div> </div> <script type="text/javascript"> (function(root) { function now() { return new Date(); } var force = false; if (typeof (root._bokeh_onload_callbacks) === "undefined" || force === true) { root._bokeh_onload_callbacks = []; root._bokeh_is_loading = undefined; } if (typeof (root._bokeh_timeout) === "undefined" || force === true) { root._bokeh_timeout = Date.now() + 0; root._bokeh_failed_load = false; } var NB_LOAD_WARNING = {'data': {'text/html': "<div style='background-color: #fdd'>\n"+ "<p>\n"+ "BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \n"+ "may be due to a slow or bad network connection. Possible fixes:\n"+ "</p>\n"+ "<ul>\n"+ "<li>re-rerun `output_notebook()` to attempt to load from CDN again, or</li>\n"+ "<li>use INLINE resources instead, as so:</li>\n"+ "</ul>\n"+ "<code>\n"+ "from bokeh.resources import INLINE\n"+ "output_notebook(resources=INLINE)\n"+ "</code>\n"+ "</div>"}}; function display_loaded() { if (root.Bokeh !== undefined) { var el = document.getElementById("0dd69ef6-4d30-48f5-a95a-1201437920de"); if (el != null) { el.textContent = "BokehJS " + Bokeh.version + " successfully loaded."; } } else if (Date.now() < root._bokeh_timeout) { setTimeout(display_loaded, 100) } } function run_callbacks() { try { root._bokeh_onload_callbacks.forEach(function(callback) { callback() }); } finally { delete root._bokeh_onload_callbacks } console.info("Bokeh: all callbacks have finished"); } function load_libs(js_urls, callback) { root._bokeh_onload_callbacks.push(callback); if (root._bokeh_is_loading > 0) { console.log("Bokeh: BokehJS is being loaded, scheduling callback at", now()); return null; } if (js_urls == null || js_urls.length === 0) { run_callbacks(); return null; } console.log("Bokeh: BokehJS not loaded, scheduling load and callback at", now()); root._bokeh_is_loading = js_urls.length; for (var i = 0; i < js_urls.length; i++) { var url = js_urls[i]; var s = document.createElement('script'); s.src = url; s.async = false; s.onreadystatechange = s.onload = function() { root._bokeh_is_loading--; if (root._bokeh_is_loading === 0) { console.log("Bokeh: all BokehJS libraries loaded"); run_callbacks() } }; s.onerror = function() { console.warn("failed to load library " + url); }; console.log("Bokeh: injecting script tag for BokehJS library: ", url); document.getElementsByTagName("head")[0].appendChild(s); } };var element = document.getElementById("0dd69ef6-4d30-48f5-a95a-1201437920de"); if (element == null) { console.log("Bokeh: ERROR: autoload.js configured with elementid '0dd69ef6-4d30-48f5-a95a-1201437920de' but no matching script tag was found. ") return false; } var js_urls = []; var inline_js = [ function(Bokeh) { (function() { var fn = function() { var docs_json = {"c7d02456-4a61-46d2-8431-34face1e6c67":{"roots":{"references":[{"attributes":{"callback":null,"column_names":["ORDTYP","counts"],"data":{"ORDTYP":["CC","CD","CZ","DB","DR","ED","EI","IC","ID","IP","MC","MF","MI","MK","MP","MS","MX","PC","PM","PT","SD","TI","TX","ZLR"],"counts":[60,3,2,17,1,13,2,28,21,1,3,6,1,2,2,35,10,20,12,2,525,7,225,1]}},"id":"46c2f30a-31d4-4c3f-ade2-4de96e13a4db","type":"ColumnDataSource"},{"attributes":{"active_drag":"auto","active_inspect":"auto","active_scroll":"auto","active_tap":"auto","tools":[{"id":"16eb22e8-c448-49d7-ab20-a02f2ebb3e5c","type":"SaveTool"},{"id":"218dbf74-8190-491c-b2dc-d13097c1f9e4","type":"PanTool"},{"id":"03f67199-1622-4784-b9a2-b36b4a47477b","type":"WheelZoomTool"},{"id":"d48948b1-297b-4915-bf84-916a417b01ee","type":"BoxZoomTool"},{"id":"722ae387-76c5-48cb-a387-7090de91b014","type":"ResetTool"}]},"id":"8ce67c3d-e8dc-41a2-86a8-76685fb90fe9","type":"Toolbar"},{"attributes":{"callback":null,"end":525,"start":0},"id":"16f5dfe6-08a6-4ad8-a190-bdf67979b26c","type":"DataRange1d"},{"attributes":{},"id":"bfe7ff64-0995-43a7-8944-6d491bf93b14","type":"CategoricalTicker"},{"attributes":{"callback":null,"factors":["CC","CD","CZ","DB","DR","ED","EI","IC","ID","IP","MC","MF","MI","MK","MP","MS","MX","PC","PM","PT","SD","TI","TX","ZLR"]},"id":"36f2f454-71a9-4511-a446-2c56a015ecd0","type":"FactorRange"},{"attributes":{"axis_label":"Count of Orders","formatter":{"id":"7303c9ca-b3e1-40be-bd44-1ef7f06d8a2d","type":"BasicTickFormatter"},"plot":{"id":"c84e964a-0b0b-4d95-aa2c-3657b7811bd0","subtype":"Figure","type":"Plot"},"ticker":{"id":"95b801e5-1f32-4ff1-be0c-e64ddcb14fa3","type":"BasicTicker"}},"id":"bd1e5045-1d72-4e92-8914-13be1ee0d04f","type":"LinearAxis"},{"attributes":{},"id":"95b801e5-1f32-4ff1-be0c-e64ddcb14fa3","type":"BasicTicker"},{"attributes":{"grid_line_color":{"value":null},"plot":{"id":"c84e964a-0b0b-4d95-aa2c-3657b7811bd0","subtype":"Figure","type":"Plot"},"ticker":{"id":"bfe7ff64-0995-43a7-8944-6d491bf93b14","type":"CategoricalTicker"}},"id":"0dee56d3-b965-4d15-bf6d-93cd1ea2a20d","type":"Grid"},{"attributes":{"bottom_units":"screen","fill_alpha":{"value":0.5},"fill_color":{"value":"lightgrey"},"left_units":"screen","level":"overlay","line_alpha":{"value":1.0},"line_color":{"value":"black"},"line_dash":[4,4],"line_width":{"value":2},"plot":null,"render_mode":"css","right_units":"screen","top_units":"screen"},"id":"2a376fc3-0bcc-47df-a0e8-2389040d3254","type":"BoxAnnotation"},{"attributes":{"plot":null,"text":"","text_color":{"value":"black"},"text_font_size":{"value":"12pt"}},"id":"04310042-3ae0-4955-bd48-3d9c8e2d9be2","type":"Title"},{"attributes":{},"id":"722ae387-76c5-48cb-a387-7090de91b014","type":"ResetTool"},{"attributes":{"dimension":1,"grid_line_color":{"value":null},"plot":{"id":"c84e964a-0b0b-4d95-aa2c-3657b7811bd0","subtype":"Figure","type":"Plot"},"ticker":{"id":"95b801e5-1f32-4ff1-be0c-e64ddcb14fa3","type":"BasicTicker"}},"id":"a281b265-486f-42a3-829a-c71bfd6830d0","type":"Grid"},{"attributes":{"axis_label":"Order Type","formatter":{"id":"259e4d35-20fb-4eb4-9fbd-6068fef93c0d","type":"CategoricalTickFormatter"},"plot":{"id":"c84e964a-0b0b-4d95-aa2c-3657b7811bd0","subtype":"Figure","type":"Plot"},"ticker":{"id":"bfe7ff64-0995-43a7-8944-6d491bf93b14","type":"CategoricalTicker"}},"id":"9f673b5b-85bf-40d1-96f3-b27737ecc242","type":"CategoricalAxis"},{"attributes":{},"id":"218dbf74-8190-491c-b2dc-d13097c1f9e4","type":"PanTool"},{"attributes":{},"id":"259e4d35-20fb-4eb4-9fbd-6068fef93c0d","type":"CategoricalTickFormatter"},{"attributes":{},"id":"16eb22e8-c448-49d7-ab20-a02f2ebb3e5c","type":"SaveTool"},{"attributes":{},"id":"721998c2-dee0-4e47-9a6d-ecef76d53889","type":"LinearScale"},{"attributes":{},"id":"03f67199-1622-4784-b9a2-b36b4a47477b","type":"WheelZoomTool"},{"attributes":{"overlay":{"id":"2a376fc3-0bcc-47df-a0e8-2389040d3254","type":"BoxAnnotation"}},"id":"d48948b1-297b-4915-bf84-916a417b01ee","type":"BoxZoomTool"},{"attributes":{"data_source":{"id":"46c2f30a-31d4-4c3f-ade2-4de96e13a4db","type":"ColumnDataSource"},"glyph":{"id":"3ab94627-160a-4913-95e6-c25fa17a1d51","type":"VBar"},"hover_glyph":null,"muted_glyph":null,"nonselection_glyph":{"id":"f72f6dc1-7c71-4f5e-80f9-fbd1351268b3","type":"VBar"},"selection_glyph":null,"view":{"id":"9df174a0-4967-47fd-82f3-3d054eee1a12","type":"CDSView"}},"id":"897248ec-4fbd-404c-b3b3-09900b6e3560","type":"GlyphRenderer"},{"attributes":{"fill_alpha":{"value":0.1},"fill_color":{"value":"#30a2da"},"line_alpha":{"value":0.1},"line_color":{"value":"#000000"},"top":{"field":"counts"},"width":{"value":0.8},"x":{"field":"ORDTYP"}},"id":"f72f6dc1-7c71-4f5e-80f9-fbd1351268b3","type":"VBar"},{"attributes":{"fill_color":{"value":"#30a2da"},"line_color":{"value":"#000000"},"top":{"field":"counts"},"width":{"value":0.8},"x":{"field":"ORDTYP"}},"id":"3ab94627-160a-4913-95e6-c25fa17a1d51","type":"VBar"},{"attributes":{"source":{"id":"46c2f30a-31d4-4c3f-ade2-4de96e13a4db","type":"ColumnDataSource"}},"id":"9df174a0-4967-47fd-82f3-3d054eee1a12","type":"CDSView"},{"attributes":{},"id":"eb1872c1-4fa5-47b0-b5c7-43dfe9b890e3","type":"CategoricalScale"},{"attributes":{},"id":"7303c9ca-b3e1-40be-bd44-1ef7f06d8a2d","type":"BasicTickFormatter"},{"attributes":{"background_fill_color":{"value":"white"},"below":[{"id":"9f673b5b-85bf-40d1-96f3-b27737ecc242","type":"CategoricalAxis"}],"left":[{"id":"bd1e5045-1d72-4e92-8914-13be1ee0d04f","type":"LinearAxis"}],"min_border_bottom":10,"min_border_left":10,"min_border_right":10,"min_border_top":10,"plot_height":300,"plot_width":300,"renderers":[{"id":"9f673b5b-85bf-40d1-96f3-b27737ecc242","type":"CategoricalAxis"},{"id":"0dee56d3-b965-4d15-bf6d-93cd1ea2a20d","type":"Grid"},{"id":"bd1e5045-1d72-4e92-8914-13be1ee0d04f","type":"LinearAxis"},{"id":"a281b265-486f-42a3-829a-c71bfd6830d0","type":"Grid"},{"id":"2a376fc3-0bcc-47df-a0e8-2389040d3254","type":"BoxAnnotation"},{"id":"897248ec-4fbd-404c-b3b3-09900b6e3560","type":"GlyphRenderer"}],"title":{"id":"04310042-3ae0-4955-bd48-3d9c8e2d9be2","type":"Title"},"toolbar":{"id":"8ce67c3d-e8dc-41a2-86a8-76685fb90fe9","type":"Toolbar"},"x_range":{"id":"36f2f454-71a9-4511-a446-2c56a015ecd0","type":"FactorRange"},"x_scale":{"id":"eb1872c1-4fa5-47b0-b5c7-43dfe9b890e3","type":"CategoricalScale"},"y_range":{"id":"16f5dfe6-08a6-4ad8-a190-bdf67979b26c","type":"DataRange1d"},"y_scale":{"id":"721998c2-dee0-4e47-9a6d-ecef76d53889","type":"LinearScale"}},"id":"c84e964a-0b0b-4d95-aa2c-3657b7811bd0","subtype":"Figure","type":"Plot"}],"root_ids":["c84e964a-0b0b-4d95-aa2c-3657b7811bd0"]},"title":"Bokeh Application","version":"0.12.7"}}; var render_items = [{"docid":"c7d02456-4a61-46d2-8431-34face1e6c67","elementid":"0dd69ef6-4d30-48f5-a95a-1201437920de","modelid":"c84e964a-0b0b-4d95-aa2c-3657b7811bd0"}]; Bokeh.embed.embed_items(docs_json, render_items); }; if (document.readyState != "loading") fn(); else document.addEventListener("DOMContentLoaded", fn); })(); }, function(Bokeh) { } ]; function run_inline_js() { if ((root.Bokeh !== undefined) || (force === true)) { for (var i = 0; i < inline_js.length; i++) { inline_js[i].call(root, root.Bokeh); }if (force === true) { display_loaded(); }} else if (Date.now() < root._bokeh_timeout) { setTimeout(run_inline_js, 100); } else if (!root._bokeh_failed_load) { console.log("Bokeh: BokehJS failed to load within specified timeout."); root._bokeh_failed_load = true; } else if (force !== true) { var cell = $(document.getElementById("0dd69ef6-4d30-48f5-a95a-1201437920de")).parents('.cell').data().cell; cell.output_area.append_execute_result(NB_LOAD_WARNING) } } if (root._bokeh_is_loading === 0) { console.log("Bokeh: BokehJS loaded, going straight to plotting"); run_inline_js(); } else { load_libs(js_urls, function() { console.log("Bokeh: BokehJS plotting callback run at", now()); run_inline_js(); }); } }(window)); </script></div> THANKS!
You are missing the JS and CSS that's required to render this output. You can either manually include BokehJS as a script tag, e.g. for bokeh 0.12.9 you'd add this: <link href="http://cdn.bokeh.org/bokeh/release/bokeh-0.12.9.min.css" rel="stylesheet" type="text/css"> <link href="http://cdn.bokeh.org/bokeh/release/bokeh-widgets-0.12.9.min.css" rel="stylesheet" type="text/css"> <script src="http://cdn.bokeh.org/bokeh/release/bokeh-0.12.9.min.js"></script> <script src="http://cdn.bokeh.org/bokeh/release/bokeh-widgets-0.12.6.min.js"></script> Alternatively you can also use the renderer.static_html method to export a div which includes all the required JS and CSS, e.g.: hmap = hv.HoloMap({i: hv.Curve(np.random.rand(10)*i) for i in range(1,5)}) html = hv.renderer('bokeh').static_html(hmap) with open('test.html', 'w') as f: f.write(html) The static_html method also accepts a template to embed the JS, CSS and HTML separately: <html> <head> {css} {js} </head> <body> {html} </body> </html> In future releases we will also have a components method letting you get the JS, CSS and HTML components separately.
UIWebView: ics and vcard-Links not handled
I do have a UIWebView included where a public URL is loaded; unfortunately, vcard and ical-Links are not handled, i.e. nothing happens when I click on them. I tried to set all data detectors, no luck unfortunately. In the Xcode-log, I get this here when clicking on such a link: 2017-07-14 13:43:00.982413+0200 xxx[2208:967973] WF: _userSettingsForUser mobile: { filterBlacklist = ( ); filterWhitelist = ( ); restrictWeb = 1; useContentFilter = 0; useContentFilterOverrides = 0; whitelistEnabled = 0; } In Safari, the same stuff works as expected. If I use UIApplication.shared.openURL(icsOrVcardUrl) Safari gets opened and from there everything works as expected again, but I don't want the user to leave the app... EDIT This doesn't work either: func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool { if let url = request.url { if url.absoluteString.contains("=vcard&") || url.absoluteString.contains("/ical/") { let sessionConfig = URLSessionConfiguration.default let session = URLSession(configuration: sessionConfig) let request = URLRequest(url:url) let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in if let tempLocalUrl = tempLocalUrl, error == nil { DispatchQueue.main.async { self.documentController.url = tempLocalUrl self.documentController.presentPreview(animated: true) } } } task.resume() return false } } return true }
Use a UIDocumentInteractionController to preview without leaving your app. I tested it quickly with an .ics file and it works fine. Implement the UIDocumentInteractionControllerDelegate protocol extension MainViewController: UIDocumentInteractionControllerDelegate { func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController { return self; } } Create an instance of the interaction controller: let documentController = UIDocumentInteractionController() Intercept the clicks in your UIWebView in shouldStartLoadWithRequest, return false for links you want to handle with the in-app preview and true for all the rest. And finally: func previewDocument(_ url: URL) { documentController.url = url documentController.presentPreview(animated: true) } Here it is in the simulator EDIT: In response to the comment to this answer: The reason it doesn't work for you is because the UIDocumentInteractionController depends on the file extension. The extension of the temp file is .tmp Renaming the file after the download solves the problem. Quick and dirty example: let task = session.downloadTask(with: url!) { (tempLocalUrl, response, error) in if let tempLocalUrl = tempLocalUrl, error == nil { do { let filemgr = FileManager.default let newUrl = tempLocalUrl.appendingPathExtension("ics") try filemgr.moveItem(at: tempLocalUrl, to: newUrl) DispatchQueue.main.async { self.documentController.url = newUrl self.documentController.presentPreview(animated: true) } } catch let error { print("Error!!!: \(error.localizedDescription)") } } } task.resume() In this case it is advisable to clean after yourself, because the file won't be deleted after the task completes although the OS will delete it eventually, when space is needed. If you often access the same urls, Library/Caches/ may be a better place for this files, just come up with good naming schema, and check if the file doesn't exist already.
Load PHP file with document.createElement()
How could I make this work? I want to load a php file like this: Click button. Call Javascript function. In Javascript function create an img with src file.php. This should force the loading of the php. Here is the code. <script type="text/javascript"> var d; function callSave() { alert ('calling'); if (d) document.body.removeChild(d); // d = document.createElement("script"); d = document.createElement("img"); d.src = "savepages.php"; //d.type = "text/javascript"; document.body.appendChild(d); } </script> Then in savepages.php I do another alert to verify that the php is called and it isn't. Here is the savepages.php. <?php echo "alert('from the php');"; ?> The alert from the php doesn't happen. Is there a different element type that will force loading of the php? I don't have ajax installed, so I need a workaround like this. Thanks.
You could use an iframe element <script type="text/javascript"> var d; function callSave() { alert ('calling'); if (d) document.body.removeChild(d); d = document.createElement("iframe"); d.src = "savepages.php"; document.body.appendChild(d); } </script>
Found out the better way to handle this. There is this simple code that explains how to call a javascript function from a form event and from that javascript function load a PHP file. The code found at http://daniel.lorch.cc/docs/ajax_simple/ is also given here: <script type="text/javascript"> var http = false; if(navigator.appName == "Microsoft Internet Explorer") { http = new ActiveXObject("Microsoft.XMLHTTP"); } else { http = new XMLHttpRequest(); } function validate(user) { http.abort(); http.open("GET", "validate.php?name=" + user, true); http.onreadystatechange=function() { if(http.readyState == 4) { document.getElementById('msg').innerHTML = http.responseText; } } http.send(null); } </script> <h1>Please choose your username:</h1> <form> <input type="text" onkeyup="validate(this.value)" /> <div id="msg"></div> </form> validate.php <?php function validate($name) { if($name == '') { return ''; } if(strlen($name) < 3) { return "<span id=\"warn\">Username too short</span>\n"; } switch($name) { case 'bob': case 'jim': case 'joe': case 'carol': return "<span id=\"warn\">Username already taken</span>\n"; } return "<span id=\"notice\">Username ok!</span>\n"; } echo validate(trim($_REQUEST['name'])); ?>