Skip to content

Commit

Permalink
新增Spotlight插件
Browse files Browse the repository at this point in the history
  • Loading branch information
pcjbird committed Jan 4, 2018
1 parent 9751d3e commit 31f34ea
Show file tree
Hide file tree
Showing 9 changed files with 267 additions and 5 deletions.
4 changes: 3 additions & 1 deletion Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ target 'QuickWebKit' do
pod 'FTPopOverMenu'
# Pods for QuickWebToastPlugin
pod 'Toast'
# Pods for QuickWebSharePlugin
# Pods for QuickWebSharePlugin,QuickWebSpotlightPlugin
pod 'EasyShareKit'
pod 'GTMNSStringHTMLAdditions'
# Pods for QuickWebQRCodePlugin
Expand All @@ -28,4 +28,6 @@ target 'QuickWebKit' do
pod 'LNRefresh'
# Pods for QuickWebMJRefreshPlugin
pod 'MJRefresh'
# Pods for QuickWebSpotlightPlugin
pod 'YYCategories'
end
13 changes: 11 additions & 2 deletions QuickWebKit.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "QuickWebKit"
s.version = "1.0.3"
s.version = "1.0.4"
s.summary = "A great & strong plugin based WebViewController. 一款基于插件的 WebView 视图控制器,您可以基于它设计您的浏览器插件,然后像积木一样来组装它们。"
s.description = <<-DESC
A great & strong plugin based WebViewController. 一款基于插件的 WebView 视图控制器,您可以基于它设计您的浏览器插件,然后像积木一样来组装它们。
Expand All @@ -15,7 +15,7 @@ Pod::Spec.new do |s|
#s.screenshot = ''

s.platform = :ios, '8.0'
s.frameworks = 'Foundation', 'UIKit', 'CoreGraphics', 'WebKit', 'JavaScriptCore' ,'QuartzCore'
s.frameworks = 'Foundation', 'UIKit', 'CoreGraphics', 'WebKit', 'JavaScriptCore' ,'QuartzCore', 'MobileCoreServices', 'CoreSpotlight'
#s.preserve_paths = ''
s.source_files = 'QuickWebKit/QuickWebKit.h'

Expand Down Expand Up @@ -80,6 +80,15 @@ Pod::Spec.new do |s|
ss.dependency 'MJRefresh'
end

s.subspec 'QuickWebSpotlightPlugin' do |ss|
ss.source_files = 'QuickWebKit/QuickWebPlugins/QuickWebSpotlightPlugin'
ss.public_header_files = 'QuickWebKit/QuickWebPlugins/QuickWebSpotlightPlugin/QuickWebSpotlightPlugin.h'
ss.dependency 'QuickWebKit/QuickWebViewController'
ss.dependency 'EasyShareKit'
ss.dependency 'GTMNSStringHTMLAdditions'
ss.dependency 'YYCategories'
end

