-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
267 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
QuickWebKit/QuickWebPlugins/QuickWebSpotlightPlugin/QuickWebSpotlightPlugin.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// QuickWebSpotlightPlugin.h | ||
// QuickWebKit | ||
// | ||
// Created by pcjbird on 2018/1/4. | ||
// Copyright © 2018年 Zero Status. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
#import "QuickWebPluginProtocol.h" | ||
#import "QuickWebViewController.h" | ||
/* | ||
* @brief Spotlight插件 | ||
*/ | ||
@interface QuickWebSpotlightPlugin : NSObject<QuickWebPluginProtocol> | ||
|
||
/* | ||
* @brief 附加关键字数组,例如:附加App名称、公司信息等, 默认会添加App名称 | ||
*/ | ||
-(NSArray<NSString*>*)additionalKeywords; | ||
|
||
/* | ||
* @brief 自定义元标签,参照EasyShareKit使用,用于解析项目自定义的网页信息 | ||
*/ | ||
-(NSArray<NSString*>*)customMetaTags; | ||
|
||
/* | ||
* @brief 在AppDelegate中调用,示例代码如下: | ||
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler | ||
{ | ||
if([QuickWebSpotlightPlugin handleContinueUserActivity:userActivity]) | ||
{ | ||
return YES; | ||
} | ||
//处理其他情形 | ||
} | ||
*/ | ||
+(BOOL) handleContinueUserActivity:(NSUserActivity *)userActivity; | ||
@end |
182 changes: 182 additions & 0 deletions
182
QuickWebKit/QuickWebPlugins/QuickWebSpotlightPlugin/QuickWebSpotlightPlugin.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
// | ||
// QuickWebSpotlightPlugin.m | ||
// QuickWebKit | ||
// | ||
// Created by pcjbird on 2018/1/4. | ||
// Copyright © 2018年 Zero Status. All rights reserved. | ||
// | ||
|
||
#import "QuickWebKit.h" | ||
#import "QuickWebSpotlightPlugin.h" | ||
#import "QuickWebKitDefines.h" | ||
#import "QuickWebStringUtil.h" | ||
#import <EasyShareKit/EasyShareKit.h> | ||
#import <GTMNSStringHTMLAdditions/GTMNSString+HTML.h> | ||
#import <MobileCoreServices/MobileCoreServices.h> | ||
#import <CoreSpotlight/CoreSpotlight.h> | ||
#import <YYCategories/YYCategories.h> | ||
|
||
@implementation QuickWebSpotlightPlugin | ||
|
||
-(NSString *)name | ||
{ | ||
return @"QuickWebSpotlightPlugin"; | ||
} | ||
|
||
-(void)setName:(NSString *)name | ||
{ | ||
|
||
} | ||
|
||
-(NSArray<NSString *> *)additionalKeywords | ||
{ | ||
NSString *app_name = QUICKWEB_APP_NAME; | ||
if([app_name isKindOfClass:[NSString class]]) | ||
{ | ||
return [NSArray arrayWithObject:app_name]; | ||
} | ||
return [NSArray array]; | ||
} | ||
|
||
-(NSArray<NSString*>*)customMetaTags | ||
{ | ||
return nil; | ||
} | ||
|
||
#pragma mark - QuickWebPluginProtocol | ||
|
||
-(void)webViewControllerDidFinishLoad:(QuickWebViewController *)webViewController | ||
{ | ||
if([[[UIDevice currentDevice] systemVersion] floatValue] < 9.0f) | ||
{ | ||
SDK_LOG(@"无法创建Spotlight,当前系统版本不支持Spotlight。"); | ||
return; | ||
} | ||
weak(weakSelf); | ||
dispatch_async(dispatch_get_main_queue(), ^{ | ||
NSString* url = [webViewController.webView.url absoluteString]; | ||
[webViewController.webView evaluateJavaScript:@"document.documentElement.innerHTML" completionHandler:^(id _Nullable result, NSError * _Nullable error) { | ||
if([error isKindOfClass:[NSError class]]) return; | ||
NSString * htmlText = result; | ||
if(![QuickWebStringUtil isStringBlank:htmlText]) | ||
{ | ||
EasyShareKit * shareKit = [[EasyShareKit alloc] initWithHtml:htmlText]; | ||
NSArray<NSString *>* metaTags = [self customMetaTags]; | ||
if([metaTags isKindOfClass:[NSArray<NSString*> class]]) | ||
{ | ||
[shareKit setCustomMetaTags:metaTags]; | ||
} | ||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ | ||
[shareKit getWebShareInfo:^(EasyShareInfo *shareInfo, long cost,NSError *error) { | ||
if([error isKindOfClass:[NSError class]]) | ||
{ | ||
SDK_LOG(@"Spotlight获取页面信息失败:%@.(%@)", error.localizedDescription, url); | ||
return; | ||
} | ||
if(![shareInfo isKindOfClass:[EasyShareInfo class]]) | ||
{ | ||
SDK_LOG(@"Spotlight未能正确获取页面信息.(%@)", url); | ||
return; | ||
} | ||
SDK_LOG(@"Spotlight获取页面信息成功,耗时:%ldms.(%@)", cost, url); | ||
|
||
|
||
if([QuickWebStringUtil isStringBlank:shareInfo.url]) | ||
{ | ||
shareInfo.url = url; | ||
} | ||
if([QuickWebStringUtil isStringBlank:shareInfo.desc]) | ||
{ | ||
dispatch_sync(dispatch_get_main_queue(), ^{ | ||
[webViewController.webView evaluateJavaScript:@"document.body" completionHandler:^(id _Nullable result, NSError * _Nullable error) { | ||
if([error isKindOfClass:[NSError class]]) return; | ||
if(![QuickWebStringUtil isStringBlank:result]) | ||
{ | ||
shareInfo.desc = [[result gtm_stringByUnescapingFromHTML] stringByReplacingOccurrencesOfString:@" " withString:@""]; | ||
if([shareInfo.desc length] > 255) | ||
{ | ||
shareInfo.desc = [shareInfo.desc substringToIndex:254]; | ||
} | ||
} | ||
}]; | ||
}); | ||
} | ||
if([QuickWebStringUtil isStringBlank:shareInfo.image]) | ||
{ | ||
dispatch_async(dispatch_get_main_queue(), ^{ | ||
[webViewController.webView evaluateJavaScript:@"SmartJSGetFirstImage();" completionHandler:^(id _Nullable result, NSError * _Nullable error) { | ||
if(![error isKindOfClass:[NSError class]]) | ||
{ | ||
shareInfo.image = result; | ||
} | ||
[weakSelf createSpotlightWithInfo:shareInfo]; | ||
}]; | ||
}); | ||
} | ||
else | ||
{ | ||
[weakSelf createSpotlightWithInfo:shareInfo]; | ||
} | ||
}]; | ||
}); | ||
} | ||
}]; | ||
}); | ||
} | ||
|
||
- (void)createSpotlightWithInfo:(EasyShareInfo *)shareInfo | ||
{ | ||
if([[[UIDevice currentDevice] systemVersion] floatValue] < 9.0f) | ||
{ | ||
SDK_LOG(@"无法创建Spotlight,当前系统版本不支持Spotlight。"); | ||
return; | ||
} | ||
|
||
if(![shareInfo isKindOfClass:[EasyShareInfo class]]) return; | ||
if (@available(iOS 9.0, *)) { | ||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ | ||
CSSearchableItemAttributeSet *set = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString *)kUTTypeWebArchive]; | ||
set.title = shareInfo.title; | ||
set.contentDescription = shareInfo.desc; | ||
set.version = QUICKWEB_APP_VERSION; | ||
NSMutableArray *keywords = [NSMutableArray array]; | ||
NSArray<NSString *>* additionalKeywords = [self additionalKeywords]; | ||
NSArray<NSString *>* webKeywords = shareInfo.keywords; | ||
if([additionalKeywords isKindOfClass:[NSArray<NSString *> class]]) | ||
{ | ||
[keywords addObjectsFromArray:additionalKeywords]; | ||
} | ||
if([webKeywords isKindOfClass:[NSArray<NSString *> class]]) | ||
{ | ||
[keywords addObjectsFromArray:webKeywords]; | ||
} | ||
[keywords addObject:set.title]; | ||
set.keywords = keywords; | ||
if(![QuickWebStringUtil isStringBlank:shareInfo.image]) | ||
{ | ||
set.thumbnailURL = [NSURL URLWithString:shareInfo.image]; | ||
set.thumbnailData = UIImageJPEGRepresentation([[UIImage imageWithData:[NSData dataWithContentsOfURL:set.thumbnailURL]] imageByResizeToSize:CGSizeMake(180.0f, 180.0f) contentMode:UIViewContentModeScaleAspectFill], 0.5); | ||
} | ||
dispatch_async(dispatch_get_main_queue(), ^{ | ||
CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:[NSString stringWithFormat:@"%@.webview.%@", QUICKWEB_APP_BUNDLEID, [shareInfo.url stringByURLEncode]] domainIdentifier:QUICKWEB_APP_BUNDLEID attributeSet:set]; | ||
[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler:^(NSError * _Nullable error) { | ||
if (error) { | ||
SDK_LOG(@"创建Spotlight失败:%@",error.localizedDescription); | ||
} | ||
}]; | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
+(BOOL) handleContinueUserActivity:(NSUserActivity *)userActivity | ||
{ | ||
if([[userActivity.userInfo valueForKey:@"kCSSearchableItemActivityIdentifier"] rangeOfString:[NSString stringWithFormat:@"%@.webview", QUICKWEB_APP_BUNDLEID]].length > 0) | ||
{ | ||
NSString *webUrl = [[userActivity.userInfo valueForKey:@"kCSSearchableItemActivityIdentifier"] stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@.webview", QUICKWEB_APP_BUNDLEID] withString:@""]; | ||
[[NSNotificationCenter defaultCenter] postNotificationName:QUICKWEBREQUESTURLHANDLERNOTIFICATION object:[webUrl stringByURLDecode]]; | ||
return YES; | ||
} | ||
return NO; | ||
} | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters