Just a Computer Graphics Studio & My Life

[iOS] 使用AFNetworking

一個互動式的應用,幾乎跟網路 (Network)脫離不了關係!於是把網路處理好是最基本的事情,在此記錄最簡便的模式,以便未來可以直接使用~

現在最著名的第三方套件是AFNetworking,可以在GitHub取得,或者使用CocoaPods下載。

AFNetworking

先建立好NetworkManager,之後新增、修改、刪除API都可以在此class中進行!

/**
 Theme: Using AFNetworking
 IDE: Xcode 5
 Language: Objective C
 Date: 103/05/27
 Author: HappyMan
 Blog: https://cg2010studio.wordpress.com/
 */
// in GGNetworkManager.h
#import <Foundation/Foundation.h>
@interface GGNetworkManager : NSObject

@property (nonatomic, copy) void (^finishBlock) (NSObject *);
@property (nonatomic, copy) void (^failBlock) (NSString *, int);

- (id)initWithFinishBlock:(void (^)(NSObject *))finishBlockToRun failBlock:(void (^)(NSString *, int))failBlockToRun;

+ (GGNetworkManager *)requestWithFinishBlock:(void (^)(NSObject *objcet))finishBlockToRun failBlock:(void (^)(NSString *errStr, int errCode))failBlockToRun;

+ (GGNetworkManager *)sharedManager;

-(void)fetchYouBikeRealTimeInfo;

@end

// in GGNetworkManager.m
#import "GGNetworkManager.h"
#import "AFNetworking.h"

@implementation GGNetworkManager

- (id)initWithFinishBlock:(void (^)(NSObject *))finishBlockToRun failBlock:(void (^)(NSString *, int))failBlockToRun
{
    self = [super init];
    if (self) {
        self.finishBlock = finishBlockToRun;
        self.failBlock = failBlockToRun;
    }
    return self;
}

+ (GGNetworkManager *)requestWithFinishBlock:(void (^)(NSObject *objcet))finishBlockToRun failBlock:(void (^)(NSString *errStr, int errCode))failBlockToRun
{
    return [[GGNetworkManager alloc] initWithFinishBlock:finishBlockToRun failBlock:failBlockToRun];
}

+ (GGNetworkManager *)sharedManager
{
    static GGNetworkManager *sharedManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedManager = [[GGNetworkManager alloc] init];
    });
    return sharedManager;
}

-(void)fetchYouBikeRealTimeInfo
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *parameters = @{};
    [manager POST:@"http://210.69.61.60:8080/you/gwjs_cityhall.json"
       parameters:parameters
          success:^(AFHTTPRequestOperation *operation, id responseObject) {
              NSLog(@"JSON: %@", responseObject);
              // 塞到finishBlock傳給使用的class
              self.finishBlock(responseObject);
    }
          failure:^(AFHTTPRequestOperation *operation, NSError *error) {
              NSLog(@"Error: %@", error);
              // 塞到failBlock傳給使用的class
              self.failBlock(error.description, error.code);
    }];
}

// in any view controller
    [[GGNetworkManager requestWithFinishBlock:^(NSObject *object) {
        DxLog(@"object: %@", object);
    } failBlock:^(NSString *errStr, int errCode) {
        DxLog(@"errStr: %@", errStr);
    }] fetchYouBikeRealTimeInfo];

此範例是去北市政府開放資料,利用Youbike開放API來取得其車輛的即時資料。

以後都照這個模式來請求網路服務吧~

要注意的是,資深工程師吩咐不要用Singleton,因為每次向網路服務取得資料很少會重複利用,因此不必實現單一實體來保留資料。

參考:AFNetworking ReferenceAFNetworking – a delightful networking framework for iOS and OSX

Advertisements

發表留言

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s

標籤雲

%d 位部落客按了讚: