SMLinkPreview
Generate data and views for URL/web link previews for iOS -- based on pluggable link preview API services
Install / Use
/learn @crspybits/SMLinkPreviewREADME
URL Link Preview
Features
The goals of this library are:
- To (a) separate the fetching or acquisition of link preview data from (b) the view or UI aspects of the link preview.
- To enable the app using the link preview data fetching to not have to disable Application Transport Security (ATS).
- To flexibly allow for different web-based API services that fetch link preview data.
The LinkPreview view adapts to different available data:
<p float="left"> <img src="https://github.com/crspybits/SMLinkPreview/blob/master/Docs/Images/LargeImage-OneLineTitle.png" width="200" title="Large Image One Line Title" /> <img src="https://github.com/crspybits/SMLinkPreview/blob/master/Docs/Images/LargeImage-TwoLineTitle.png" width="200" title="Large Image Two Line Title" /> <img src="https://github.com/crspybits/SMLinkPreview/blob/master/Docs/Images/Icon.png" width="200" title="Icon and URL" /> <img src="https://github.com/crspybits/SMLinkPreview/blob/master/Docs/Images/OnlyURL.png" width="200" title="Only URL" /> <img src="https://github.com/crspybits/SMLinkPreview/blob/master/Docs/Images/Icon-TwoLineTitle.png" width="200"title="Icon Two Line Title" /> </p>Example
To run the example project, clone the repo, and run pod install from the Example directory first.
How to use
- Set up plugin's that will do the fetching. You (only) have to have at least one of these:
PreviewManager.session.reset()
guard let requestKeyName = MicrosoftURLPreview.requestKeyName,
let microsoftKey = APIKey.getFromPlist(plistKeyName: "MicrosoftURLPreview", requestKeyName: requestKeyName, plistName: "APIKeys") else {
throw URLPreviewGeneratorError.failedToGetPlistValue
}
guard let msPreview = MicrosoftURLPreview(apiKey: microsoftKey) else {
throw URLPreviewGeneratorError.failedToInitializePlugin
}
guard let adaPreview = AdaSupportPreview(apiKey: nil) else {
throw URLPreviewGeneratorError.failedToInitializePlugin
}
guard let mPreview = MicrolinkPreview(apiKey: nil) else {
throw URLPreviewGeneratorError.failedToInitializePlugin
}
PreviewManager.session.add(source: msPreview)
PreviewManager.session.add(source: adaPreview)
PreviewManager.session.add(source: mPreview)
- Optionally add some filtering:
// I'm going to require that the linkData have at least some content
PreviewManager.session.linkDataFilter = { linkData in
return linkData.description != nil ||
linkData.icon != nil ||
linkData.image != nil
}
- Given that you have a URL that you want to generate a preview for:
PreviewManager.session.getLinkData(url: url) { linkData in
logger.debug("linkData: \(String(describing: linkData))")
completion(linkData)
}
- Use this resulting
linkDatato render a preview:
let preview = LinkPreview.create(with: linkData) { loadedImage in
// This is optional. Only needed if, for example, you need the UIImage for purposes other than showing it on the screen.
model.loadedImage = loadedImage
}
// Add the `preview` to your view hierarchy.
Installation
Cocoapods
SMLinkPreview is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'SMLinkPreview'
or
pod 'SMLinkPreview', :git => 'https://github.com/crspybits/SMLinkPreview.git'
Swift Package Manager
It is also available through the Swift Package Manager. To install, add the following line to your Package.swift file as a dependency:
.package(url: "https://github.com/crspybits/SMLinkPreview.git", from: "0.2.0")
Author
crspybits, chris@SpasticMuffin.biz
License
SMLinkPreview is available under the MIT license. See the LICENSE file for more info.
