- 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.
To run the example project, clone the repo, and run pod install
from the Example directory first.
- 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
linkData
to 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.
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'
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")
crspybits, [email protected]
SMLinkPreview is available under the MIT license. See the LICENSE file for more info.