码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发小知识

时间:2015-07-31 17:55:37      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

不是什么非常高深的知识、只是记载开发中用到过的一些小技巧。随时更新!

 

1、UILabel根据文字数量确定其Frame

//定义UILable宽度最大值
CGFloat contentMaxW = 300;
//获取UILabel的Size,注意传入的Font大小要与UILabel的的Font大小保持相同
CGSize contentSize = [content boundingRectWithSize:CGSizeMake(contentMaxW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;

//注意要设置UILable的numberOfLines属性为0,才能显示多行文字
contentLabel.numberOfLines = 0;

 2、UILabel显示HTML代码字符串

NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlstr dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
contentLabel.attributedText = attrStr;

 3、使用AFN封装网络层

AFN非常优秀的HTTP网络框架,一般在项目中我们会在其基础上封装网络层。在这一层处理传入的url和参数,例如数据加密。

//YQHttpTool.h
+ (void)post:(NSString *)url params:(NSDictionary *)params success:(void (^)(id json))success failure:(void (^)(NSError *error))failure;
+ (void)get:(NSString *)url params:(NSDictionary *)params success:(void (^)(id json))success failure:(void (^)(NSError *error))failure;
 
//YQHttpTools.m
+ (void)post:(NSString *)url params:(NSDictionary *)params auth:(NSDictionary *)auth success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
   AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
   mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
   [mgr POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
       if(success){
           success(responseObject);
       }
   } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
       if(failure){
           failure(error);
       }
   }];
}
 
+ (void)get:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure { AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager]; mgr.responseSerializer = [AFHTTPResponseSerializer serializer]; [mgr GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { if(success){ success(responseObject); } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { if(failure){ failure(error); } }]; }

 4、检查更新

//appID为应用的appid,建议定义成宏
define appID @"xxxxxxxxx"

- (void)checkUpdate
{
    NSString *url = [NSString stringWithFormat:@"https://itunes.apple.com/lookup?id=%@", appID];
    [YQHttpTool getWithURL:url params:nil  success:^(id json) {
        id jsonObject = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingAllowFragments error:nil];
        if ([jsonObject isKindOfClass:[NSDictionary class]]) {
            NSDictionary *dic = jsonObject;
            if (nil != dic) {
                NSArray *result_array = [dic objectForKey:@"results"];
                if (nil != result_array && [result_array count] != 0) {
                    NSDictionary *result_dic = [result_array objectAtIndex:0];
                    if (nil != result_dic) {
                        NSString *version = [result_dic objectForKey:@"version"];
                        NSString *releaseNotes = [result_dic objectForKey:@"releaseNotes"];
                        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"];
                        NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfFile: filePath];
                        NSString *oldVersion = [dict objectForKey: @"CFBundleShortVersionString"];
                        if ([version compare:oldVersion options:NSNumericSearch] == NSOrderedDescending) {
                            UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"升级提示"
                                                                         message:releaseNotes
                                                                        delegate:self       //委托给Self,才会执行上面的调用
                                                               cancelButtonTitle:@"以后再说"
                                                               otherButtonTitles:@"马上更新",nil];
                            [av show];
                        }
                    }
                }
            }
        }
    } failure:^(NSError *error) {
        
    }];
}
//遵循<UIAlertView>

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {

    if (buttonIndex != [alertView cancelButtonIndex])

    {

        NSString *str = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/id%@?mt=8", appID];

        NSURL *url = [NSURL URLWithString:str];

        [[UIApplication sharedApplication] openURL:url];

    }

}

 

 

iOS开发小知识

标签:

原文地址:http://www.cnblogs.com/c-y-q/p/4692521.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!