s.subspec 'QuickWebJSBridgePlugin' do |ss|
ss.source_files = 'QuickWebKit/QuickWebPlugins/QuickWebJSBridgePlugin'
ss.public_header_files = 'QuickWebKit/QuickWebPlugins/QuickWebJSBridgePlugin/*.{h}'
Expand Down
16 changes: 16 additions & 0 deletions QuickWebKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
890BF7C11FF3EB47003E8E7C /* QuickWebQRCodePlugin.m in Sources */ = {isa = PBXBuildFile; fileRef = 890BF7BF1FF3EB47003E8E7C /* QuickWebQRCodePlugin.m */; };
898A50C41FE7FDE7002DDFA7 /* QuickWebViewController.h in Headers */ = {isa = PBXBuildFile; fileRef = 898A50C21FE7FDE7002DDFA7 /* QuickWebViewController.h */; settings = {ATTRIBUTES = (Public, ); }; };
898A50CD1FE7FF98002DDFA7 /* QuickWebViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 898A50CC1FE7FF98002DDFA7 /* QuickWebViewController.m */; };
89A79CA31FFE71F2009AF079 /* QuickWebSpotlightPlugin.h in Headers */ = {isa = PBXBuildFile; fileRef = 89A79CA11FFE71F2009AF079 /* QuickWebSpotlightPlugin.h */; settings = {ATTRIBUTES = (Public, ); }; };
89A79CA41FFE71F2009AF079 /* QuickWebSpotlightPlugin.m in Sources */ = {isa = PBXBuildFile; fileRef = 89A79CA21FFE71F2009AF079 /* QuickWebSpotlightPlugin.m */; };
89B310961FF34C13000D07CB /* QuickWebJSBridgeSystemProxy.h in Headers */ = {isa = PBXBuildFile; fileRef = 89B310941FF34C13000D07CB /* QuickWebJSBridgeSystemProxy.h */; settings = {ATTRIBUTES = (Public, ); }; };
89B310971FF34C13000D07CB /* QuickWebJSBridgeSystemProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = 89B310951FF34C13000D07CB /* QuickWebJSBridgeSystemProxy.m */; };
89B3109A1FF35EF3000D07CB /* QuickWebJSBridgeAccountProxy.h in Headers */ = {isa = PBXBuildFile; fileRef = 89B310981FF35EF3000D07CB /* QuickWebJSBridgeAccountProxy.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -174,6 +176,8 @@
898A50C21FE7FDE7002DDFA7 /* QuickWebViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = QuickWebViewController.h; sourceTree = "<group>"; };
898A50C31FE7FDE7002DDFA7 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
898A50CC1FE7FF98002DDFA7 /* QuickWebViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = QuickWebViewController.m; sourceTree = "<group>"; };
89A79CA11FFE71F2009AF079 /* QuickWebSpotlightPlugin.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = QuickWebSpotlightPlugin.h; sourceTree = "<group>"; };
89A79CA21FFE71F2009AF079 /* QuickWebSpotlightPlugin.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = QuickWebSpotlightPlugin.m; sourceTree = "<group>"; };
89B310941FF34C13000D07CB /* QuickWebJSBridgeSystemProxy.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = QuickWebJSBridgeSystemProxy.h; sourceTree = "<group>"; };
89B310951FF34C13000D07CB /* QuickWebJSBridgeSystemProxy.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = QuickWebJSBridgeSystemProxy.m; sourceTree = "<group>"; };
89B310981FF35EF3000D07CB /* QuickWebJSBridgeAccountProxy.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = QuickWebJSBridgeAccountProxy.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -387,6 +391,7 @@
890BF7801FEFCA87003E8E7C /* QuickWebJSBridgePlugin */,
890479681FFBBB7800B0DA41 /* QuickWebLNRefreshPlugin */,
8904796D1FFBBFB300B0DA41 /* QuickWebMJRefreshPlugin */,
89A79CA01FFE71C2009AF079 /* QuickWebSpotlightPlugin */,
);
path = QuickWebPlugins;
sourceTree = "<group>";
Expand Down Expand Up @@ -482,6 +487,15 @@
name = Products;
sourceTree = "<group>";
};
89A79CA01FFE71C2009AF079 /* QuickWebSpotlightPlugin */ = {
isa = PBXGroup;
children = (
89A79CA11FFE71F2009AF079 /* QuickWebSpotlightPlugin.h */,
89A79CA21FFE71F2009AF079 /* QuickWebSpotlightPlugin.m */,
);
path = QuickWebSpotlightPlugin;
sourceTree = "<group>";
};
89B310921FF34999000D07CB /* Proxies */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -560,6 +574,7 @@
89B3109E1FF3621D000D07CB /* QuickWebJSBridgeContactProxy.h in Headers */,
8904796B1FFBBBA700B0DA41 /* QuickWebLNRefreshPlugin.h in Headers */,
890479701FFBC10F00B0DA41 /* QuickWebMJRefreshPlugin.h in Headers */,
89A79CA31FFE71F2009AF079 /* QuickWebSpotlightPlugin.h in Headers */,
89B310A61FF36825000D07CB /* QuickWebJSBridgeNavBarProxy.h in Headers */,
890BF7531FEEB31F003E8E7C /* QuickWebProvider.h in Headers */,
890BF74F1FEEB262003E8E7C /* UIScrollView+QuickWebProviderPlugin.h in Headers */,
Expand Down Expand Up @@ -857,6 +872,7 @@
files = (
89B310B81FF374B6000D07CB /* NSString+QuickWeb.m in Sources */,
890BF78C1FEFCE9E003E8E7C /* QuickWebDataParseUtil.m in Sources */,
89A79CA41FFE71F2009AF079 /* QuickWebSpotlightPlugin.m in Sources */,
890BF7A71FF13506003E8E7C /* QuickWebJSInvokeCommand.m in Sources */,
890BF7C11FF3EB47003E8E7C /* QuickWebQRCodePlugin.m in Sources */,
890BF7881FEFCE1E003E8E7C /* QuickWebJSBridgeInvokeCommand.m in Sources */,
Expand Down
10 changes: 10 additions & 0 deletions QuickWebKit/QuickWebKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@
// 框架名称:QuickWebKit
// 框架功能:A great & strong plugin based WebViewController. 一款基于插件的 WebView 视图控制器,您可以基于它设计您的浏览器插件,然后像积木一样来组装它们。
// 修改记录:
// pcjbird 2018-01-04 Version:1.0.4 Build:201801040001
// 1.新增Spotlight插件
//
// pcjbird 2018-01-02 Version:1.0.3 Build:201801020003
// 1.新增LNRefresh和MJRefresh插件
// 2.新增是否优先使用WKWebView初始化函数
//
// pcjbird 2018-01-02 Version:1.0.2 Build:201801020002
// 1.修复QuickWebProviderPlugin字体颜色的问题
//
// pcjbird 2018-01-02 Version:1.0.1 Build:201801020001
// 1.修复dealloc中使用了weak导致的crash问题
//
// pcjbird 2017-12-24 Version:1.0.0 Build:201712240001
// 1.首次发布SDK版本

Expand Down Expand Up @@ -92,6 +98,10 @@ FOUNDATION_EXPORT const unsigned char QuickWebKitVersionString[];
#import "QuickWebMJRefreshPlugin.h"
#endif

#if __has_include("QuickWebSpotlightPlugin.h")
#import "QuickWebSpotlightPlugin.h"
#endif

#if __has_include("QuickWebJSBridgePlugin.h")
#import "QuickWebJSBridgePlugin.h"
#endif
Expand Down
4 changes: 4 additions & 0 deletions QuickWebKit/QuickWebKitDefines/QuickWebKitDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#ifndef QuickWebKitDefines_h
#define QuickWebKitDefines_h

#define QUICKWEB_APP_NAME ([[[NSBundle mainBundle] localizedInfoDictionary] objectForKey:@"CFBundleDisplayName"] ? [[[NSBundle mainBundle] localizedInfoDictionary] objectForKey:@"CFBundleDisplayName"]:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"])
#define QUICKWEB_APP_BUNDLEID ([[[NSBundle mainBundle] localizedInfoDictionary] objectForKey:@"CFBundleIdentifier"])
#define QUICKWEB_APP_VERSION ([[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"])

#ifdef DEBUG
# define SDK_LOG(fmt, ...) NSLog((@"[🦉QuickWebKit] %s (line %d) " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ -(void)webViewControllerDidFinishLoad:(QuickWebViewController *)webViewControlle
}
if(![shareInfo isKindOfClass:[EasyShareInfo class]])
{
SDK_LOG(@"未能正确页面获取分享信息.(%@)", url);
SDK_LOG(@"未能正确获取页面分享信息.(%@)", url);
return;
}
SDK_LOG(@"获取页面分享信息成功,耗时:%ldms.(%@)", cost, url);
Expand Down
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
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
2 changes: 1 addition & 1 deletion QuickWebKit/Supporting Files/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0.3</string>
<string>1.0.4</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
Expand Down

0 comments on commit 31f34ea

Please sign in to comment.