URLEmbeddedView 自动缓存确认为开放图形协议的对象

URLEmbeddedView

平台
语言
迦太基兼容
版本
许可证

Features

  • [x] Simple interface for fetching Open Graph Data
  • [x] Be able to display Open Graph Data
  • [x] Automatically caching Open Graph Data
  • [x] Automatically caching Open Graph Image
  • [x] Tap handleable
  • [x] Clearable image cache
  • [x] Clearable data cache
  • [x] Support Swift3.2 (until 0.11.x)
  • [x] Supprot Swift4.x (until 0.17.1)
  • [x] Support Swift5 (since 0.18.0)
  • [x] Support Carthage since 0.11.1
  • [x] Support tvOS since 0.16.0
  • [x] Custom implementation of OGData cache

Usage

To run the example project, clone the repo, and run from the Example directory first.carthage update

let embeddedView = URLEmbeddedView()
embeddedView.loadURL(urlString)
Swift

Layouts

  • Default
  • No Image
  • No response

Customization

embeddedView.textProvider[.Title].font = .boldSystemFontOfSize(18)
embeddedView.textProvider[.Title].fontColor = .lightGrayColor()
embeddedView.textProvider[.Title].numberOfLines = 2
//You can use ".Title", ".Description", ".Domain" and ".NoDataTitle"
Swift

Data and Image Cache

You can get Open Graph Data with .OGDataProvider

OGDataProvider.shared.fetchOGData(urlString: String, completion: ((OpenGraph.Data, Error?) -> Void)? = nil) -> String?
OGDataProvider.shared.deleteOGData(urlString: String, completion: ((Error?) -> Void)? = nil)
OGDataProvider.shared.deleteOGData(_ ogData: OpenGraph.Data, completion: ((Error?) -> Void)? = nil)
Swift

You can configure time interval for next updating of OGData.
Default is 10 days.

OGDataProvider.shared.updateInterval = 10.days
Swift

You can get UIImage with .OGImageProvider

OGImageProvider.shared.loadImage(urlString: String, completion: ((UIImage?, Error?) -> Void)? = nil) -> NSURLSessionDataTask?
OGImageProvider.shared.clearMemoryCache()
OGImageProvider.shared.clearAllCache()
Swift

Custom Data Cache implementation

Default cache feature is using Core Data.
If you want to use other cache features, please implement cache manager with .
For example, URLEmbeddedView has that feature is not using cache.
If you want to use that feature, you can use like this.
OGDataCacheManagerProtocolOGDataNoCacheManager

OGDataProvider.shared.cacheManager = OGDataNoCacheManager()
Swift

You can implement custom cache feature and use it like this.

class MemoryCacheManager: OGDataCacheManagerProtocol {
    // implementation of required methods
}

OGDataProvider.shared.cacheManager = MemoryCacheManager()
Swift

OpenGraph.Data Properties

public let imageUrl: URL?
public let pageDescription: String?
public let pageTitle: String?
public let pageType: String?
public let siteName: String?
public let sourceUrl: URL?
public let url: URL?
Swift

OpenGraphDataDownloader

You can only use download feature of OGData with like this.OpenGraphDataDownloader

let urlString = ...
OpenGraphDataDownloader.shared.fetchOGData(urlString: urlString) { result in
    switch result {
    case let .success(data, isExpired):
        // do something
    case let .failure(error, isExpired):
        // do something
    }
}
Swift

If you use with , it is almost same process.OGDataProviderOGDataNoCacheManager

OGDataProvider.shared.cacheManager = OGDataNoCacheManager()
let urlString = ...
OGDataProvider.shared.fetchOGData(urlString: urlString) { ogData, error in
    // do something
}
Swift

Installation

CocoaPods

URLEmbeddedView is available through CocoaPods. To install
it, simply add the following line to your Podfile:

# Uncomment the next line to define a global platform for your project
# platform :ios, '8.0'

target 'Your Project Name' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  # Pods for URLEmbeddedViewSample
  pod "URLEmbeddedView"
end
Ruby

Carthage

If you’re using Carthage, simply add
NoticeObserveKit to your :
Cartfile

github "marty-suzuki/URLEmbeddedView"
Ruby

Use in Objective-C

#import <URLEmbeddedView/URLEmbeddedView-Swift.h>

- (void)viewDidLoad {
    [super viewDidLoad];
    URLEmbeddedView *embeddedView = [[URLEmbeddedView alloc] init];
    [self.view addSubView:embeddedView];
    [embeddedView loadURL:@"https://github.com/" completion:nil];
}

- (void)setUpdateInterval {
  [OGDataProvider sharedInstance].updateInterval = [NSNumber days:10];
}

- (void)fetchOpenGraphData {
  [[OGDataProvider sharedInstance] fetchOGDataWithUrlString:self.textView.text
                                            completion:^(OpenGraphData *data, NSError *error) {
      NSLog(@"OpenGraphData = %@", data);
  }];
}
Objective-c

Here is Objective-C sample.

Special Thanks

Requirements

  • Xcode 10.2 or greater
  • iOS 8.0 or greater
  • tvOS 10.0 or greater
  • UIKit
  • CoreData
  • CoreGraphics

Other

Author

Taiki Suzuki, s1180183@gmail.com

License

URLEmbeddedView is available under the MIT license. See the LICENSE file for more info.

GitHub

https://github.com/marty-suzuki/URLEmbeddedView