tabBarItem.selectedImage不起作用

swift tabBarController中设置tabBarItem.image 是正常,但是设置tabBarItem.selectedImage发现是默认的蓝色,图标没有变化,最后查到资料需要在Assets该图片default改为Original即可,特此记录

cordova加载服务器H5页面,js放本地

首先需要改造一下cordova文件源码
CDVURLProtocol.m这个文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//重写方法
-(void)startLoading
{
//处理自定义标签 ,并实现内嵌本地资源
NSLog(@"startLoading");
NSLog(@"%@", super.request.URL);
NSString *url=super.request.URL.resourceSpecifier;// 得到//image1.png"
//去掉 //前缀()
url=[url substringFromIndex:2];//image1.png
NSString *path= [[NSBundle mainBundle] pathForResource:url ofType:nil];//这里是获取本地资源路径 如 :png,js 等
if (!path) {
[self sendResponseWithResponseCode:401 data:nil mimeType:nil];//重要
return;
}
//根据路径获取MIMEType (以下函数方法需要添加.h文件的引用,)
// Get the UTI from the file's extension:
CFStringRef pathExtension = (__bridge_retained CFStringRef)[path pathExtension];
CFStringRef type = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension, NULL);
CFRelease(pathExtension);
// The UTI can be converted to a mime type:
NSString *mimeType = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass(type, kUTTagClassMIMEType);
if (type != NULL)
CFRelease(type);
// 这里需要用到MIMEType
NSURLResponse *response = [[NSURLResponse alloc] initWithURL:super.request.URL
MIMEType:mimeType
expectedContentLength:-1
textEncodingName:nil];
NSData *data = [NSData dataWithContentsOfFile:path];//加载本地资源
[self sendResponseWithResponseCode:200 data:data mimeType:mimeType];
//硬编码 开始嵌入本地资源到web中
[[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
[[self client] URLProtocol:self didLoadData:data];
[[self client] URLProtocolDidFinishLoading:self];
}

这里使用之前要注册一下

1
2
3
4
[NSURLProtocol registerClass:[CDVURLProtocol class]];
TestViewController *test = [[TestViewController alloc]init];
test.wwwFolderName = @"";
test.startPage = @"http://localhost/www/index.html";

把按照cordova官网教程做的demo中,插件拖进来,注意这里,一定要选择Create folder references
工程中显示蓝色文件夹,在cordova_plugins.js中,自己设置导入的插件目录,差不多就这样了

Cordova iOS使用心得(爬坑)

现在做个很老很老的项目,整个工程只有一个Controller,能想到这得多老的项目了吧,要集成Cordova,之前js交互实在很差,写一点爬坑过程
1、项目如果不是刚开始就集成Cordova,不建议使用官网那个教程,试了下,没成功
2、使用cocoapods安装的cordova 和插件

1
2
pod 'Cordova', '~> 4.5.4'
pod 'cordova-plugin-device', '~> 1.1.3'

3、使用官网教程新建demo,自己工程需要哪些插件,用官网命令在demo里面装上,platform里面www复制进项目
然后,将下面js原名称Run Script修改为Copy www directory;然后将show env那个选项去掉,最后将下面的代码字段复制到build phases:(如果h5放服务器,这段不需要)

1
NODEJS_PATH=/usr/local/bin; NVM_NODE_PATH=~/.nvm/versions/node/`nvm version 2>/dev/null`/bin; N_NODE_PATH=`find /usr/local/n/versions/node/* -maxdepth 0 -type d 2>/dev/null | tail -1`/bin; XCODE_NODE_PATH=`xcode-select --print-path`/usr/share/xcs/Node/bin; PATH=$NODEJS_PATH:$NVM_NODE_PATH:$N_NODE_PATH:$XCODE_NODE_PATH:$PATH && node cordova/lib/copy-www-build-step.js

4、将config.xml拖到工程
5、自己的Controller继承CDVViewController,创建的时候这样写,如果www本地调试的话

1
2
3
TestViewController *test = [[TestViewController alloc]init];
test.wwwFolderName = @"www";
test.startPage = @"index.html";

如果www放服务器,可以这样写

1
2
3
TestViewController *test = [[TestViewController alloc]init];
test.wwwFolderName = @"";
test.startPage = @"http://localhost/www/index.html";

我这个放在我本地搭建的服务器了,所以写localhost,改成自己服务器地址即可,切记:这样写,是打开safary打开的,需要在config.xml中修改

1
2
3
4
<!-- <allow-intent href="http://*/*" />-->
<!-- <allow-intent href="https://*/*" />-->
<allow-navigation href="http://*/*" />
<allow-navigation href="https://*/*" />

这样基本可跑了。用的话去看官方怎么用就可以了,自定义插件一定要在config.xml中配上才行,例如我这个

1
2
3
4
<feature name="MyPlugin">
<param name="ios-package" value="MyPlugin" />
<param name="onload" value="true" />
</feature>

凭印象写,就这样

jsonmodel中出现系统关键词处理

服务器中返回数据中使用jsonModel解析转化为模型时,如遇到跟系统关键字或系统自有属性冲突,例如id等使用jsonmodel中的JSONKeyMapper类方法

1
2
3
+(JSONKeyMapper *)keyMapper{
return [[JSONKeyMapper alloc]initWithModelToJSONDictionary:@{@"自己设置的model属性":@"服务器返回的model属性"}];
}

swift automaticallyAdjustsScrollViewInsets空白间距问题

标签(空格分隔): swift automaticallyAdjustsScrollViewInsets


控制器中

1
self.automaticallyAdjustsScrollViewInsets = false

这样是没用的,必须在storyboard中Under Top Bar勾去掉可以解决

webView打开https,不验证证书

标签(空格分隔): webview https


只需要条件一个类方法即可,这个方法在整个app中任何一个文件实现都可以,我是做sdk遇到这个问题,demo中webview https打不开,在集成app中竟然可以,最好发现,app中有类写了这两句代码

1
2
3
4
5
6
7
@implementation NSURLRequest (NSURLRequestWithIgnoreSSL)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host
{
return YES;
}
@end

AFNetWorking支持https,不验证证书

tags: AFNetWorking https 不验证证书

直接上代码

AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
[securityPolicy setValidatesDomainName:NO];
[securityPolicy setAllowInvalidCertificates:YES];
_sharedInstance.securityPolicy = securityPolicy;

UITableView顶部有一块空白区域

上个项目一直用代码写控件,所以没有发现这个问题,今天没事写个h5的东西,用到xib,发现UITableView最上面一直有一块空白区域,搜了资料发现,iOS 7 viewcontroller新增属性automaticallyAdjustsScrollViewInsets,即是否根据按所在界面的navigationbar与tabbar的高度,自动调整scrollview的 inset,设置为no,让它不要自动调整就可以了,加一句代码就ok了

1
self.automaticallyAdjustsScrollViewInsets = NO;

statusBar状态栏消息滚动显示控件

利用状态栏来提示和显示状态,字数太多可以滚动显示完之后消失

- (void)showWithStatus:(NSString *)status barColor:(UIColor*)barColor textColor:(UIColor*)textColor{
if (_isshowing) {
    return;
}
_isshowing = YES;
if(!self.superview)
    [self.overlayWindow addSubview:self];
[self.overlayWindow setHidden:NO];
[self.topBar setHidden:NO];
self.topBar.backgroundColor = barColor;
NSString *labelText = status;
CGRect labelRect = CGRectZero;
CGFloat stringWidth = 0;
CGFloat stringHeight = 0;
if(labelText) {
    CGSize size = [labelText boundingRectWithSize:CGSizeMake((self.topBar.frame.size.width-20), 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.label4Title.font} context:nil].size;
    stringWidth = size.width;
    stringHeight = size.height;
    _timeInterval4Label = stringHeight/10;
    labelRect = CGRectMake(10, 0, stringWidth, stringHeight);
}
self.label4Title.frame = labelRect;
self.topBar.contentSize = CGSizeMake(self.topBar.frame.size.width, labelRect.size.height+20);
self.label4Title.alpha = 0.0;
self.label4Title.hidden = NO;
self.label4Title.text = labelText;
self.label4Title.textColor = textColor;
[UIView animateWithDuration:0.4 animations:^{
    self.label4Title.alpha = 1.0;
}];
[UIView animateWithDuration:0.4 animations:^{
    self.label4Title.alpha = 1.0;
} completion:^(BOOL finished) {
    [self beginScrollWithTimeOfTimer:_timeInterval4Label];
}];
[self setNeedsDisplay];}

使用起来相当简单 [WHPStatusBar showSuccessWithStatus:@”显示消息”];这一句就好

源码地址下载github