CocoaPods trunk is moving to be read-only. Read more on the blog, there are 17 months to go.

CSafariWebKit 2.0.1

CSafariWebKit 2.0.1

Maintained by Douglas Mandarino.



  • By
  • Douglas Mandarino

CSafariWebKit

A framework to browser web from your app and easily configured it.

Overview

It works as a wrapper for SFSafariViewController and WKWebView that helps with customizations, takes away the need to implement delegates and uncovers the intelligence of browsing the web from inside of your app.

Getting Started

Prerequisites

Your app's target should be at least iOS 9.0. And if you are not cloning it, then you will need CocoaPods.

Installing

You can either clone it and add it as a Framework or you can use it as a dependency from CocoaPods

CocoaPods

Go to your Podfile and add:

pod `CSafariWebKit`

Then in your terminal run:

pod install

Now you can open YourApp.xcworkspace

How to use it

First import the Framework in a ViewController that you want to use it.

import CSafariWebKit

SFSafariViewController

You can either receive it as a UIViewController to be presented :

let vc = WebView.safari.load(url: url)
self.present(vc, animated: true, completion: nil)

Or you can present it directly from your ViewController

WebView.safari.present(url: url, from: self)

WebView.safari.present(url: url, from: self, whenDidClose: { doSomething() })

WebView.safari.present(url: url, from: self, whenDidClose: { doSomething() })

WebView.safari.present(url: url, from: self, dismissButtonStyle: .cancel,
                 whenDidClose: { doSomething() })
                 
WebView.safari.present(url: url, from: self, dismissButtonStyle: .cancel,
                 barTintColor: .blue, tintColor: .white, whenDidClose: { doSomething() })
                 
WebView.safari.present(url: url, from: self, dismissButtonStyle: .cancel,
                 presentModally: true, barCollapsingEnabled: true, entersReaderIfAvailable: true,
                 barTintColor: .blue, tintColor: .white, whenDidClose: { doSomething() })

Where:

  • url - The URL to be loaded.
  • previousViewController - The ViewController that will present the Browser.
  • dismissButtonStyle - Indicates which button should be shown in SafariViewController to close it.
  • presentModally - Indicates if SafariViewController should be presented modally.
  • barCollapsingEnabled - Should enable collapsing of the navigation bar and hiding of the bottom toolbar when the user scrolls web content.
  • entersReaderIfAvailable - Indicates if SafariViewController should automatically show the Reader version of web pages. This will only happen when Safari Reader is available on a web page.
  • barTintColor - The tint color to apply to the navigation bar background.
  • tintColor - The tint color to apply to the navigation items and bar button items.
  • whenDidClose - A method used as a callback when the user closes the Browser.

WKWebView

You can either receive it as a UIViewController to be presented :

let vc = WebView.webview.load(url: url)
self.present(vc, animated: true, completion: nil)

Or you can present it directly from your ViewController

WebView.wkWebView.present(url: url, from: self)

WebView.wkWebView.present(url: url, from: self, whenDidClose: { doSomething() })

WebView.wkWebView.present(url: url, from: self, whenDidClose: { doSomething() })

WebView.wkWebView.present(url: url, from: self, closeButtonName: "Close", buttonSide: .left, 
                  barTintColor: .blue, tintColor: .white, whenDidClose: { doSomething() })
                 
WebView.wkWebView.present(url: url, from: self, closeButtonName: "Close", buttonSide: .left,
                  barTintColor: .blue, tintColor: .white, title: "My Screen", whenDidClose: { doSomething() })
                 
WebView.wkWebView.present(url: url, from: self, closeButtonName: "Close", buttonSide: .left,
                  barTintColor: .blue, tintColor: .white, title: "My Screen", 
                  userAgent: userAgent, whenDidClose: { doSomething() })

Where:

  • url - The URL to be loaded.
  • previousViewController - The ViewController that will present the Browser.
  • closeButtonName - Set a custom name to use as dismiss button.
  • closeButtonName - Set a custom name to use as dismiss button.
  • buttonSide - Indicates wich side the button should be presented.
  • barTintColor - The tint color to apply to the navigation bar background.
  • tintColor - The tint color to apply to the navigation items and bar button items.
  • title - Set a title for NavigationBar.
  • userAgent - Append a UserAgent to your WebView.
  • whenDidClose - A method used as a callback when the user closes the Browser.

Framework

Clone this project, copy CSafariWebKit.framework to your project. Then and go to Xcode and follow these steps.

  1. Select the project file from the project navigator on the left side of the project window.
  2. Select the target for where you want to add frameworks in the project settings editor.
  3. Select the “Build Phases” tab, and click the small triangle next to “Link Binary With Libraries” to view all of the frameworks in your application.
  4. To Add frameworks, click the “+” below the list of frameworks.

Built With