LoginSignup
7
8

More than 5 years have passed since last update.

SwiftでSafariのリーディングリストに追加

Posted at

Safariのリーディングリストは、後で見たいウェブページをリーディングリストに登録することで、ネット環境が無いオフラインでもiPhone内のストレージにキャッシュされて記事の続きを閲覧できる機能

今回はリストに追加する実装を記載

はじめに

Build PhasesのLink Binary With Librariesに
iOS7から追加されたSafari Service Frameworkを追加

実装

import SafariServices

let title = "my profile site"
let url = NSURL(string: "http://manchan.github.io")

// リーディングリストに追加できるかどうかチェック
let isValid = SSReadingList.supportsURL(url)
let list = SSReadingList.defaultReadingList()
var msg:String?

// 追加
if isValid && list.addReadingListItemWithURL(url, title: title, previewText: nil, error: nil){
    msg = "追加に成功"
}else{
    msg = "追加に失敗"
}

// 確認       
UIAlertView(title: "追加test", message: msg, delegate: nil, cancelButtonTitle: "OK").show()

用例

AppleWatchからニュースの記事見出しだけ見て、親iPhoneのsafariに追加するなどが考えられる実装(Google News, NYTimes)
watchOS1ならopenParentApplicationで送信
watchOS2ならWatch Connectivityで

注意

追加済みのURLとかのチェックはできない、また既出のリストも取得、編集、削除できない
読みだすことができれば、いろいろとできそうなので残念。
オフライン閲覧が可能なので、ストレージにキャッシュが貯まるので膨大にある場合、キャッシュを削除する必要がある
iOS9であれば設定から
一般 > ストレージとiCloudの使用状況 > ストレージを管理 > Safari
のリーディングリストを編集を押して削除

参考

iOS Developer Library > Safari Services Framework Reference SSReadingList Class Reference
https://developer.apple.com/library/prerelease/ios/documentation/UserExperience/Reference/SSReadingList_Ref/index.html#//apple_ref/occ/cl/SSReadingList

7
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
8