iOS单例

以WeiboHelper单例为例子:

头文件:

 1 #import <Foundation/Foundation.h>
 2 
 3 #import "WeiboSDK.h"
 4 
 5 
 6 
 7 @interface WeiboHelper : NSObject<WeiboSDKDelegate>
 8 
 9  
10 
11 + (WeiboHelper *)sharedWeiboHelper;
12 
13  
14 
15 - (void) WBAuthorize;
16 
17 - (void)sendTextContent:(NSString *)text;
18 
19 - (void)sendTextContent:(NSString *)text imageData:(NSData *)imageData;
20 
21  
22 
23 @end

.m实现文件

  1 #import "WeiboHelper.h"
  2 
  3 #define kWEIBOAPPKEY        @"1742124337"
  4 #define kRedirectURI        @"http://"
  5 
  6 
  7 @implementation WeiboHelper
  8 
  9 #pragma mark - Singleton life cycle
 10 + (WeiboHelper *)sharedWeiboHelper
 11 {
 12     static WeiboHelper *weiboHelper = nil;
 13     static dispatch_once_t onceToken;
 14     dispatch_once(&onceToken, ^{
 15         weiboHelper = [[super allocWithZone:NULL] init];
 16         [WeiboSDK registerApp:kWEIBOAPPKEY];
 17         [WeiboSDK enableDebugMode:YES];
 18     });
 19     return weiboHelper;
 20 }
 21 
 22 + (id)allocWithZone:(NSZone *)zone
 23 {
 24     return [[self sharedWeiboHelper] retain];
 25 }
 26 
 27 - (id) copyWithZone:(NSZone *)zone
 28 {
 29     return self;
 30 }
 31 
 32 - (id) retain
 33 {
 34     return self;
 35 }
 36 
 37 - (unsigned) retainCount
 38 {
 39     return UINT_MAX;
 40 }
 41 
 42 - (oneway void) release
 43 {
 44     
 45 }
 46 
 47 - (id) autorelease
 48 {
 49     return self;
 50 }
 51 
 52 #pragma mark - user method
 53 - (void)WBAuthorize
 54 {
 55     WBAuthorizeRequest *request = [WBAuthorizeRequest request];
 56     request.redirectURI = kRedirectURI;
 57     request.scope = @"all";
 58     request.userInfo = @{@"SSO_From": @"ViewController",
 59                          @"Other_Info_1": [NSNumber numberWithInt:123],
 60                          @"Other_Info_2": @[@"obj1", @"obj2"],
 61                          @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
 62     [WeiboSDK sendRequest:request];
 63 }
 64 
 65 - (void)sendTextContent:(NSString *)text
 66 {
 67     WBMessageObject *message = [WBMessageObject message];
 68     NSString *share = [[NSUserDefaults standardUserDefaults] objectForKey:@"share"];
 69     message.text = [NSString stringWithFormat:@"%@ %@", text, share];
 70     WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:message];
 71     [WeiboSDK sendRequest:request];
 72 }
 73 
 74 - (void)sendTextContent:(NSString *)text imageData:(NSData *)imageData
 75 {
 76     WBImageObject *imageObject = [WBImageObject object];
 77     imageObject.imageData = imageData;
 78     
 79     WBMessageObject *message = [WBMessageObject message];
 80     NSString *share = [[NSUserDefaults standardUserDefaults] objectForKey:@"share"];
 81     message.text = [NSString stringWithFormat:@"%@ %@", text, share];
 82     message.imageObject = imageObject;
 83     
 84     WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:message];
 85     [WeiboSDK sendRequest:request];
 86 }
 87 
 88 #pragma mark - WeiboSDKDelegate
 89 /**
 90  收到一个来自微博客户端程序的请求
 91  
 92  收到微博的请求后,第三方应用应该按照请求类型进行处理,处理完后必须通过 [WeiboSDK sendResponse:] 将结果回传给微博
 93  @param request 具体的请求对象
 94  */
 95 - (void)didReceiveWeiboRequest:(WBBaseRequest *)request
 96 {
 97     
 98 }
 99 
100 /**
101  收到一个来自微博客户端程序的响应
102  
103  收到微博的响应后,第三方应用可以通过响应类型、响应的数据和 [WBBaseResponse userInfo] 中的数据完成自己的功能
104  @param response 具体的响应对象
105  */
106 - (void)didReceiveWeiboResponse:(WBBaseResponse *)response
107 {
108     switch (response.statusCode) {
109         case WeiboSDKResponseStatusCodeSuccess:
110             //成功
111             break;
112         case WeiboSDKResponseStatusCodeUserCancel:
113             //用户取消发送
114             break;
115         case WeiboSDKResponseStatusCodeSentFail:
116             //发送失败
117             break;
118         case WeiboSDKResponseStatusCodeAuthDeny:
119             //授权失败
120             break;
121         case WeiboSDKResponseStatusCodeUserCancelInstall:
122             //用户取消安装微博客户端
123             break;
124         case WeiboSDKResponseStatusCodeUnsupport:
125             //不支持的请求
126             break;
127         case WeiboSDKResponseStatusCodeUnknown:
128             //未知
129             break;
130         default:
131             break;
132     }
133 }
134 
135 @end

 

 

posted @ 2013-12-17 11:13  坤坤同学  阅读(1914)  评论(0编辑  收藏  举报