iOS 微博 OAuth2.0 分享文字微博的方法
2012-08-10 17:40
316 查看
这里以新浪的为例子进行说明,后面会陆续添加;
这里同时提供两种方式,一种是通过ASIFormData的方式来完成
NSURL *url = [[NSURLalloc] initWithString:@"https://api.weibo.com/2/statuses/update.json"];
// statuses/upload
ASIFormDataRequest *request = [[ASIFormDataRequestalloc] initWithURL:url];
NSUserDefaults *info = [NSUserDefaultsstandardUserDefaults];
[request addPostValue:SINAAPPKEY forKey:@"source"];
[request addPostValue:Text forKey:@"status"];
[request addPostValue:@"0" forKey:@"lat"];
[request addPostValue:@"0" forKey:@"long"];
[request addPostValue:[selfDecrypt:MyPWDKey :[info objectForKey:@"sina_access_tokenV2"]] forKey:@"access_token"];
[request startSynchronous];
NSString *rst = [request responseString];
[request release];
[url release];
另一个种是通过iOS SDK 来实现
NSAutoreleasePool *pool = [[NSAutoreleasePoolalloc] init];
NSUserDefaults *info = [NSUserDefaultsstandardUserDefaults];
NSString *post=[NSStringstringWithFormat:@"source=%@&status=%@&lat=0&long=0&access_token=%@",SINAAPPKEY,sinaWeiBoText,[selfDecrypt:MyPWDKey :[info objectForKey:@"sina_access_tokenV2"]]];
NSData *postData=[post dataUsingEncoding:NSUTF8StringEncodingallowLossyConversion:YES];
NSString *postLength=[NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest *request=[[NSMutableURLRequestalloc] initWithURL:[NSURLURLWithString:SINASENDTEXT]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded"forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn)
{
NSURLResponse *response;
NSError *error;
NSData *resutlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *resultSting=[[NSStringalloc] initWithData:resutlData encoding:NSUTF8StringEncoding];
NSDictionary *dict = [[NSDictionary alloc] initWithDictionary:[resultSting JSONValue]];
NSLog(@"%@",dict);
if ([dict objectForKey:@"error_code"] != nil) {
int code = [[dict objectForKey:@"error_code"] intValue];
NSLog(@"code = %d",code);
if (code == 20019) {
[self showMessage:@"不能发布相同的微博!"];
}
else {
[self showMessage:@"发送微博失败!"];
}
}
else {
[self showMessage:@"分享新浪微博成功!"];
}
[resultSting release];
[dict release];
}
[conn release];
[request release];
[pool release];
第一种方式更为简洁
这里同时提供两种方式,一种是通过ASIFormData的方式来完成
NSURL *url = [[NSURLalloc] initWithString:@"https://api.weibo.com/2/statuses/update.json"];
// statuses/upload
ASIFormDataRequest *request = [[ASIFormDataRequestalloc] initWithURL:url];
NSUserDefaults *info = [NSUserDefaultsstandardUserDefaults];
[request addPostValue:SINAAPPKEY forKey:@"source"];
[request addPostValue:Text forKey:@"status"];
[request addPostValue:@"0" forKey:@"lat"];
[request addPostValue:@"0" forKey:@"long"];
[request addPostValue:[selfDecrypt:MyPWDKey :[info objectForKey:@"sina_access_tokenV2"]] forKey:@"access_token"];
[request startSynchronous];
NSString *rst = [request responseString];
[request release];
[url release];
另一个种是通过iOS SDK 来实现
NSAutoreleasePool *pool = [[NSAutoreleasePoolalloc] init];
NSUserDefaults *info = [NSUserDefaultsstandardUserDefaults];
NSString *post=[NSStringstringWithFormat:@"source=%@&status=%@&lat=0&long=0&access_token=%@",SINAAPPKEY,sinaWeiBoText,[selfDecrypt:MyPWDKey :[info objectForKey:@"sina_access_tokenV2"]]];
NSData *postData=[post dataUsingEncoding:NSUTF8StringEncodingallowLossyConversion:YES];
NSString *postLength=[NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest *request=[[NSMutableURLRequestalloc] initWithURL:[NSURLURLWithString:SINASENDTEXT]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded"forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn)
{
NSURLResponse *response;
NSError *error;
NSData *resutlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *resultSting=[[NSStringalloc] initWithData:resutlData encoding:NSUTF8StringEncoding];
NSDictionary *dict = [[NSDictionary alloc] initWithDictionary:[resultSting JSONValue]];
NSLog(@"%@",dict);
if ([dict objectForKey:@"error_code"] != nil) {
int code = [[dict objectForKey:@"error_code"] intValue];
NSLog(@"code = %d",code);
if (code == 20019) {
[self showMessage:@"不能发布相同的微博!"];
}
else {
[self showMessage:@"发送微博失败!"];
}
}
else {
[self showMessage:@"分享新浪微博成功!"];
}
[resultSting release];
[dict release];
}
[conn release];
[request release];
[pool release];
第一种方式更为简洁
相关文章推荐
- iOS 微博 OAuth2.0 分享文字微博的方法
- iOS 微博 OAuth2.0 分享 文字 + 图片 微博的方法
- iOS 微博 OAuth2.0 分享 文字 + 图片 微博的方法
- iOS 友盟微博分享文字+图片+链接实现
- iOS 微博 sdk 分享文字图片
- iOS之OC使用原生控件实现分享到QQ、微信、短信、微博等的两种方法
- js 调用原生 安卓 ios 分享、等 方法
- iOS 系统原生分享图片 文字 音乐 纯视频 网页
- 史上最全的iOS之UITextView实现placeHolder占位文字的N种方法
- 【IOS】最简单方法实现Cell文字高度自适应.
- iOS 分享成功后不走回调方法
- iOS集成QQ、微信、微博、短信、邮件分享(非第三方集成)
- ios开发学习--文字视图(Text)效果源码分享--系列教程
- iOS 网络请求框架AFNetworking使用方法(欢迎提建议和分享遇到的问题)
- iOS开发笔记14:微博/微信登录与分享、微信/支付宝支付
- iOS开发:分享一个获取设备型号的方法
- [置顶] 安卓调用系统自带分享功能分享文字,分享大图片,仿好奇心日报分享长图片(不用申请微信微博官方sdk就能直接分享)
- iOS 自定义分享功能(分享到微博等sahreSDK)
- iOS UIActivityViewController 实现微信微博等等的分享功能
- iOS画矩形、直线、文字的方法