tôi sẽ cho cùng một mục tiêu và tôi đã làm điều đó trên các mặt hàng
tôi đã sử dụng những vỏ
pod 'HTMLReader'
pod 'AFNetworking'
Sau đó, tôi thừa hưởng từ AFHTTPResponseSerializer
và trả về một đối tượng có chứa thông tin chi tiết liên kết
#import <UIKit/UIKit.h>
@interface LinkDetails : NSObject
@property (nonatomic,strong) NSString *linkURL;
@property (nonatomic,strong) NSString *linkHOST;
@property (nonatomic,strong) NSString *linkTitle;
@property (nonatomic,strong) NSString *linkDescription;
@property (nonatomic,strong) NSString *linkWebSiteName;
@property (nonatomic,strong) NSString *linkImageUrl;
@property (nonatomic,strong) UIImage *linkImage;
@end
Đây là tiêu đề cho phản hồi của tôiSerializer
#import <AFNetworking/AFNetworking.h>
@interface HTMLResponseSerializer : AFHTTPResponseSerializer
@end
và điều này là việc thực hiện cho responseSerializer tôi
#import "HTMLResponseSerializer.h"
#import <HTMLReader/HTMLReader.h>
#import "LinkDetails.h"
@implementation HTMLResponseSerializer
-(id)responseObjectForResponse:(NSURLResponse *)response data:(NSData *)data error:(NSError *__autoreleasing _Nullable *)error{
NSString *responseStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
LinkDetails *details = [[LinkDetails alloc] init];
HTMLDocument *document = [HTMLDocument documentWithString:responseStr];
NSArray *metaTags = [document nodesMatchingSelector:@"meta"];
for (HTMLElement *metaTag in metaTags) {
if ([[[metaTag attributes] objectForKey:@"property"] isEqualToString:@"og:url"] || [[[metaTag attributes] objectForKey:@"property"] isEqualToString:@"twitter:url"]) {
NSLog(@"%@",[[metaTag attributes] objectForKey:@"content"]);
details.linkURL = [[metaTag attributes] objectForKey:@"content"];
}
if ([[[metaTag attributes] objectForKey:@"property"] isEqualToString:@"og:title"] || [[[metaTag attributes] objectForKey:@"property"] isEqualToString:@"twitter:title"]) {
NSLog(@"%@",[[metaTag attributes] objectForKey:@"content"]);
details.linkTitle = [[metaTag attributes] objectForKey:@"content"];
}
if ([[[metaTag attributes] objectForKey:@"property"] isEqualToString:@"og:description"] || [[[metaTag attributes] objectForKey:@"property"] isEqualToString:@"twitter:description"]) {
NSLog(@"%@",[[metaTag attributes] objectForKey:@"content"]);
details.linkDescription = [[metaTag attributes] objectForKey:@"content"];
}
if ([[[metaTag attributes] objectForKey:@"property"] isEqualToString:@"og:image"] || [[[metaTag attributes] objectForKey:@"property"] isEqualToString:@"twitter:image"]) {
NSLog(@"%@",[[metaTag attributes] objectForKey:@"content"]);
details.linkImageUrl = [[metaTag attributes] objectForKey:@"content"];
}
if ([[[metaTag attributes] objectForKey:@"property"] isEqualToString:@"og:site_name"] || [[[metaTag attributes] objectForKey:@"property"] isEqualToString:@"twitter:site_name"]) {
NSLog(@"%@",[[metaTag attributes] objectForKey:@"content"]);
details.linkWebSiteName = [[metaTag attributes] objectForKey:@"content"];
}
}
if(!details.linkTitle){
details.linkTitle = [document firstNodeMatchingSelector:@"title"].textContent;
}
if(!details.linkDescription){
details.linkTitle = [document firstNodeMatchingSelector:@"description"].textContent;
}
if (!details.linkHOST) {
details.linkHOST = [response.URL host];
}
if (!details.linkURL) {
details.linkURL = [response.URL absoluteString];
}
return details;
}
@end
Đừng quên để gán responseSerlializer để tùy chỉnh của bạn một
này làm việc cho tôi rất tốt
Nguồn
2016-07-14 11:13:43
Các bạn đã thử _anything_? – Abizern
tốt, tôi đã xây dựng một bản xem trước liên kết rất cơ bản trong PHP để lấy dữ liệu nhất định từ một trang web cụ thể, nhưng nó khá thô – d2burke
Bạn đã tìm thấy gì chưa? – Esqarrouth