完整的iOS新浪微博分享功能开发
作者:young_for_your 发布时间:2023-06-24 14:14:08
标签:iOS,微博,分享
本文实例为大家分享了iOS新浪微博分享功能的具体代码,供大家参考,具体内容如下
做新浪分享 需先去http://open.weibo.com/apps注册开发者app 很简单!
第1步
第2步
设置你的应用的信息
找到自己的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); 字典数据 返回字段下面
}
}
返回字段说明


猜你喜欢
- 需求:学生输入姓名和语文、数学、英语,编程求出总分和平均分,并在屏幕上显示XX的总分和平均分using System;using Syste
- 本文实例为大家分享了C#实现航班预订的具体代码,供大家参考,具体内容如下连接数据库using System;using System.Col
- 本文实例为大家分享了java实现录音播放的具体代码,供大家参考,具体内容如下需求:1.实现可以从麦克风进行录音2.可以停止录音3.实现播放录
- 1.性能考虑,优先选择数组数组在项目开发当中使用的频率是越来越少,特别是在业务为主的开发当中,首先数组没有List,Set等集合提供的诸多方
- 前言用过Spring的人多多少少也都用过@Async注解,至于作用嘛,看注解名,大概能猜出来,就是在方法执行的时候进行异步执行。一、如何使用
- 这篇文章主要介绍了如何使用HttpClient发送java对象到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学
- 获取map的key和value的方法分为两种形式:map.keySet():先获取map的key,然后根据key获取对应的value;map
- 我object != null要避免很多NullPointerException。有什么替代方法:if (someobject != nul
- 环境: idea2020.1插件: LeetCode-editor 6.7一、IDEA安装LeetCode插件安装完成重启idea打开插件U
- 在spring boot中,简单几步,使用spring AOP实现一个 * :1、引入依赖:<dependency> &nbs
- 前几天,公司数据库出现了两条相同的数据,而且时间相同(毫秒也相同)。排查原因,发现是网络波动造成了重复提交。由于网络波动而重复提交的例子也比
- 记一次 Data Binding 在 library module 中遇到的大坑使用 Data Binding 也有半年多了,从最初的 se
- 在实际应用中,很可能我们希望自己的app在按下返回键的时候并不退出,而是像按home键一样仅仅返回桌面,而程序仍然在后台运行着。要怎么实现这
- 一、@RequestMapping@RequestMapping注解的源码:@Target({ElementType.TYPE, Eleme
- 一、String与Date(java.util.Date)互转 1.1 String -&g
- MyBatis的前身叫iBatis,本是apache的一个开源项目, 2010年这个项目由apache software foundatio
- 本文为大家分享了Android实现水波纹效果展示的具体代码,供大家参考,具体内容如下一、效果二、实现原理自定义view,使用Path和贝塞尔
- 一、背景说明由于以前在项目中一直使用sqlmap.xml进行mybatis语句的编写和实现,其xml实现动态更新和查询较为方便,而目前由于技
- 概述状态模式允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它的类。这个模式将状态封装成独立的类,并将动作委托到 代表当前状态的对
- LZ77压缩算法原理的理解数据压缩是一个减小数据存储空间的过程,目前被应用在软件工程的各个地方,了解其一些原理,方便我们更好的甄选压缩方案。