新浪微博 分享 功能的实现
1. 首先在http://open.t.sina.com.cn / 中申请成为开发者,再创建不同的应用,获得相应的 App Key (在下面链接中的source即为app key)
2. 登录认证:
NSString *authString = [ NSString stringWithFormat : @"%@:%@" , sinaIDField . text ,sinaPasswordField . text ];
NSData *authData = [authString dataUsingEncoding : NSUTF8StringEncoding ];
NSString *authValue = [ NSString stringWithFormat : @"Basic %@" ,[authData base64EncodingWithLineLength : 80 ]];
NSURL *url = [ NSURL URLWithString : @"http://api.t.sina.com.cn/account/verify_credentials.xml?source=3930264715 " ];
NSMutableURLRequest *request = [[ NSMutableURLRequest alloc ] initWithURL :url];
[request setHTTPMethod : @"GET" ];
[request setValue :authValue forHTTPHeaderField : @"Authorization" ];
NSURLResponse *response;
NSError *error;
[ NSURLConnection sendSynchronousRequest :request returningResponse :&response error :&error];
[request release ];
NSHTTPURLResponse *httpResponse = ( NSHTTPURLResponse *)response;
int statusCode = [httpResponse statusCode ];
NSLog ( @"status code = %d" ,statusCode);
if (statusCode != 200 ) {
alertTitle = @" 帐号或密码错误 " ;
alertMassage = @" 请您输入正确的帐号和密码! " ;
} else {
alertTitle = @"" ;
alertMassage = @" 登录成功! " ;
}
}
UIAlertView *alert = [[ UIAlertView alloc ] initWithTitle :alertTitle
message :alertMassage
delegate : nil cancelButtonTitle : @" 确定 "
otherButtonTitles : nil ];
[alert show ];
[alert release ];
3. 将内容(图片 文字)发送到新浪微博
NSString *authString = [ NSString stringWithFormat : @"%@:%@" ,[[ NSUserDefaults standardUserDefaults ] objectForKey : @"sinaID" ],[[ NSUserDefaults standardUserDefaults ] objectForKey : @"sinaPassword" ]];
NSData *authData = [authString dataUsingEncoding : NSUTF8StringEncoding ];
NSString *authValue = [ NSString stringWithFormat : @"Basic %@" ,[authData base64EncodingWithLineLength : 80 ]];
NSString *boundary = @"0xKhTmLbOuNdArYckkk" ;
NSString *filename = @" test .jpg" ;
NSData *imageData = UIImageJPEGRepresentation (shareImage, 1 );
NSString *bodyPrefixString = [ NSString stringWithFormat : @"--%@/r/n" , boundary];
NSString *bodySuffixString = [ NSString stringWithFormat : @"/r/n--%@--/r/n" , boundary];
NSString *contentDisposition = [ NSString stringWithFormat : @"Content-Disposition: form-data; name="pic"; filename="%@"/r/n" , filename];
NSString *contentImageType = [ NSString stringWithFormat : @"Content-Type: image/%@/r/n" , [filename pathExtension ]];
NSString *contentTransfer = @"Content-Transfer-Encoding: binary/r/n/r/n" ;
NSString *bodyUpdateField = [ NSString stringWithFormat : @"Content-Disposition: form-data;name="status"/r/n/r/n%@/r/n" ,[ NSString stringWithFormat : @"%@" , textView . text ]];
NSMutableData *postBody = [ NSMutableData data ];
[postBody appendData :[bodyPrefixString dataUsingEncoding : NSUTF8StringEncoding allowLossyConversion : NO ]];
[postBody appendData :[bodyUpdateField dataUsingEncoding : NSUTF8StringEncoding ]];
[postBody appendData :[bodyPrefixString dataUsingEncoding : NSUTF8StringEncoding allowLossyConversion : NO ]];
[postBody appendData :[contentDisposition dataUsingEncoding : NSUTF8StringEncoding ]];
[postBody appendData :[contentImageType dataUsingEncoding : NSUTF8StringEncoding ]];
[postBody appendData :[contentTransfer dataUsingEncoding : NSUTF8StringEncoding ]];
[postBody appendData :imageData];
[postBody appendData :[bodySuffixString dataUsingEncoding : NSUTF8StringEncoding allowLossyConversion : NO ]];
NSString *baseURL = [ NSString stringWithFormat :@"http://api.t.sina.com.cn/statuses/upload.xml?source=3930264715 " ];
NSURL *url = [ NSURL URLWithString :baseURL];
NSMutableURLRequest *mainRequest = [[ NSMutableURLRequest alloc ] initWithURL :url
cachePolicy : NSURLRequestUseProtocolCachePolicy
timeoutInterval : 30.0f ];
[mainRequest setHTTPMethod : @"POST" ];
[mainRequest setValue :authValue forHTTPHeaderField : @"Authorization" ];
NSString *contentType = [ NSString stringWithFormat : @"multipart/form-data; boundary=%@" , boundary, nil ];
[mainRequest setValue :contentType forHTTPHeaderField : @"Content-Type" ];
[mainRequest setHTTPBody :postBody];
NSURLResponse *shareResponse;
NSError *error;
NSData *responseData = [ NSURLConnection sendSynchronousRequest :mainRequest returningResponse:&shareResponse error :&error];
NSString *responseString = [[ NSString alloc ] initWithData :responseData encoding :NSUTF8StringEncoding ];
NSHTTPURLResponse *httpResponse = ( NSHTTPURLResponse *)shareResponse;
int statusCode = [httpResponse statusCode ];
NSLog ( @"status code = %d" ,statusCode);
BOOL succeed = NO ;
if (statusCode == 200 ) {
succeed = YES ;
}
[mainRequest release ];
NSLog ( @"response string : %@" ,responseString);
[responseString release ];
[ uploadWaiting stopAnimating ];
NSString *message = nil ;
if (succeed) {
message = @" 分享成功 " ;
} else {
message = @" 分享失败 " ;
}
UIAlertView *alert = [[ UIAlertView alloc ] initWithTitle : @"" message :message
delegate : self
cancelButtonTitle : @" 确定 " otherButtonTitles : nil ];
[alert show ];
[alert release ];
新浪微博 分享 功能的实现相关推荐
- js实现QQ、微信、新浪微博分享功能
使用js实现QQ.微信.新浪微博分享功能. 微信分享需要手机扫描二维码,需要对url进行编码.在https协议下,扫描二维码时,浏览器打不开可能是没有安全证书导致的. js代码: 1 var shar ...
- android 分享到微博客户端,Android APP集成新浪微博分享功能
本文为大家分享了新浪微博分享功能集成,供大家参考,具体内容如下 直接导入weibosdkcore.jar:适用于只需要授权.分享.网络请求框架功能的项目. 无论使用哪一种方式,都需要先将demo中li ...
- php实现新浪微博分享功能,自定义新浪微博分享按钮样式
新浪开放平台虽然有分享按钮的组件(http://open.weibo.com/sharebutton)并且提供了两种格式的应用方法:WBML和JS,但还是无法据自己的需求做到自定义样式. 为了解决该问 ...
- jq 移动端网页分享功能_js实现QQ、微信、新浪微博分享功能
var shareModel ={/** * 分享QQ好友 * @param {[type]} title [分享标题] * @param {[type]} url [分享url链接,默认当前页面链接 ...
- 网页分享功能 支持 微信二维码 qq空间 qq好友 新浪微博 百度贴吧 豆瓣 人人...
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Android进阶之使用第三方平台ShareSDK实现新浪微博的一键分享功能
http://www.it165.net/pro/html/201402/9510.html http://www.it165.net/pro/html/201402/9510.html http:/ ...
- 使用第三方平台 ShareSDK 实现新浪微博的一键分享功能
http://software.intel.com/zh-cn/blogs/2014/03/02/android-sharesdk/?utm_campaign=CSDN&utm_source= ...
- react native一键分享功能实现amp;原理和注意点(支持微信、qq、新浪微博等)
前言 目前使用一键分享比较主流的两个SDK:ShareSDK.友盟: 又因为友盟功能比较多且比较全,比如说友盟统计.友盟推送等,所以本文重点介绍的是友盟分享功能在rn上的应用以及要注意的点. reac ...
- Android分享功能,微博、QQ、QQ空间等社交平台分享之入门与进阶
前言 如何能迅速做一个分享功能,那主要就是根据第一个步骤来做就好了,因为友盟的sdk包已经集成很多的内容了,只要你有相应的app和平台账号就可以能马上分享.进阶的内容就是从第三部分开始,这些内容不难, ...
最新文章
- 本科发表6篇SCI论文,获多个荣誉,他刚入学就享受研究生待遇!
- NOIp #2010
- LeetCode 147. Insertion Sort List 链表插入排序 C++/Java
- servlet增删改查实例_SpringMVC4+MyBatis3+SQLServer 2014 整合(包括增删改查分页)
- Google Chrome Frame
- node --- 后端使用body-parse解析Post请求,前端使用axios发送Post请求
- QT 中textEdit 和 textBrowser 无法使用斜体及加粗等 解决办法
- Oracle:使用ASM自动存储管理, 严重推荐
- Oracle中一把梭获取对象DDL创建语句
- DataGrid小扩展
- 规范规约是最左规约吗_Java开发者必须要知道的MySQL规范
- 【Linux-shell】shell脚本基础语法练习
- MySQL 之 库操作
- 基于SATI的建筑智能化最新调查与进展
- java数据清洗_数据清洗例子
- 硬件知识:固态硬盘和机械硬盘区别
- PADS 去除走线折角处提示和过孔提示
- Android编程权威指南第三版 第32章
- 如何添加和删除字段?
- Linux中系统进程的详细管理