• Stars
    star
    1,372
  • Rank 34,264 (Top 0.7 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 8 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

It makes a preview from an URL, grabbing all the information such as title, relevant texts and images.

Swift Link Preview

Link Previewer for iOS, macOS, watchOS and tvOS

It makes a preview from an URL, grabbing all the information such as title, relevant texts and images.

Platform CocoaPods Carthage Compatible Swift Package Manager Build Status

Index


Visual Examples

UTF-8 Extended UTF-8 Gallery
UTF-8 Extended UTF-8 Gallery
Video Websites Images
Video Websites Images

Requirements and Details

  • iOS 8.0+ / macOS 10.11+ / tvOS 9.0+ / watchOS 2.0+
  • Xcode 8.0+
  • Built with Swift 3

Installation

CocoaPods

To use SwiftLinkPreview as a pod package just add the following in your Podfile file.

	source 'https://github.com/CocoaPods/Specs.git'
	platform :ios, '9.0'

	target 'Your Target Name' do
	  	use_frameworks!
	  	// ...
	  	pod 'SwiftLinkPreview', '~> 3.4.0'
	  	// ...
	end

Carthage

To use SwiftLinkPreview as a Carthage module package just add the following in your Cartfile file.

  	// ...
	github "LeonardoCardoso/SwiftLinkPreview" ~> 3.4.0
  	// ...

Swift Package Manager

To use SwiftLinkPreview as a Swift Package Manager package just add the following in your Package.swift file.

import PackageDescription

let package = Package(
  name: "Your Target Name",
  dependencies: [
  	// ...
    .Package(url: "https://github.com/LeonardoCardoso/SwiftLinkPreview.git", "3.4.0")
  	// ...
  ]
)

Manually

You just need to drop all contents in Sources folder, but .plist files, into Xcode project (make sure to enable "Copy items if needed" and "Create groups").

Usage

Instatiating

import SwiftLinkPreview

// ...

let slp = SwiftLinkPreview(session: URLSession.shared,
			   workQueue: SwiftLinkPreview.defaultWorkQueue,
			   responseQueue: DispatchQueue.main,
		           cache: DisabledCache.instance)

Requesting preview

let preview = slp.preview("Text containing URL",
                          onSuccess: { result in print("\(result)") },
                          onError: { error in print("\(error)")})
// preview.cancel() to cancel it.

result is a struct Response:

Response {
	let url: URL // URL
	let finalUrl: URL // unshortened URL
	let canonicalUrl: String // canonical URL
	let title: String // title
	let description: String // page description or relevant text
	let images: [String] // array of URLs of the images
	let image: String // main image
	let icon: String // favicon
	let video: String // video
	let price: String // price
}

Cancelling a request

let cancelablePreview = slp.preview(...,
				    onSuccess: ...,
				    onError: ...)

cancelablePreview.cancel()

Enabling and accessing cache

SLP has a built-in memory cache, so create your object as the following:

let slp = SwiftLinkPreview(cache: InMemoryCache())

To get the cached response:

if let cached = self.slp.cache.slp_getCachedResponse(url: String) {
    // Do whatever with the cached response
} else {
	// Perform preview otherwise
	slp.preview(...)
}

If you want to create your own cache, just implement this protocol and use it on the object initializer.

public protocol Cache {

    func slp_getCachedResponse(url: String) -> SwiftLinkPreview.Response?

    func slp_setCachedResponse(url: String, response: SwiftLinkPreview.Response?)
}

FLOW

flow

Important

You need to set Allow Arbitrary Loads to YES on your project's Info.plist file.

app security

If you don't want to use the option above and you are using SwiftLinkPreview for services of your knowledge, you can whitelist them on Info.plist file as well. You can read more about it here, here and here.

app security

Tips

Not all websites will have their info brought, you can treat the info that your implementation gets as you like. But here are two tips about posting a preview:

  • If some info is missing, you can offer the user to enter it. Take for example the description.
  • If more than one image is fetched, you can offer the user the feature of picking one image.

Tests

Feel free to fork this repo and add URLs on SwiftLinkPreviewTests/URLs.swift to test URLs and help improving this lib. The more URLs the better the reliability.

Information and Contact

Developed by @LeonardoCardoso.

Contact me either by Twitter @leocardz or emailing me to [email protected].

Related Projects

License

The MIT License (MIT)

Copyright (c) 2016 Leonardo Cardoso

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Follow me for the latest updates

More Repositories

1

NFDownloadButton

Revamped Download Button. It's kinda a reverse engineering of Netflix's app download button.
Swift
434
star
2

Android-Link-Preview

It makes a preview from an url, grabbing all the information such as title, relevant texts and images. This a version for Android of my web link preview https://github.com/LeonardoCardoso/Link-Preview
Java
420
star
3

SectionedSlider

iOS 11 Control Center Slider
Swift
369
star
4

AudioIndicatorBars

AIB indicates for your app users which audio is playing. Just like the Podcasts app.
Swift
288
star
5

Link-Preview

AngularJS + Bootstrap Version of https://github.com/LeonardoCardoso/Facebook-Link-Preview
PHP
163
star
6

Facebook-Link-Preview

Go to >> https://github.com/LeonardoCardoso/Link-Preview
PHP
131
star
7

Animated-Expanding-ListView

Animated Expanding ListView provides a fancy animation on expanding or collapsing the content of a listview item.
Java
130
star
8

SendIndicator

Yet another task indicator
Swift
62
star
9

InitMacro

Swift
41
star
10

BLE

Swift
28
star
11

gsrd

GitHub Starred Repos Downloader
Ruby
26
star
12

Colorblinding

An extension for Google Chrome (and Chromium) that simulates the website as a color vision impaired person would see.
JavaScript
25
star
13

Multitouch-Test

A simple app to test the touch of your device. It measures and shows how accurate is the touch of your device, how many touches it supports, and there are some options to personalize the exhibition.
Java
17
star
14

BedtimeClock

Swift
14
star
15

RevealTextField

Simple extension to add a reveal button to your boring UITextField
Swift
14
star
16

Sorting-Algorithms

Quick set of sorting algorithms using Java.
Java
11
star
17

UILabelSearchHighlighter

Swift
9
star
18

Unshorten-It

A simple, beautiful, fast and useful url unshortener with link preview.
Java
7
star
19

Multiple-Layout-ListView

Multiple Layout ListView
Java
7
star
20

UITableViewFiltering

Swift
6
star
21

Android-Audio-Recorder

A simple Android app to record an audio.
Java
6
star
22

Silence-Please

Control the ambient sound by a sound level limit. When it is exceeded, the "Silence Please" will alert!
Java
5
star
23

GitAliases

Some useful Git aliases to optimise your time
Shell
5
star
24

Genetic-Algorithm

This project is about the functionality of a genetic algorithm.
Java
5
star
25

Xcode-Snippets

4
star
26

Folder

Shell
3
star
27

Clipboarded

Have you ever copied an image but couldn't find a way to save it to the Camera Roll?! That's why!
Swift
3
star
28

Movement

A funny way to interact with the mouse
JavaScript
3
star
29

AnnotationsOff

An extension for Google Chrome (and Chromium) that removes all unwanted annotations in Youtube videos.
JavaScript
3
star
30

mvn-repo

A simple maven repository
3
star
31

Trees

Set of many algorithms related to Trees with lexicographical ordering.
Java
3
star
32

What-coding-style-is-Leo-Using

For everyone who wants to know more about how I'm structuring my projects.
3
star
33

Separate-Compilation-Unit

Single Compilation Unit, also known as Separate Compilation.
C++
3
star
34

i18n-PHP-AngularJS

Easy i18n localization with PHP and AngularJS
PHP
2
star
35

Hashing-Algorithms

It's designed to keep insertion, deletion and search with O(1) complexity.
Java
2
star
36

Rotate-and-Scale-jQuery

A mix of Rotate and Scale.
JavaScript
2
star
37

dotfiles

My .files
Shell
2
star
38

Pix-Color-Analyzer

Image pixel color analyzer
Java
2
star
39

Priority-Queue

Set of many algorithms related to Priority Queues.
Java
2
star
40

gitcase

Share your complete GitHub contribution graph
CSS
2
star
41

Scale-jQuery

Scale whatever elements you want with jQuery
JavaScript
2
star
42

Rotate-jQuery

Rotate whatever elements you want with jQuery
HTML
2
star
43

Android-Audio-Preview

A simple app to stream a song by url for pre(view|listen)ing it.
Java
2
star
44

Android-Custom-Font

Two ways to use custom fonts without memory leak, even if you use in a ListView.
Java
2
star
45

What-terminal-is-Leo-using

For everyone who wants to know more about the terminal I'm using
2
star
46

Font-Awesomed-Android

Example code of how to use Font Awesome on Android native and programatically
Java
1
star
47

Primitive-Recursive-Functions

A class with the common Math operations using recursion.
Java
1
star
48

LeonardoCardoso

1
star