pub struct LPLinkMetadata { /* private fields */ }LPLinkMetadata only.Expand description
An object that contains metadata about a URL.
Use LPLinkMetadata to store the metadata about a URL, including its
title, icon, images and video.
Fetch metadata using LPMetadataProvider. For remote URLs, cache the
metadata locally to avoid the data and performance cost of fetching it from
the internet every time you present it. LPLinkMetadata is serializable
with
<doc
://com.apple.documentation/documentation/foundation/nssecurecoding>.
For local file URLs, the <doc ://com.apple.documentation/documentation/quicklookthumbnailing> API retrieves a representative thumbnail for the file, if possible.
§Provide custom metadata
Say your app already has a database of links, with titles and images that
weren’t fetched by LPMetadataProvider. You don’t have to fetch new
metadata from the internet in order to accelerate the share sheet or to
present a rich link. Instead, you can fill in the fields of
LPLinkMetadata yourself.
Create an LPLinkMetadata object, and fill in at least the
LPLinkMetadata/originalURL and LPLinkMetadata/URL fields, plus
whatever additional information you have.
func activityViewControllerLinkMetadata(_: UIActivityViewController) -> LPLinkMetadata? {
let metadata = LPLinkMetadata()
metadata.originalURL = URL(string: "https://www.example.com/apple-pie")
metadata.url = metadata.originalURL
metadata.title = "The Greatest Apple Pie In The World"
metadata.imageProvider = NSItemProvider.init(contentsOf:
Bundle.main.url(forResource: "apple-pie", withExtension: "jpg"))
return metadata
}§Accelerate the share sheet preview
For existing apps that share URLs, the share sheet automatically presents a preview of the link. The preview first shows a placeholder link icon alongside the base URL while fetching the link’s metadata over the network. The preview updates once the link’s icon and title become available.
If you already have an LPLinkMetadata object for a URL, pass it to the
share sheet to present the preview instantly, without fetching data over the
network. In your implementation of
<doc
://com.apple.documentation/documentation/uikit/uiactivityitemsource/3144571-activityviewcontrollerlinkmetada>,
return the metadata object.
func activityViewControllerLinkMetadata(_:
UIActivityViewController) -> LPLinkMetadata? {
return self.metadata
}If the user chooses to share to Messages, the same metadata passes directly through, providing a smooth and seamless experience with no unnecessary loading.
See also Apple’s documentation
Implementations§
Source§impl LPLinkMetadata
impl LPLinkMetadata
Sourcepub unsafe fn originalURL(&self) -> Option<Retained<NSURL>>
pub unsafe fn originalURL(&self) -> Option<Retained<NSURL>>
The original URL of the metadata request.
Sourcepub unsafe fn setOriginalURL(&self, original_url: Option<&NSURL>)
pub unsafe fn setOriginalURL(&self, original_url: Option<&NSURL>)
Setter for originalURL.
Sourcepub unsafe fn URL(&self) -> Option<Retained<NSURL>>
pub unsafe fn URL(&self) -> Option<Retained<NSURL>>
The URL that returned the metadata, taking server-side redirects into account.
The URL that returns the metadata may differ from the
LPLinkMetadata/originalURL to which you sent the metadata request. This
can happen if the server redirects the request, for example, when a resource
has moved, or when the original URL is a domain alias.
Sourcepub unsafe fn iconProvider(&self) -> Option<Retained<NSItemProvider>>
pub unsafe fn iconProvider(&self) -> Option<Retained<NSItemProvider>>
An object that retrieves data corresponding to a representative icon for the URL.
Sourcepub unsafe fn setIconProvider(&self, icon_provider: Option<&NSItemProvider>)
pub unsafe fn setIconProvider(&self, icon_provider: Option<&NSItemProvider>)
Setter for iconProvider.
Sourcepub unsafe fn imageProvider(&self) -> Option<Retained<NSItemProvider>>
pub unsafe fn imageProvider(&self) -> Option<Retained<NSItemProvider>>
An object that retrieves data corresponding to a representative image for the URL.
Sourcepub unsafe fn setImageProvider(&self, image_provider: Option<&NSItemProvider>)
pub unsafe fn setImageProvider(&self, image_provider: Option<&NSItemProvider>)
Setter for imageProvider.
Sourcepub unsafe fn videoProvider(&self) -> Option<Retained<NSItemProvider>>
pub unsafe fn videoProvider(&self) -> Option<Retained<NSItemProvider>>
An object that retrieves data corresponding to a representative video for the URL.
The item provider returns a video that <doc ://com.apple.documentation/documentation/avfoundation> can play.
Sourcepub unsafe fn setVideoProvider(&self, video_provider: Option<&NSItemProvider>)
pub unsafe fn setVideoProvider(&self, video_provider: Option<&NSItemProvider>)
Setter for videoProvider.
Sourcepub unsafe fn remoteVideoURL(&self) -> Option<Retained<NSURL>>
pub unsafe fn remoteVideoURL(&self) -> Option<Retained<NSURL>>
A remote URL corresponding to a representative video for the URL.
This may reference a remote video file that <doc ://com.apple.documentation/documentation/avfoundation> can stream, or a YouTube video URL.
Sourcepub unsafe fn setRemoteVideoURL(&self, remote_video_url: Option<&NSURL>)
pub unsafe fn setRemoteVideoURL(&self, remote_video_url: Option<&NSURL>)
Setter for remoteVideoURL.
Methods from Deref<Target = NSObject>§
Sourcepub fn doesNotRecognizeSelector(&self, sel: Sel) -> !
pub fn doesNotRecognizeSelector(&self, sel: Sel) -> !
Handle messages the object doesn’t recognize.
See Apple’s documentation for details.
Methods from Deref<Target = AnyObject>§
Sourcepub fn class(&self) -> &'static AnyClass
pub fn class(&self) -> &'static AnyClass
Dynamically find the class of this object.
§Panics
May panic if the object is invalid (which may be the case for objects
returned from unavailable init/new methods).
§Example
Check that an instance of NSObject has the precise class NSObject.
use objc2::ClassType;
use objc2::runtime::NSObject;
let obj = NSObject::new();
assert_eq!(obj.class(), NSObject::class());Sourcepub unsafe fn get_ivar<T>(&self, name: &str) -> &Twhere
T: Encode,
👎Deprecated: this is difficult to use correctly, use Ivar::load instead.
pub unsafe fn get_ivar<T>(&self, name: &str) -> &Twhere
T: Encode,
Ivar::load instead.Use Ivar::load instead.
§Safety
The object must have an instance variable with the given name, and it
must be of type T.
See Ivar::load_ptr for details surrounding this.
Sourcepub fn downcast_ref<T>(&self) -> Option<&T>where
T: DowncastTarget,
pub fn downcast_ref<T>(&self) -> Option<&T>where
T: DowncastTarget,
Attempt to downcast the object to a class of type T.
This is the reference-variant. Use Retained::downcast if you want
to convert a retained object to another type.
§Mutable classes
Some classes have immutable and mutable variants, such as NSString
and NSMutableString.
When some Objective-C API signature says it gives you an immutable class, it generally expects you to not mutate that, even though it may technically be mutable “under the hood”.
So using this method to convert a NSString to a NSMutableString,
while not unsound, is generally frowned upon unless you created the
string yourself, or the API explicitly documents the string to be
mutable.
See Apple’s documentation on mutability and on
isKindOfClass: for more details.
§Generic classes
Objective-C generics are called “lightweight generics”, and that’s because they aren’t exposed in the runtime. This makes it impossible to safely downcast to generic collections, so this is disallowed by this method.
You can, however, safely downcast to generic collections where all the
type-parameters are AnyObject.
§Panics
This works internally by calling isKindOfClass:. That means that the
object must have the instance method of that name, and an exception
will be thrown (if CoreFoundation is linked) or the process will abort
if that is not the case. In the vast majority of cases, you don’t need
to worry about this, since both root objects NSObject and
NSProxy implement this method.
§Examples
Cast an NSString back and forth from NSObject.
use objc2::rc::Retained;
use objc2_foundation::{NSObject, NSString};
let obj: Retained<NSObject> = NSString::new().into_super();
let string = obj.downcast_ref::<NSString>().unwrap();
// Or with `downcast`, if we do not need the object afterwards
let string = obj.downcast::<NSString>().unwrap();Try (and fail) to cast an NSObject to an NSString.
use objc2_foundation::{NSObject, NSString};
let obj = NSObject::new();
assert!(obj.downcast_ref::<NSString>().is_none());Try to cast to an array of strings.
use objc2_foundation::{NSArray, NSObject, NSString};
let arr = NSArray::from_retained_slice(&[NSObject::new()]);
// This is invalid and doesn't type check.
let arr = arr.downcast_ref::<NSArray<NSString>>();This fails to compile, since it would require enumerating over the array to ensure that each element is of the desired type, which is a performance pitfall.
Downcast when processing each element instead.
use objc2_foundation::{NSArray, NSObject, NSString};
let arr = NSArray::from_retained_slice(&[NSObject::new()]);
for elem in arr {
if let Some(data) = elem.downcast_ref::<NSString>() {
// handle `data`
}
}Trait Implementations§
Source§impl AsRef<AnyObject> for LPLinkMetadata
impl AsRef<AnyObject> for LPLinkMetadata
Source§impl AsRef<LPLinkMetadata> for LPLinkMetadata
impl AsRef<LPLinkMetadata> for LPLinkMetadata
Source§impl AsRef<NSObject> for LPLinkMetadata
impl AsRef<NSObject> for LPLinkMetadata
Source§impl Borrow<AnyObject> for LPLinkMetadata
impl Borrow<AnyObject> for LPLinkMetadata
Source§impl Borrow<NSObject> for LPLinkMetadata
impl Borrow<NSObject> for LPLinkMetadata
Source§impl ClassType for LPLinkMetadata
impl ClassType for LPLinkMetadata
Source§const NAME: &'static str = "LPLinkMetadata"
const NAME: &'static str = "LPLinkMetadata"
Source§type ThreadKind = <<LPLinkMetadata as ClassType>::Super as ClassType>::ThreadKind
type ThreadKind = <<LPLinkMetadata as ClassType>::Super as ClassType>::ThreadKind
Source§impl CopyingHelper for LPLinkMetadata
impl CopyingHelper for LPLinkMetadata
Source§type Result = LPLinkMetadata
type Result = LPLinkMetadata
Self if the type has no
immutable counterpart. Read moreSource§impl Debug for LPLinkMetadata
impl Debug for LPLinkMetadata
Source§impl Deref for LPLinkMetadata
impl Deref for LPLinkMetadata
Source§impl Hash for LPLinkMetadata
impl Hash for LPLinkMetadata
Source§impl Message for LPLinkMetadata
impl Message for LPLinkMetadata
Source§impl NSCoding for LPLinkMetadata
impl NSCoding for LPLinkMetadata
Source§impl NSCopying for LPLinkMetadata
impl NSCopying for LPLinkMetadata
Source§impl NSObjectProtocol for LPLinkMetadata
impl NSObjectProtocol for LPLinkMetadata
Source§fn isEqual(&self, other: Option<&AnyObject>) -> bool
fn isEqual(&self, other: Option<&AnyObject>) -> bool
Source§fn hash(&self) -> usize
fn hash(&self) -> usize
Source§fn isKindOfClass(&self, cls: &AnyClass) -> bool
fn isKindOfClass(&self, cls: &AnyClass) -> bool
Source§fn is_kind_of<T>(&self) -> bool
fn is_kind_of<T>(&self) -> bool
isKindOfClass directly, or cast your objects with AnyObject::downcast_ref