完整的iOS新浪微博分享功能开发
程序员文章站
2023-12-20 15:00:16
本文实例为大家分享了android九宫格图片展示的具体代码,供大家参考,具体内容如下
做新浪分享 需先去http://open.weibo.com/apps注册开发者...
本文实例为大家分享了android九宫格图片展示的具体代码,供大家参考,具体内容如下
做新浪分享 需先去http://open.weibo.com/apps注册开发者app 很简单!
第1步
第2步
3
设置你的应用的信息
找到自己的appkey
还需要设置自己的kappredirecturl测试可以随便写个!
开发部分在下面ios新浪微博分享(2)这部分:
开发需要下载官方的sdkhttp://open.weibo.com/wiki/sdk#ios_sdk
本人下载的版本
新建一个viewcontrroler==weiboviewcontroller
效果图
h文件
#import #import "sinaweb/sinaweibo/sinaweibo.h" #import "sinaweb/sinaweibo/sinaweiborequest.h" @interface weiboviewcontroller : uiviewcontroller<</span>sinaweibodelegate,sinaweiborequestdelegate> { uibutton *_sharebutton; uitextview *_textview; uiview *_shareview; uiactivityindicatorview *_indicator;} @property (strong, nonatomic) uibutton *sharebutton; @property (strong, nonatomic) uitextview *textview; @property (strong, nonatomic) uiview *shareview; @property (strong, nonatomic) uiactivityindicatorview *indicator; @property (readonly, nonatomic) sinaweibo *sinaweibo; - (void) addbutton; - (void) addshareview; - (void) share:(uibutton*) sender; - (void) removeshare:(uibutton*) sender; - (void) sendshare:(uibutton*) sender; - (void) exitshare:(uibutton*) sender; @end
m文件
#import "weiboviewcontroller.h" #define kappkey @"appkey" #define kappsecret @"appsecret" #define kappredirecturl @"重定向url" @interface weiboviewcontroller () @end @implementation weiboviewcontroller @synthesize sharebutton = _sharebutton; @synthesize textview = _textview; @synthesize shareview = _shareview; @synthesize indicator = _indicator; @synthesize sinaweibo = _sinaweibo; - (sinaweibo*)sinaweibo { _sinaweibo.delegate=self; return _sinaweibo; } - (void)viewdidload { [super viewdidload]; _indicator = [[uiactivityindicatorview alloc] initwithactivityindicatorstyle:uiactivityindicatorviewstylewhitelarge]; [_indicator setframe:cgrectmake(0, 0, 50, 50)]; _indicator.center = self.view.center; [self.view addsubview:_indicator]; _sinaweibo = [[sinaweibo alloc] initwithappkey:kappkey appsecret:kappsecret appredirecturi:kappredirecturl anddelegate:self]; nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults]; nsdictionary *sinaweiboinfo = [defaults objectforkey:@"sinaweiboauthdata"]; if ([sinaweiboinfo objectforkey:@"accesstokenkey"] && [sinaweiboinfo objectforkey:@"expirationdatekey"] && [sinaweiboinfo objectforkey:@"useridkey"]) { _sinaweibo.accesstoken = [sinaweiboinfo objectforkey:@"accesstokenkey"]; _sinaweibo.expirationdate = [sinaweiboinfo objectforkey:@"expirationdatekey"]; _sinaweibo.userid = [sinaweiboinfo objectforkey:@"useridkey"]; } [self addbutton]; } - (void) addbutton { _sharebutton = [uibutton buttonwithtype:uibuttontyperoundedrect]; uiimage*butimg=[uiimage imagenamed:@"button_background@2x.png"]; uiimage*logobutimg=[uiimage imagenamed:@"logo@2x.png"]; [self.sharebutton setframe:cgrectmake(10, 10, butimg.size.width, butimg.size.height)]; [self.sharebutton setbackgroundimage:butimg forstate:uicontrolstatenormal]; [self.sharebutton setimage:logobutimg forstate:uicontrolstatenormal]; [self.sharebutton addtarget:self action:@selector(share:) forcontrolevents:uicontroleventtouchupinside]; [self.view addsubview:self.sharebutton]; } //分享按钮响应方法 - (void) share:(uibutton*) sender { sinaweibo *sinaweibo = [self sinaweibo]; bool authvalid = sinaweibo.isauthvalid; if (!authvalid) { [sinaweibo login]; } else { nsstring *poststatustext = @"[哈哈]"; sinaweibo *sinaweibo = [self sinaweibo]; //只发送汉字 // [sinaweibo requestwithurl:@"statuses/update.json" params:[nsmutabledictionary dictionarywithobjectsandkeys:poststatustext,@"status", nil] httpmethod:@"post" delegate:self]; //图片和连接 和文字 [sinaweibo requestwithurl:@"statuses/upload.json" params:[nsmutabledictionary dictionarywithobjectsandkeys: @"要发布的微博文本内容,超链接http://baidu.com", @"status", [uiimage imagenamed:@"icon.png"], @"pic", nil] httpmethod:@"post" delegate:self]; [_shareview removefromsuperview]; [self.indicator startanimating]; } } //登陆成功后回调方法 - (void) sinaweibodidlogin:(sinaweibo *)sinaweibo { nslog(@"%@--%@--%@--%@",sinaweibo.accesstoken,sinaweibo.expirationdate, sinaweibo.userid,sinaweibo.refreshtoken); nsdictionary *authdata = [nsdictionary dictionarywithobjectsandkeys: sinaweibo.accesstoken, @"accesstokenkey", sinaweibo.expirationdate, @"expirationdatekey", sinaweibo.userid, @"useridkey", sinaweibo.refreshtoken, @"refresh_token", nil]; [[nsuserdefaults standarduserdefaults] setobject:authdata forkey:@"sinaweiboauthdata"]; [[nsuserdefaults standarduserdefaults] synchronize]; //可以在此选在授权成功后直接发送 } //取消按钮回调方法 - (void) removeshare:(uibutton*) sender { [_shareview removefromsuperview]; } //发送按钮回调方法 - (void) sendshare:(uibutton*) sender { nsstring *poststatustext = self.textview.text; sinaweibo *sinaweibo = [self sinaweibo]; [sinaweibo requestwithurl:@"statuses/updates.json" params:[nsmutabledictionary dictionarywithobjectsandkeys:poststatustext,@"status", nil] httpmethod:@"post" delegate:self]; [_shareview removefromsuperview]; [self.indicator startanimating]; } //退出登陆回调方法 - (void) exitshare:(uibutton*) sender { sinaweibo *sinaweibo = [self sinaweibo]; [sinaweibo logout]; [_shareview removefromsuperview]; nslog(@"退出登陆"); } //请求完成回调该方法 - (void)request:(sinaweiborequest *)request didfinishloadingwithresult:(id)result { [self.indicator stopanimating]; uialertview* alert = [[uialertview alloc] initwithtitle:@"发送成功" message:@"提示" delegate:self cancelbuttontitle:@"确定" otherbuttontitles: nil]; [alert show]; [alert release]; nslog(@"发送成功"); } //请求失败回调该方法 - (void)request:(sinaweiborequest *)request didfailwitherror:(nserror *)error { [self.indicator stopanimating]; uialertview* alert = [[uialertview alloc] initwithtitle:@"发送失败,请检测网络链接" message:@"提示" delegate:self cancelbuttontitle:@"确定" otherbuttontitles: nil]; [alert show]; [alert release]; nslog(@"发送失败"); } - (void)viewdidunload { [super viewdidunload]; // release any retained subviews of the main view. } - (bool)shouldautorotatetointerfaceorientation:(uiinterfaceorientation)interfaceorientation { return (interfaceorientation != uiinterfaceorientationportraitupsidedown); } @end
源码下载:http://xiazai.jb51.net/201611/yuanma/sinaweiboshare(jb51.net).rar
个人信息获得
@interface userinfoviewcontroller : uiviewcontroller<sinaweiborequestdelegate>//实现请求代理 bool authvalid = self.sina.isauthvalid;//判断是否授权了 if (authvalid){ [self.sina requestwithurl:@"users/show.json" params:[nsmutabledictionarydictionarywithobject:self.sina.userid forkey:@"uid"] httpmethod:@"get" delegate:self]; } //请求失败回调方法 - (void)request:(sinaweiborequest *)request didfailwitherror:(nserror *)error{ if ([request.url hassuffix:@"users/show.json"]){ [self.userinfodic release], self.userinfodic = nil; } } //请求成功回调方法 - (void)request:(sinaweiborequest *)request didfinishloadingwithresult:(id)result{ if ([request.url hassuffix:@"users/show.json"]){ [self.userinfodic release]; self.userinfodic = [result retain]; //nslog(@"用户信息字典:%@", self.userinfodic); 字典数据 返回字段下面 } }
返回字段说明
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。