{"id":38327,"date":"2023-01-30T15:47:33","date_gmt":"2023-01-30T20:47:33","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=38327"},"modified":"2023-01-30T15:47:33","modified_gmt":"2023-01-30T20:47:33","slug":"using-javascript-in-a-swift-app","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2023\/01\/30\/using-javascript-in-a-swift-app\/","title":{"rendered":"Using JavaScript in a Swift App"},"content":{"rendered":"<p><a href=\"https:\/\/douglashill.co\/javascript-in-swift\/\">Douglas Hill<\/a>:<\/p>\n<blockquote cite=\"https:\/\/douglashill.co\/javascript-in-swift\/\"><p>If you&rsquo;re writing an iOS app using Swift and trying to solve a problem you&rsquo;re sure has been solved before, you may look for existing code that solves that problem. It&rsquo;s likely you&rsquo;ll first think of looking for open source code written in Swift[&#8230;] However, we don&rsquo;t need to limit ourselves to Swift. [&#8230;] In this article, we&rsquo;ll look at how to call JavaScript code from Swift using JavaScriptCore.<\/p><p>[&#8230;]<\/p><p>This sort of code is inviting code injection security vulnerabilities. Instead, we can set our input URL as a variable in the JavaScript environment and then reference it by name.<\/p><p><a href=\"https:\/\/developer.apple.com\/documentation\/javascriptcore\/jscontext\"><code>JSContext<\/code><\/a> lets us read variables (in Swift) from JavaScript using <code>objectForKeyedSubscript(_:)<\/code> and set variables using <code>setObject(_:forKeyedSubscript)<\/code>. Oddly, this API is nicer to use in Objective-C since these map to subscript syntax so you can read and set values like in a dictionary. Subscript syntax doesn&rsquo;t seem to work in Swift here.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Douglas Hill: If you&rsquo;re writing an iOS app using Swift and trying to solve a problem you&rsquo;re sure has been solved before, you may look for existing code that solves that problem. It&rsquo;s likely you&rsquo;ll first think of looking for open source code written in Swift[&#8230;] However, we don&rsquo;t need to limit ourselves to Swift. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2023-01-30T20:47:37Z","apple_news_api_id":"f751fcb8-a82e-43f3-9498-49d618265deb","apple_news_api_modified_at":"2023-01-30T20:47:37Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/A91H8uKguQ_OUmEnWGCZd6w","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[31,2185,346,30,2223,991,71,2231,901,328],"class_list":["post-38327","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-ios","tag-ios-16","tag-javascript","tag-mac","tag-macos-13-ventura","tag-open-source-software","tag-programming","tag-swift-charts","tag-swift-programming-language","tag-webkit"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/38327","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=38327"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/38327\/revisions"}],"predecessor-version":[{"id":38328,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/38327\/revisions\/38328"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=38327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=38327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=38327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}