Skip to content

Instantly share code, notes, and snippets.

@alexisakers
Last active May 12, 2016 07:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexisakers/29e87c2ac1cd3045ac9d49fc95c00eb4 to your computer and use it in GitHub Desktop.
Save alexisakers/29e87c2ac1cd3045ac9d49fc95c00eb4 to your computer and use it in GitHub Desktop.
An easy and safe way to add URLs to the Safari Reading List on iOS with Swift.
//
// SSReadingListItem.swift
// Created by Alexis Aubry on 09/05/2016.
//
// The MIT License (MIT)
// Copyright (c) 2016 ALEXIS AUBRY
//
import Foundation
import SafariServices
/// An object representing an item to add to the Safari reading list.
public struct SSReadingListItem {
// MARK: - Item Properties
/// The URL of the item.
public var url: NSURL
/// The optional title of the item displayed in Safari.
public var title: String?
/// The optional preview text of the item displayed in Safari.
public var previewText: String?
// MARK: - Lifecycle
/**
Creates a new reading list item.
- Parameters:
- url: The URL of the item.
- title: An optional title for the item that is displayed in Safari.
- previewText: An optional preview text that user reads in Safari in the Reading List table.
- Throws:
`SSReadingListError.urlNotSupported` is the URL you provided is not supported by the reading list.
*/
public init(url: NSURL, title: String?, previewText: String?) throws {
guard SSReadingList.supportsURL(url) else {
throw SSReadingListError.urlNotSupported(url: url)
}
self.url = url
self.title = title
self.previewText = previewText
}
}
/// An enumeration of reading list errors
enum SSReadingListError: ErrorType, CustomStringConvertible {
/// The URL provided is not supported in the reading list.
case urlNotSupported(url: NSURL)
/// The reading list is not available.
case noReadingList
/// The URL scheme is not allowed.
case urlSchemeNotAllowed(scheme: String)
/// The description of the error.
var description: String {
switch self {
case .urlNotSupported(let url): return "The URL \(url) cannot be used in the Reading List."
case .noReadingList: return "The Safari Reading List is not available."
case .urlSchemeNotAllowed(let scheme): return "The scheme \(scheme) is not allowed for use in the Reading List."
}
}
}
extension SSReadingList {
/**
Add a reading list item to the reading list.
- Parameters:
- readingListItem: A reading list item.
- Throws:
A `SSReadingListError`.
*/
static func add(readingListItem readingListItem: SSReadingListItem) throws {
guard let readingList = self.defaultReadingList() else {
throw SSReadingListError.noReadingList
}
do {
try readingList.addReadingListItemWithURL(readingListItem.url, title: readingListItem.title, previewText: readingListItem.previewText)
} catch SSReadingListErrorCode.URLSchemeNotAllowed {
let scheme = readingListItem.url.scheme
throw SSReadingListError.urlSchemeNotAllowed(scheme: scheme)
}
}
}
@alexisakers
Copy link
Author

alexisakers commented May 9, 2016

SSReadingListItem.swift

Purpose :

This file defines a wrapper around the SSReadingList class that enables you to create API-safe items you can assume to be valid. Using this class also lets you make your safety checks all in one place, as seen on the example implementation below.

Requirements :

  • Swift 2
  • iOS 7.0

Installation :

  • Download and drag this file to your Xcode projects.
  • Copy/Paste also supported (be careful, requires highly developed computer science skills)

Example Usage :

import Foundation
import SafariServices

if let swiftWebsiteURL = NSURL(string: "https://www.swift.org") {

    do {

          let swiftReadingListItem = try SSReadingListItem(
               url: swiftWebsiteURL,
               title: "Swift",
               previewText: "Welcome to Swift.org. Swift is now open source! We are excited by this new chapter in the story of Swift. After Apple unveiled the Swift programming language, ..."
          )

          try SSReadingList.add(readingListItem: swiftReadingListItem)
          print("Added Reading List item")

    } catch let error as SSReadingListError {
          print(error.description)
    } catch {
          print(error)
    }

}

License

MIT - Copyright © 2016 ALEXIS AUBRY

Help

Tweet Me

Analytics

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment