LPLinkView 是官方iOS13开始提供的一种链接预览的超级方便的view实现。
LPLinkView presents a link based on its available metadata. Use it to show a link’s title and icon, associated images, inline audio, video playback, and maps in a familiar and consistent style.
意思大概是 LPLinkView根据其可用的元数据显示一个链接。使用它可以以熟悉且一致的样式显示链接的标题和图标,关联的图像,嵌入式音频,视频播放和地图。
其简单的使用方法如下:
#import "ViewController.h"
#import <LinkPresentation/LPMetadataProvider.h>
#import <LinkPresentation/LPLinkView.h>
@interface ViewController ()
@property (nonatomic ,strong) LPLinkMetadata *linkMetadata;
@property (nonatomic ,strong) LPLinkView *linkView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
LPMetadataProvider *pv = [LPMetadataProvider new];
NSString *url = @"https://www.baidu.com";
__weak typeof(self) weakSelf = self;
[pv startFetchingMetadataForURL:[NSURL URLWithString:url] completionHandler:^(LPLinkMetadata * _Nullable metadata, NSError * _Nullable error) {
if (!error) {
weakSelf.linkMetadata = metadata;
}
}];
}
- (void)setLinkMetadata:(LPLinkMetadata *)linkMetadata {
_linkMetadata = linkMetadata;
dispatch_async(dispatch_get_main_queue(), ^{
self.linkView = [[LPLinkView alloc] initWithMetadata:linkMetadata];
self.linkView.frame = CGRectMake(10, 80, 300, 600);
[self.view addSubview:self.linkView];
[self.linkView sizeToFit];
});
}
运行结果如图