博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS单例
阅读量:5755 次
发布时间:2019-06-18

本文共 4217 字,大约阅读时间需要 14 分钟。

以WeiboHelper单例为例子:

头文件:

1 #import 
2 3 #import "WeiboSDK.h" 4 5 6 7 @interface WeiboHelper : NSObject
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 *)response107 {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

 

 

转载于:https://www.cnblogs.com/wanyakun/p/3478071.html

你可能感兴趣的文章
一个简单的接口,被调用并同步给出响应的方法
查看>>
Hadoop序列化与压缩
查看>>
由“男怕入错行”说开去
查看>>
php-fpm多实例运行
查看>>
CGImageSource对图像数据读取任务的抽象
查看>>
我的友情链接
查看>>
xss test
查看>>
也谈svn分支与合并
查看>>
显式锁(第十三章)
查看>>
微软超融合私有云测试29-SCDPM2016部署之创建保护组备份(备份虚拟机)
查看>>
LBS“他爹”GIS
查看>>
SCCM的证书配置PKI
查看>>
看linux书籍做的一些重要笔记(2011.07.03更新)
查看>>
CString、Char* ,char [20]、wchar_t、unsigned short转化
查看>>
从案例学RxAndroid开发(上)
查看>>
Redis学习手册(内存优化)
查看>>
浅尝TensorFlow on Kubernetes
查看>>
springboot系列十 Spring-Data-Redis
查看>>
Confluence 6 注册外部小工具
查看>>
excel进行矩阵计算
查看>>