{"id":22534,"date":"2015-11-05T12:02:41","date_gmt":"2015-11-05T20:02:41","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=22534"},"modified":"2015-11-05T12:02:41","modified_gmt":"2015-11-05T20:02:41","slug":"keep-users-engaged-with-ios-9s-sfsafariviewcontroller","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/keep-users-engaged-with-ios-9s-sfsafariviewcontroller\/","title":{"rendered":"Keep Users Engaged with iOS 9&#8217;s SFSafariViewController"},"content":{"rendered":"<p>\t\t\t\tAt one point or another, you&#8217;ve probably needed to show web content within your app.\n<img decoding=\"async\" class=\"wp-image-22686 alignright\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/Safari-493x1024.png\" alt=\"Safari\" width=\"200\" height=\"415\" \/>Before iOS 9, developers had two main options for accomplishing this: display the content in a UIWebView within the app, or launch Safari and navigate the user away from the app. Both of these options present unique problems from a user experience point of view.<\/p>\n<h2>Safari<\/h2>\n<p>Navigating users away from your app to Safari should generally always be avoided. Most users will not expect navigation outside of your application, so if you navigate away from your app, users may never\u00a0return it, essentially killing\u00a0engagement. If you require a user to interact with a web view and have to support iOS 8 or below, using a UIWebView should be your first choice.<\/p>\n<p>Apple has made some user experience improvements for apps launching Safari by allowing users to navigate back to your app using the status bar. This makes it easy to return to the original app, but is only applicable for users running iOS 9 and above.<\/p>\n<pre class=\"EnlighterJSRAW\"> UIApplication.SharedApplication.OpenUrl(url);\n<\/pre>\n<h2><img decoding=\"async\" class=\"alignright wp-image-22687\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/WebView-493x1024.png\" alt=\"WebView\" width=\"200\" height=\"415\" \/>UIWebView<\/h2>\n<p>UIWebView allows us to show users HTML content from within our apps. For apps that need to support iOS 8 or below, this is the preferred methodology for showing web content, such as your\u00a0app&#8217;s support page, to users. Users will not have to leave your application, though without some work on your end,\u00a0the UIWebView will not\u00a0support much of the functionality users expect of a web view, such as the ability to navigate backwards or to a specific page.<\/p>\n<p>In this situation, you would need to add a navigation bar with a UIWebViewDelegate to give the user much more control over the navigation stack, such as back, forward, stop, and reload. If you just have to support iOS 8 and above, you can use the WKWebView, which improves performance over the UIWebView, but still requires additional setup to give the user a pleasant navigation experience.<\/p>\n<pre class=\"EnlighterJSRAW\"> \nvar webview = new UIWebView(this.View.Bounds);\nwebview.LoadRequest(new NSUrlRequest(url));\nView.AddSubview(webview);\n<\/pre>\n<p>&nbsp;<\/p>\n<h2>SFSafariViewController<\/h2>\n<p>iOS 9 adds significant upgrades to our toolbox for displaying web content within native apps. SFSafariViewController\u00a0gives you a whole new way to display web content to users without having to navigate away from your application or roll your own complex web view. You can even customize the appearance and styling of SFSafariViewController to conform to your app&#8217;s existing design.\u00a0To get started, you&#8217;ll need to reference Safari Services.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-22684\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/ezgif.com-crop.gif\" alt=\"SafariVCGif\" width=\"195\" height=\"360\" \/><\/p>\n<pre class=\"EnlighterJSRAW\"> \nusing SafariServices;\n\n\/\/Standard iOS ViewController code....\n\nvar sfViewController = new SFSafariViewController (url);\nPresentViewControllerAsync (sfViewController, true);\n<\/pre>\n<p>In iOS 9 and above, Apple has taken steps to increase the level of security for all apps. This means you will likely need to tweak your app&#8217;s\u00a0<a href=\"https:\/\/developer.xamarin.com\/guides\/ios\/platform_features\/introduction_to_ios9\/ats\/\">transport security settings<\/a> in order to display remote content without issue.<\/p>\n<h2>Conclusion<\/h2>\n<p>SFSafariViewController is the preferred way to display web content to your users using iOS 9 or above. It drastically improves the user experience when the user has to interact with content on the web, all while keeping users from\u00a0leaving the original app. If your apps are displaying web content and support iOS 9, download our <a href=\"https:\/\/github.com\/MikeCodesDotNet\/WebViews\">web view<\/a> sample and update them today.\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>At one point or another, you&#8217;ve probably needed to show web content within your app. Before iOS 9, developers had two main options for accomplishing this: display the content in a UIWebView within the app, or launch Safari and navigate the user away from the app. Both of these options present unique problems from a [&hellip;]<\/p>\n","protected":false},"author":1929,"featured_media":22686,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[6,4],"class_list":["post-22534","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-ios","tag-xamarin-platform"],"acf":[],"blog_post_summary":"<p>At one point or another, you&#8217;ve probably needed to show web content within your app. Before iOS 9, developers had two main options for accomplishing this: display the content in a UIWebView within the app, or launch Safari and navigate the user away from the app. Both of these options present unique problems from a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/22534","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/1929"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=22534"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/22534\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=22534"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=22534"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=22534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}