Skip to content

Commit

Permalink
UIWebView+JKWebStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakey committed Dec 14, 2016
1 parent 0b7030e commit 89b64c2
Show file tree
Hide file tree
Showing 15 changed files with 189 additions and 26 deletions.
18 changes: 12 additions & 6 deletions JKCategories-Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@
B8C8D1391DED106F0069354F /* UIBarButtonItem+JKBadge.m in Sources */ = {isa = PBXBuildFile; fileRef = B8C8D1381DED106F0069354F /* UIBarButtonItem+JKBadge.m */; };
B8C8D1421DED16D60069354F /* UITextField+JKInputLimit.m in Sources */ = {isa = PBXBuildFile; fileRef = B8C8D1411DED16D60069354F /* UITextField+JKInputLimit.m */; };
B8C8D1451DED18820069354F /* UITextView+JKInputLimit.m in Sources */ = {isa = PBXBuildFile; fileRef = B8C8D1441DED18820069354F /* UITextView+JKInputLimit.m */; };
B8C999AE1E012CAD00DB9F8C /* UIWebView+JKWebStorage.m in Sources */ = {isa = PBXBuildFile; fileRef = B8C999AD1E012CAD00DB9F8C /* UIWebView+JKWebStorage.m */; };
BCB799F31AFDD95300C12525 /* UIImage+JKGIF.m in Sources */ = {isa = PBXBuildFile; fileRef = BCB799F21AFDD95300C12525 /* UIImage+JKGIF.m */; };
BCB799F61AFDDAA500C12525 /* UIButton+JKBackgroundColor.m in Sources */ = {isa = PBXBuildFile; fileRef = BCB799F51AFDDAA500C12525 /* UIButton+JKBackgroundColor.m */; };
BCB79A021AFDE0B600C12525 /* NSObject+JKBlockTimer.m in Sources */ = {isa = PBXBuildFile; fileRef = BCB79A011AFDE0B600C12525 /* NSObject+JKBlockTimer.m */; };
Expand Down Expand Up @@ -973,8 +974,8 @@
A2FD5B5F1A52708900555EA2 /* NSURL+JKParam.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSURL+JKParam.m"; sourceTree = "<group>"; };
A2FD5B611A52718200555EA2 /* NSDate+JKUtilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = "NSDate+JKUtilities.h"; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
A2FD5B621A52718200555EA2 /* NSDate+JKUtilities.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = "NSDate+JKUtilities.m"; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
A2FF6FE51D04334F00320C36 /* NSData+JKPCM.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSData+JKPCM.h"; sourceTree = "<group>"; };
A2FF6FE61D04334F00320C36 /* NSData+JKPCM.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSData+JKPCM.m"; sourceTree = "<group>"; };
A2FF6FE51D04334F00320C36 /* NSData+JKPCM.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = "NSData+JKPCM.h"; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
A2FF6FE61D04334F00320C36 /* NSData+JKPCM.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = "NSData+JKPCM.m"; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
B869399B1CA8EEB500EE7A1A /* UIButton+JKImagePosition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIButton+JKImagePosition.h"; sourceTree = "<group>"; };
B869399C1CA8EEB500EE7A1A /* UIButton+JKImagePosition.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIButton+JKImagePosition.m"; sourceTree = "<group>"; };
B87E87DF1DFAAFC7008EB83A /* UIWebView+JKLoadInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIWebView+JKLoadInfo.h"; sourceTree = "<group>"; };
Expand All @@ -985,10 +986,12 @@
B8C8D1351DED106A0069354F /* UIButton+JKBadge.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIButton+JKBadge.m"; sourceTree = "<group>"; };
B8C8D1371DED106F0069354F /* UIBarButtonItem+JKBadge.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIBarButtonItem+JKBadge.h"; sourceTree = "<group>"; };
B8C8D1381DED106F0069354F /* UIBarButtonItem+JKBadge.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIBarButtonItem+JKBadge.m"; sourceTree = "<group>"; };
B8C8D1401DED16D60069354F /* UITextField+JKInputLimit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UITextField+JKInputLimit.h"; sourceTree = "<group>"; };
B8C8D1411DED16D60069354F /* UITextField+JKInputLimit.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UITextField+JKInputLimit.m"; sourceTree = "<group>"; };
B8C8D1431DED18820069354F /* UITextView+JKInputLimit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UITextView+JKInputLimit.h"; sourceTree = "<group>"; };
B8C8D1441DED18820069354F /* UITextView+JKInputLimit.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UITextView+JKInputLimit.m"; sourceTree = "<group>"; };
B8C8D1401DED16D60069354F /* UITextField+JKInputLimit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = "UITextField+JKInputLimit.h"; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
B8C8D1411DED16D60069354F /* UITextField+JKInputLimit.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = "UITextField+JKInputLimit.m"; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
B8C8D1431DED18820069354F /* UITextView+JKInputLimit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = "UITextView+JKInputLimit.h"; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
B8C8D1441DED18820069354F /* UITextView+JKInputLimit.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = "UITextView+JKInputLimit.m"; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
B8C999AC1E012CAD00DB9F8C /* UIWebView+JKWebStorage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIWebView+JKWebStorage.h"; sourceTree = "<group>"; };
B8C999AD1E012CAD00DB9F8C /* UIWebView+JKWebStorage.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIWebView+JKWebStorage.m"; sourceTree = "<group>"; };
BCB799F11AFDD95300C12525 /* UIImage+JKGIF.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImage+JKGIF.h"; sourceTree = "<group>"; };
BCB799F21AFDD95300C12525 /* UIImage+JKGIF.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImage+JKGIF.m"; sourceTree = "<group>"; };
BCB799F41AFDDAA500C12525 /* UIButton+JKBackgroundColor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIButton+JKBackgroundColor.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2407,6 +2410,8 @@
A218D8DC1A47A25100AB83CA /* UIWebView+JKCanvas.m */,
A218D8DD1A47A25100AB83CA /* UIWebView+JKJavaScript.h */,
A218D8DE1A47A25100AB83CA /* UIWebView+JKJavaScript.m */,
B8C999AC1E012CAD00DB9F8C /* UIWebView+JKWebStorage.h */,
B8C999AD1E012CAD00DB9F8C /* UIWebView+JKWebStorage.m */,
);
path = UIWebView;
sourceTree = "<group>";
Expand Down Expand Up @@ -3096,6 +3101,7 @@
A28BE3321A3E9FDC005C4AC6 /* UILabel+JKAdjustableLabel.m in Sources */,
A28BE3371A3E9FDC005C4AC6 /* UIView+JKAnimation.m in Sources */,
A248782D1C55CC5C00AFF958 /* UIImage+JKSuperCompress.m in Sources */,
B8C999AE1E012CAD00DB9F8C /* UIWebView+JKWebStorage.m in Sources */,
BCB9547D1B0D829800E6BA78 /* NSData+JKDataCache.m in Sources */,
A2CFAEF81A56D15C0004A282 /* UIColor+JKModify.m in Sources */,
A21F34BD1B0F221400D73A91 /* UINavigationItem+JKLock.m in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion JKCategories/Foundation/NSData/NSData+JKPCM.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// NSData+JKPCM.h
// JKCategories-Demo
// JKCategories (https://github.com/shaojiankui/JKCategories)
//
// Created by Jakey on 16/6/5.
// Copyright © 2016年 www.skyfox.org. All rights reserved.
Expand Down
2 changes: 1 addition & 1 deletion JKCategories/Foundation/NSData/NSData+JKPCM.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// NSData+JKPCM.m
// JKCategories-Demo
// JKCategories (https://github.com/shaojiankui/JKCategories)
//
// Created by Jakey on 16/6/5.
// Copyright © 2016年 www.skyfox.org. All rights reserved.
Expand Down
2 changes: 1 addition & 1 deletion JKCategories/Foundation/NSObject/NSObject+JKKVOBlocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ typedef void (^JKKVOBlock)(NSDictionary *change, void *context);

-(void)jk_removeBlockObserverForKeyPath:(NSString *)keyPath;

@end
@end
1 change: 1 addition & 0 deletions JKCategories/JKUIKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,6 @@
#import "UIWebView+JKStyle.h"
#import "UIWebVIew+JKSwipeGesture.h"
#import "UIWebView+JKLoadInfo.h"
#import "UIWebView+JKWebStorage.h"
#import "UIWindow+JKHierarchy.h"
#endif /* JKUIKit_h */
4 changes: 2 additions & 2 deletions JKCategories/UIKit/UITextField/UITextField+JKInputLimit.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//
// UITextField+JKInputLimit.h
// JKCategories-Demo
// JKCategories (https://github.com/shaojiankui/JKCategories)
//
// Created by runlin on 2016/11/29.
// Created by Jakey on 2016/11/29.
// Copyright © 2016年 www.skyfox.org. All rights reserved.
//

