看到ios6支持新浪微博了..学习了一下core services layer/Accounts类库.
刚开始一看云里雾里的.完全不知道干什么.揣测下只知道和帐号相关的.google搜索一下.发现是苹果内嵌twitter使用twitter的.
类有
ACAccount 帐号
ACAccountCredential 帐号凭证?翻译不好.主要是OAuth的token和secret2个字段的验证
ACAccountStore 帐号存储列表
ACAccountType 帐号类型 目前只有twitter 揣测iOS6里面会添加Facebook和sina等支持
4个类
使用时候
通过 ACAccountStore 来获取帐号存储类.
然后accountsWithAccountType: 方法提交twitter类型.获取twitter类型的ACAccountType对象.
在通过ACAccountStore 提交twitter类型ACAccountType对象获取到twitter帐号列表.
然后遍历帐号..
tiwtter框架里面包含2个类
TWRequest twitter请求.使用方法在代码内有说明.
TWTweetCompostViewController twitter控制器.可以直接使用控制器完成提交
代码部分.注意导入2个框架Accounts和twitter
main.m
//
// main.m
// ControlDemo
//
// Created by watsy0007 on 12-6-3.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <Accounts/Accounts.h>
#import <Twitter/Twitter.h>
#define BARBUTTONITEM(bar,title,act) UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithTitle:title \
style:UIBarButtonItemStylePlain \
target:self \
action:act];
@interface ViewController : UIViewController {
}
@end
@implementation ViewController
- (void) dealloc {
[super dealloc];
}
- (void) displayText:(NSString *) sOutput {
NSLog(@"out : %@",sOutput);
}
- (void) sendTextToTwitter:(id) sender {
TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];
[tweetViewController setInitialText:@"hello,this is a tweet from iphone by watsy0007."];
// [tweetViewController addImage:[UIImage @"head.png"]];
[tweetViewController setCompletionHandler:^(TWTweetComposeViewControllerResult result) {
NSString *output;
switch (result) {
case TWTweetComposeViewControllerResultCancelled:
output = @"Tweet cancelled.";
break;
case TWTweetComposeViewControllerResultDone:
output = @"tweet done.";
break;
default:
break;
}
[self performSelectorOnMainThread:@selector(displayText:) withObject:output waitUntilDone:NO];
[self dismissModalViewControllerAnimated:YES];
}];
[self presentModalViewController:tweetViewController animated:YES];
[tweetViewController release];
}
- (void) sendCustomTwitter:(id) sender {
//获取帐号存储
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
//获取twitter帐号类型
//按照此数据提示.可以看出来苹果在更新后的SDK中,应该会封装facebook sina的帐号吧.
//这样封装了帐号密码在系统级别.而在应用程序中,只需要获取到帐号的索引
//然后调用发送和接收服务
//剩下的只是数据处理
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
//申请访问帐号
[accountStore requestAccessToAccountsWithType:accountType
withCompletionHandler:^(BOOL granted,NSError *error) {
//授权访问
//提示用户程序需要访问twitter帐号
if (granted) {
//获取twitter帐号列表
NSArray *accountArray = [accountStore accountsWithAccountType:accountType];
//如果添加了twitter帐号
if ([accountArray count] > 0) {
//这里只是获取了第一个帐号.其实还可以通过username选择具体的用户
ACAccount *twitterAccount = [accountArray objectAtIndex:0];
//twitter 访问请求
//封装的相当简洁
//用户只需要提交 url 数据字典 和 请求类型
//这样独立于帐号
TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"]
parameters:[NSDictionary dictionaryWithObject:@"Hello, This is a tweet from ios demo." forKey:@"status"]
requestMethod:TWRequestMethodPOST];
//设置请教的归属帐号
//使用什么帐号来完成操作
[postRequest setAccount:twitterAccount];
//请求数据
[postRequest performRequestWithHandler:^(NSData *responseData,NSHTTPURLResponse *urlResponse,NSError *error){
//请求返回的结果
NSString *output = [NSString stringWithFormat:@"HTTP response status : %i\n data: %@",[urlResponse statusCode],[[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease]];
[self performSelectorOnMainThread:@selector(displayText:) withObject:output waitUntilDone:NO];
}];
}
}
}];
}
- (void) getpublicLineTwitter:(id) sender {
//同上
TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"http://api.twitter.com/1/statuses/public_timeline.json"]
parameters:nil
requestMethod:TWRequestMethodGET];
[postRequest performRequestWithHandler:^(NSData *responseData,NSHTTPURLResponse *urlResponse,NSError *error){
NSString *output;
if ([urlResponse statusCode] == 200) {
// Parse the responseData, which we asked to be in JSON format for this request, into an NSDictionary using NSJSONSerialization.
NSError *jsonParsingError = nil;
NSDictionary *publicTimeline = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonParsingError];
output = [NSString stringWithFormat:@"HTTP response status: %i\nPublic timeline:\n%@", [urlResponse statusCode], publicTimeline];
}
else {
output = [NSString stringWithFormat:@"HTTP response status: %i\n", [urlResponse statusCode]];
}
[self performSelectorOnMainThread:@selector(displayText:) withObject:output waitUntilDone:NO];
}];
}
- (void) loadView {
[super loadView];
[[self view] setBackgroundColor:[UIColor whiteColor]];
BARBUTTONITEM(leftItem1, @"默认测试",@selector(sendTextToTwitter:));
BARBUTTONITEM(leftItem2, @"post测试",@selector(sendCustomTwitter:));
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:leftItem1,leftItem2, nil];
[leftItem1 release];
[leftItem2 release];
BARBUTTONITEM(rightItem, @"获取列表",@selector(getpublicLineTwitter:));
self.navigationItem.rightBarButtonItem = rightItem;
[rightItem release];
}
- (void) viewWillUnload {
[super viewWillUnload];
}
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return YES;
}
- (void) viewDidLoad {
[super viewDidLoad];
}
@end
//-----------------------------------------------------------------------------------------------------
#pragma mark -
#pragma mark AppDelegate
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIViewController *viewController;
@end
@implementation AppDelegate
@synthesize window = _window;
@synthesize viewController = _viewController;
- (void) dealloc {
[_window release];
[_viewController release];
[super dealloc];
}
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
srand(time(NULL));
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.viewController = [[ViewController alloc] init];
UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = controller;
[controller release];
[self.window makeKeyAndVisible];
return YES;
}
@end
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}