完整的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); 字典数据 返回字段下面
}
}
返回字段说明
0
投稿
猜你喜欢
- 介绍什么是ThreadLocal?ThreadLocal叫做线程变量,用于在多线程环境下创建线程本地变量。通俗的讲,ThreadLocal可
- 前言这里主要简单介绍如何使用Camera+SurfaceView自定义相机拍照,如果是Camera2或者是TextureView的可以前往主
- 协议做如下规定:规定数据协议:序列号 长度 状态字 数据长度 数据1 &n
- 项目概况:Spring Cloud搭的微服务,使用了eureka,FeignClient,现在遇到FeignClient调用接口时不支持上传
- 无论哪种界面框架输入文本框都是非常重要的控件, 但是发现flutter中的输入框TextField介绍的虽然多,但是各个属性怎么组合满足需要
- 前言上一篇我们认识了Kotlin编程语言,也搭建好开发环境。本篇就进入Kotlin的基础语法介绍,与其他编程语言一样,Kotlin也有自己的
- /// <summary> /// 遍历Co
- 正则表达式是一种描述词素的重要表示方法。虽然正则表达式并不能表达出所有可能的模式(例如“由等数量的 a 和 b 组成的字符串”),但是它可以
- 效果展示在实际项目当中我们经常看到如下各种剪裁形状的效果,Flutter 为我们提供了非常方便的 Widget 很轻松就可以实现,下面我们来
- [LeetCode] 159. Longest Substring with At Most Two Distinct Characters
- 简介对于一个APP来说,肯定会有一个AppBar,这个AppBar一般包含了APP的导航信息等。虽然我们可以用一个固定的组件来做为AppBa
- 示例 1 :使用搜索表单创建全屏模式我们要构建的小应用程序有一个应用程序栏,右侧有一个搜索按钮。按下此按钮时,将出现一个全屏模式对话框。它不
- 目录Android 集成Flutter1, Hello Flutter2, 引入 Flutter 模块3,使用Flutter3.1 添加依赖
- Android Studio 打包 jar 及 aar 包创建工程 New -> Module -> Library在gradl
- 模拟登陆的原理很简单,就是发送一个Http 请求服务器获得响应,然后客户端获取到cookie即可实现模拟登陆,比如一些抢票软件的原理无非也是
- Android ListView的Item点击效果的定制
- 本文实例为大家分享了Unity实现场景漫游相机的具体代码,供大家参考,具体内容如下前言拿到场景后总喜欢在场景里面玩一段时间,那这个脚本就是你
- 背景后台系统需要接入 企业微信登入,满足企业员工快速登入系统流程图简单代码说明自定义一套 springsecurity 认证逻辑主要就是 根
- mport java.text.DecimalFormat; DecimalFormat &nb
- .c 源程序 ----- 编译 ----- 链接 ---- exe ----运行 -------->程序翻译环境和执行环境翻译环境:源