Expand Down
4 changes: 2 additions & 2 deletions JKCategories/UIKit/UITextField/UITextField+JKInputLimit.m
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//
// UITextField+JKInputLimit.m
// JKCategories-Demo
// JKCategories (https://github.com/shaojiankui/JKCategories)
//
// Created by runlin on 2016/11/29.
// Created by Jakey on 2016/11/29.
// Copyright © 2016年 www.skyfox.org. All rights reserved.
//

Expand Down
4 changes: 2 additions & 2 deletions JKCategories/UIKit/UITextView/UITextView+JKInputLimit.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//
// UITextView+JKInputLimit.h
// JKCategories-Demo
// JKCategories (https://github.com/shaojiankui/JKCategories)
//
// Created by runlin on 2016/11/29.
// Created by Jakey on 2016/11/29.
// Copyright © 2016年 www.skyfox.org. All rights reserved.
//

Expand Down
4 changes: 2 additions & 2 deletions JKCategories/UIKit/UITextView/UITextView+JKInputLimit.m
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//
// UITextView+JKInputLimit.m
// JKCategories-Demo
// JKCategories (https://github.com/shaojiankui/JKCategories)
//
// Created by runlin on 2016/11/29.
// Created by Jakey on 2016/11/29.
// Copyright © 2016年 www.skyfox.org. All rights reserved.
//

