Last active
May 12, 2016 07:57
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 :
Installation :
Example Usage :
License
MIT - Copyright © 2016 ALEXIS AUBRY
Help