iOS开发之常⽤第三⽅框架(下载地址,使⽤⽅法,总结)
iOS开发之常⽤第三⽅框架(下载地址,使⽤⽅法,总结)
说句实话,⾃学了这么久iOS,如果说我不知道的但是⼜基本上都摸遍了iOS相关知识,但是每次做项⽬的时候,遇到难⼀点的地⽅或者没试过的东西就闷了。
⽐如这次,打算做⼀个着⼿做⼀个iOS的项⽬,是⼀个关于⽇计划的⼩软件,界⾯都其他的都算满意,⽹络就不说了,没有服务器,所以很多数据相关的功能不⽆法实现。
但是嘴头疼的事情就是,⽐如遇到⼀个功能的时候,其实如果说要实现的话还是可以的,但是每次在我实现之后我总会想到,这么实现更好,这么实现更简单,更加优化,虽然这么实现能够让我学到东西。
做完之后看了别⼈的相似代码才会知道原来第三⽅框架原来这么简单这么容易,⽽且代码这么少,我⼜换成第三⽅的框架来实现。
换来换去,换了⼀⼤堆,没办法只能这⾥点哪⾥点,搞了⼀天终于都弄完了。
现在我就总结⼀下第三⽅的框架,虽然⼤部分都是在别⼈那⾥搬过来的,但是笔者做了⼩量的优化更改还有就是增加⼀了⼀下最新的。
当然这⾥只是⼀些常⽤的别⼈总结过了的ios开发OC版的第三⽅,关于Swift的第三⽅现在不是很多,也没有太多很好的,再后⾯的⽂章我将会试着总结⼀下关于Swift的第三⽅框架的使⽤和总结,希望各位不要吐槽,也希望能对⼤家有⽤!
⼀:Reachability 检测⽹络连接
⽤来检查⽹络连接是否可⽤:包括WIFI和WWAN(3G/EDGE/CDMA等)两种⼯作模式。
1. <font color="rgb(51, 51, 51)">Reachability* reach = [Reachability reachabilityWithHostname:@"le"];
2. achableBlock = ^(Reachability*reach) {
3. NSLog(@"⽹络可⽤!");
4. };
5. reach.unreachableBlock = ^(Reachability*reach) {
6. NSLog(@"⽹络不可⽤!");
7. };
8. // 开始监听
9. [reach startNotifier];</font>
⼆:ASIHTTPRequest
1. NSURL *url = [NSURL URLWithString:@"allseeing-i"];
2. __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
3. [request setCompletionBlock:^{
4. // Use when fetching text data
5. NSString *responseString = [request responseString];
6.
7. // Use when fetching binary data
8. NSData *responseData = [request responseData];
9. }];
10. [request setFailedBlock:^{
11. NSError *error = [request error];
12. }];
13. [request startAsynchronous];
它的ASIFormDataRequest⼦类可以横容易的提交表单数据和⽂件:
1. ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
2. [request setPostValue:@"Ben" forKey:@"first_name"];
3. [request setPostValue:@"Copsey" forKey:@"last_name"];
4. // Upload a file on disk
5. [request setFile:@"/Users/ben/Desktop/ben.jpg" withFileName:@"myphoto.jpg" andContentType:@"image/jpeg"
6. forKey:@"photo"];
7. // Upload an NSData instance
8. [request setData:imageData withFileName:@"myphoto.jpg" andContentType:@"image/jpeg" forKey:@"photo"];
复制代码
1. @interface SampleViewController ()<MBProgressHUDDelegate>
2. {
3. MBProgressHUD *HUD;
4. }
5. #pragma mark -
6. #pragma mark MBProgressHUDDelegate methods
7.
8. - (void)hudWasHidden:(MBProgressHUD *)hud {
9. // Remove HUD from screen when the HUD was hidded
10. [HUD removeFromSuperview];
11. HUD = nil;
12. }
在执⾏某个异步请求时开始调⽤:
1. HUD = [MBProgressHUD showHUDAddedTo:self.webView animated:YES];
2. HUD.labelText = @"正在请求...";
3. // mode参数可以控制显⽰的模式
4. //de = MBProgressHUDModeText;
5. HUD.delegate = self;
请求完成时隐藏提⽰效果:
1. [HUD hide:YES];
对于同步⽅法⼀般都是⽤showWhileExecuting⽅法,⽅法执⾏完成之后会⾃动隐藏提⽰效果:
1. [HUD showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];
四:SVProgressHUD
1. [SVProgressHUD method]
可以使⽤以下⽅法来显⽰状态:
1. + (void)show;
2. + (void)showWithMaskType:(SVProgressHUDMaskType)maskType;
3. + (void)showWithStatus:(NSString*)string;
4. + (void)showWithStatus:(NSString*)string maskType:(SVProgressHUDMaskType)maskType;
如果需要明确的进度,则使⽤以下⽅法:
1. + (void)showProgress:(CGFloat)progress;
2. + (void)showProgress:(CGFloat)progress status:(NSString*)status;
3. + (void)showProgress:(CGFloat)progress status:(NSString*)status maskType:(SVProgressHUDMaskType)maskType;通过dismiss⽅法来隐藏提⽰:
1. + (void)dismiss;
1. + (void)showSuccessWithStatus:(NSString*)string;
2. + (void)showErrorWithStatus:(NSString *)string;
3. + (void)showImage:(UIImage*)image status:(NSString*)string; // use 28x28 white pngs
五:ZAActivityBar
1. [ZAActivityBar showWithStatus:@"加载中..."];
显⽰成功、失败状态:
1. [ZAActivityBar showSuccessWithStatus:@"成功!"];
2. [ZAActivityBar showErrorWithStatus:@"失败!"];
隐藏提⽰:
1. [ZAActivityBar dismiss];
六:SBJson
1. @interface TestViewController ()<SBJsonStreamParserAdapterDelegate> {
2. SBJsonStreamParser *parser;
3. SBJsonStreamParserAdapter *adapter;
4. }
5.
6. // 冗长的初始化⽅法⾜以吓到⼀⼤⽚⼈
7. - (void)initSBJSON
8. {
9. // We don't want *all* the individual messages from the
10. // SBJsonStreamParser, just the top-level objects. The stream
11. // parser adapter exists for this purpose.
12. adapter = [[SBJsonStreamParserAdapter alloc] init];
13.
14. // Set ourselves as the delegate, so we receive the messages
15. // from the adapter.
16. adapter.delegate = self;
17.
18. // Create a new stream parser..
19. parser = [[SBJsonStreamParser alloc] init];
20.
21. // .. and set our adapter as its delegate.
22. parser.delegate = adapter;
23.
24. // Normally it's an error if JSON is followed by anything but
25. // whitespace. Setting this means that the parser will be
26. // expecting the stream to contain multiple whitespace-separated
27. // JSON documents.
28. parser.supportMultipleDocuments = YES;
29. }
30.
31. #pragma mark SBJsonStreamParserAdapterDelegate methods
32.
33. - (void)parser:(SBJsonStreamParser *)parser foundArray:(NSArray *)array {
34. [NSException raise:@"unexpected" format:@"Should not get here"];
35. }
36.
37. - (void)parser:(SBJsonStreamParser *)parser foundObject:(NSDictionary *)dict {
38. NSLog(@"SBJson parser foundObject");
39. // 处理返回的数据view ui框架
40. }
41.
42. // 使⽤ASIHTTPRequest请求测试
43. - (void) loadData {
44. __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
45. [request setRequestMethod:@"POST"];
46. [request setCompletionBlock:^{
47. // Use when fetching text data
48. //NSString *responseString = [request responseString];
49. // Use when fetching binary data
50. NSData *responseData = [request responseData];
51. NSLog(@"Connection didReceiveData of length: %u", responseData.length);
52.
53. // Parse the new chunk of data. The parser will append it to
54. // its internal buffer, then parse from where it left off in
55. // the last chunk.
56. SBJsonStreamParserStatus status = [parser parse:responseData];
57.
58. if (status == SBJsonStreamParserError) {
59. NSLog(@"Parser error: %@", );
60. } else if (status == SBJsonStreamParserWaitingForData) {
61. NSLog(@"Parser waiting for more data");
62. }
63. }];
64. [request setFailedBlock:^{
65. NSError *error = [request error];
66. NSLog(@"failed - %@ %@", [error localizedDescription], error);
67. }];
68. [request startAsynchronous];
69. }
七:JSONKit
1. JSONDecoder* decoder = [[JSONDecoder alloc] initWithParseOptions:JKParseOptionNone];
2. id result = [decoder objectWithData:jsonData];
详细的使⽤⽅法请看它的GitHub主页。
⼋:SDWebImage
图⽚异步加载及缓存
SDWebImage⽤于异步下载⽹络上的图⽚,并⽀持对图⽚的缓存等。多数情况下是使⽤UIImageView+WebCache为UIImageView异步加载图⽚:
1. #import <SDWebImage/UIImageView+WebCache.h>
2. // ...
3. [cell.imageView setImageWithURL:[NSURL URLWithString:@"www.domain/path/to/image.jpg"]
4. placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
需要注意的是,pladeholderImage的⼤⼩⼀定要⼤于UIImageView的⼤⼩,否则可能不显⽰placeholderImage图⽚。它还⽀持block语法⽤于在加载完成时做⼀些操作:
1. [cell.imageView setImageWithURL:[NSURL URLWithString:@"www.domain/path/to/image.jpg"]
2. placeholderImage:[UIImage imageNamed:@"placeholder.png"]
3. completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {... completion code here ...}]; SDWebImage并不局限于UIImageView上,使⽤SDWebImageManager完成更多的操作:
1. SDWebImageManager *manager = [SDWebImageManager sharedManager];
2. [manager downloadWithURL:imageURL
3. options:0
4. progress:^(NSUInteger receivedSize, long long expectedSize)
5. {
6. // 下载进度
7. }
8. completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType)
9. {
10. if (image)
11. {
12. // 下载完成
13. }
14. }];
或者使⽤Image Downloader也是⼀样的效果:
1. [SDWebImageDownloader.sharedDownloader downloadImageWithURL:imageURL
2. options:0
3. progress:^(NSUInteger receivedSize, long long expectedSize)
4. {
5. // 进度
6. }
7. completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished)
8. {
9. if (image && finished)
10. {
11. // 下载完成
12. }
13. }];
九:UIActivityIndicator-for-SDWebImage
1. - (void)setImageWithURL:(NSURL *)url usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)activityStyle;
2. - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder usingActivityIndicatorStyle:
(UIActivityIndicatorViewStyle)activityStyle;
3. - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options
usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)activityStyle;
4. - (void)setImageWithURL:(NSURL *)url completed:(SDWebImageCompletedBlock)completedBlock usingActivityIndicatorStyle:
(UIActivityIndicatorViewStyle)activityStyle;
5. - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:
(SDWebImageCompletedBlock)completedBlock usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)activityStyle;
6. - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:
(SDWebImageCompletedBlock)completedBlock usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)activityStyle;
7. - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:
(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock
usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)activityStyle;
⼗:UIImage+Resize
1. - (UIImage *)imageWithAlpha;
2. - (UIImage *)transparentBorderImage:(NSUInteger)borderSize;
3. - (UIImage *)roundedCornerImage:(NSInteger)cornerSize borderSize:(NSInteger)borderSize;
4. - (UIImage *)croppedImage:(CGRect)bounds;
5. - (UIImage *)thumbnailImage:(NSInteger)thumbnailSize
6. transparentBorder:(NSUInteger)borderSize
7. cornerRadius:(NSUInteger)cornerRadius
8. interpolationQuality:(CGInterpolationQuality)quality;
9. - (UIImage *)resizedImage:(CGSize)newSize
10. interpolationQuality:(CGInterpolationQuality)quality;
11. - (UIImage *)
12. resizedImageWithContentMode:(UIViewContentMode)contentMode
13. bounds:(CGSize)bounds
14. interpolationQuality:(CGInterpolationQuality)quality;
1. - (void)setImageWithURL:(NSURL *)url andCropToBounds:(CGRect)bounds;
2. - (void)setImageWithURL:(NSURL *)url andResize:(CGSize)size withContentMode:(UIViewContentMode)mode;
3. - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder andCropToBounds:(CGRect)bounds;
4. - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:
(SDWebImageOptions)options andResize:(CGSize)size;
5. - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:
(SDWebImageOptions)options andResize:(CGSize)size withContentMode:(UIViewContentMode)mod
e;
6. - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:
(SDWebImageOptions)options andCropToBounds:(CGRect)bounds;
使⽤⽅法和SDWebImage⼀样简单,如以下官⽅例⼦:
1. [imageview setImageWithURL:[NSURL URLWithString:@"t0.gstatic/images?
q=tbn:ANd9GcQfraHpiabjEY8iDdBe9OUQYHMtwfuAv9ZRR0RYKuoVF_EpE8Fp5A"] andResize:CGSizeMake(30, 30)
withContentMode:UIViewContentModeScaleAspectFit]; // 按⽐例缩放
2. [imageview setImageWithURL:[NSURL URLWithString:@"t0.gstatic/images?
q=tbn:ANd9GcQfraHpiabjEY8iDdBe9OUQYHMtwfuAv9ZRR0RYKuoVF_EpE8Fp5A"] andCropToBounds:CGRectMake(0, 0, 100, 100)]; // 裁剪成100x100⼤⼩
⼗⼆:EGOTableViewPullRefresh
1. #import "EGORefreshTableHeaderView.h"
2.
3. @interface RootViewController : UITableViewController <EGORefreshTableHeaderDelegate, UITableViewDelegate,
UITableViewDataSource>{
4. EGORefreshTableHeaderView *_refreshHeaderView;
5. // 是否正在加载中
6. BOOL _reloading;
7. }
8.
9. - (void)viewDidLoad {
10. [super viewDidLoad];
11.
12. if (_refreshHeaderView == nil) {
13. EGORefreshTableHeaderView *view = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f -
self.tableView.bounds.size.height, self.view.frame.size.width, self.tableView.bounds.size.height)];
14. view.delegate = self;
15. [self.tableView addSubview:view];
16. _refreshHeaderView = view;
17. [view release];
18. }
19. // 更新最后加载时间
20. [_refreshHeaderView refreshLastUpdatedDate];
21. }
22.
23. #pragma mark -
24. #pragma mark Data Source Loading / Reloading Methods
25.
26. - (void)reloadTableViewDataSource{
27. // 在这⾥加⼊代码⽤于获取数据
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论