Expand Down
1 change: 0 additions & 1 deletion JKCategories/UIKit/UITextView/UITextView+JKPlaceHolder.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@
#import <objc/runtime.h>
@interface UITextView (JKPlaceHolder) <UITextViewDelegate>
@property (nonatomic, strong) UITextView *jk_placeHolderTextView;
//@property (nonatomic, assign) id <UITextViewDelegate> textViewDelegate;
- (void)jk_addPlaceHolder:(NSString *)placeHolder;
@end
23 changes: 15 additions & 8 deletions JKCategories/UIKit/UITextView/UITextView+JKPlaceHolder.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@

#import "UITextView+JKPlaceHolder.h"
static const char *jk_placeHolderTextView = "jk_placeHolderTextView";

@implementation UITextView (JKPlaceHolder)
- (UITextView *)jk_placeHolderTextView {
return objc_getAssociatedObject(self, jk_placeHolderTextView);
}
- (void)setJk_placeHolderTextView:(UITextView *)placeHolderTextView {
objc_setAssociatedObject(self, jk_placeHolderTextView, placeHolderTextView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}


- (void)jk_addPlaceHolder:(NSString *)placeHolder {
if (![self jk_placeHolderTextView]) {
self.delegate = self;
UITextView *textView = [[UITextView alloc] initWithFrame:self.bounds];
textView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
textView.font = self.font;
Expand All @@ -27,20 +29,25 @@ - (void)jk_addPlaceHolder:(NSString *)placeHolder {
textView.text = placeHolder;
[self addSubview:textView];
[self setJk_placeHolderTextView:textView];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidBeginEditing:) name:UITextViewTextDidBeginEditingNotification object:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidEndEditing:) name:UITextViewTextDidEndEditingNotification object:self];

}
self.jk_placeHolderTextView.text = placeHolder;
}
# pragma mark -
# pragma mark - UITextViewDelegate
- (void)textViewDidBeginEditing:(UITextView *)textView {
- (void)textViewDidBeginEditing:(NSNotification *)noti {
self.jk_placeHolderTextView.hidden = YES;
// if (self.textViewDelegate) {
//
// }
}
- (void)textViewDidEndEditing:(UITextView *)textView {
if (textView.text && [textView.text isEqualToString:@""]) {
- (void)textViewDidEndEditing:(UITextView *)noti {
if (self.text && [self.text isEqualToString:@""]) {
self.jk_placeHolderTextView.hidden = NO;
}
}

- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
9 changes: 9 additions & 0 deletions JKCategories/UIKit/UIWebView/UIWebView+JKJavaScript.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,13 @@
- (void)jk_setFontColor:(UIColor *) color withTag:(NSString *)tagName;
/// 改变指定标签的字体大小
- (void)jk_setFontSize:(int) size withTag:(NSString *)tagName;

#pragma mark 删除
///根据 ElementsID 删除WebView 中的节点
- (void )jk_deleteNodeByElementID:(NSString *)elementID;
/// 根据 ElementsClass 删除 WebView 中的节点
- (void )jk_deleteNodeByElementClass:(NSString *)elementClass;
///根据 TagName 删除 WebView 的节点
- (void)jk_deleteNodeByTagName:(NSString *)elementTagName;

@end
40 changes: 40 additions & 0 deletions JKCategories/UIKit/UIWebView/UIWebView+JKJavaScript.m
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,44 @@ - (void)jk_setFontSize:(int)size withTag:(NSString *)tagName
nodes[i].style.fontSize = '%dpx';}", tagName, size];
[self stringByEvaluatingJavaScriptFromString:jsString];
}

#pragma mark -
#pragma mark 删除
/**
* 根据 ElementsID 删除WebView 中的节点
*/
- (void )jk_deleteNodeByElementID:(NSString *)elementID
{
[self stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('%@').remove();",elementID]];
}
/**
* 根据 ElementsClass 删除 WebView 中的节点
*/
- (void)jk_deleteNodeByElementClass:(NSString *)elementClass
{
NSString *javaScriptString = [NSString stringWithFormat:@"\
function getElementsByClassName(n) {\
var classElements = [],allElements = document.getElementsByTagName('*');\
for (var i=0; i< allElements.length; i++ )\
{\
if (allElements[i].className == n) {\
classElements[classElements.length] = allElements[i];\
}\
}\
for (var i=0; i<classElements.length; i++) {\
classElements[i].style.display = \"none\";\
}\
}\
getElementsByClassName('%@')",elementClass];
[self stringByEvaluatingJavaScriptFromString:javaScriptString];
}
/**
* 根据 ElementsClassTagName 删除 WebView 的节点
*/
- (void)jk_deleteNodeByTagName:(NSString *)elementTagName
{
NSString *javaScritptString = [NSString stringWithFormat:@"document.getElementByTagName('%@').remove();",elementTagName];
[self stringByEvaluatingJavaScriptFromString:javaScritptString];
}

@end
32 changes: 32 additions & 0 deletions JKCategories/UIKit/UIWebView/UIWebView+JKWebStorage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// UIWebView+JKWebStorage.h
// JKCategories (https://github.com/shaojiankui/JKCategories)
//
// Created by Jakey on 2016/12/14.
// Copyright © 2016年 www.skyfox.org. All rights reserved.
// https://github.com/cprime/UIWebView-WebStorage

#import <UIKit/UIKit.h>

@interface UIWebView (JKWebStorage)
#pragma mark - Local Storage

- (void)jk_setLocalStorageString:(NSString *)string forKey:(NSString *)key;

- (NSString *)jk_localStorageStringForKey:(NSString *)key;

- (void)jk_removeLocalStorageStringForKey:(NSString *)key;

- (void)jk_clearLocalStorage;

#pragma mark - Session Storage

- (void)jk_setSessionStorageString:(NSString *)string forKey:(NSString *)key;

- (NSString *)jk_sessionStorageStringForKey:(NSString *)key;

- (void)jk_removeSessionStorageStringForKey:(NSString *)key;

- (void)jk_clearSessionStorage;

@end
69 changes: 69 additions & 0 deletions JKCategories/UIKit/UIWebView/UIWebView+JKWebStorage.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// UIWebView+JKWebStorage.m
// JKCategories (https://github.com/shaojiankui/JKCategories)
//
// Created by Jakey on 2016/12/14.
// Copyright © 2016年 www.skyfox.org. All rights reserved.
//

#import "UIWebView+JKWebStorage.h"

static NSString * const jk_kLocalStorageName = @"localStorage";
static NSString * const jk_kSessionStorageName = @"sessionStorage";


@implementation UIWebView (JKWebStorage)
#pragma mark - Local Storage

- (void)jk_setLocalStorageString:(NSString *)string forKey:(NSString *)key {
[self jk_ip_setString:string forKey:key storage:jk_kLocalStorageName];
}

- (NSString *)jk_localStorageStringForKey:(NSString *)key {
return [self jk_ip_stringForKey:key storage:jk_kLocalStorageName];
}

- (void)jk_removeLocalStorageStringForKey:(NSString *)key {
[self jk_ip_removeStringForKey:key storage:jk_kLocalStorageName];
}

- (void)jk_clearLocalStorage {
[self jk_ip_clearWithStorage:jk_kLocalStorageName];
}

#pragma mark - Session Storage

- (void)jk_setSessionStorageString:(NSString *)string forKey:(NSString *)key {
[self jk_ip_setString:string forKey:key storage:jk_kSessionStorageName];
}

- (NSString *)jk_sessionStorageStringForKey:(NSString *)key {
return [self jk_ip_stringForKey:key storage:jk_kSessionStorageName];
}

- (void)jk_removeSessionStorageStringForKey:(NSString *)key {
[self jk_ip_removeStringForKey:key storage:jk_kSessionStorageName];
}

- (void)jk_clearSessionStorage {
[self jk_ip_clearWithStorage:jk_kSessionStorageName];
}

#pragma mark - Helpers

- (void)jk_ip_setString:(NSString *)string forKey:(NSString *)key storage:(NSString *)storage {
[self stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"%@.setItem('%@', '%@');", storage, key, string]];
}

- (NSString *)jk_ip_stringForKey:(NSString *)key storage:(NSString *)storage {
return [self stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"%@.getItem('%@');", storage, key]];
}

- (void)jk_ip_removeStringForKey:(NSString *)key storage:(NSString *)storage {
[self stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"%@.removeItem('%@');", storage, key]];
}

- (void)jk_ip_clearWithStorage:(NSString *)storage {
[self stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"%@.clear();", storage]];
}
@end

0 comments on commit 89b64c2

Please sign in to comment.