Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Tree: b50c548b72
Fetching contributors…

Cannot retrieve contributors at this time

155 lines (104 sloc) 7.91 KB

YToolkit for Cocoa

What it includes

At the moment , the YToolkit includes a base64 lib (implemented in C, with NSData/NSString catetories), a handy cocoa categories lib, an oauth 1.0 & 2.0 lib (implemented in C function but using Cocoa object arguments, with NSMutableURLRequest & ASIHTTPRequest categories).

Besides, a demo for YToolkit is available from ytoolkitdemo (which is including demos for signing in & retrieving basic data from Twitter, Facebook, Douban, Sina Weibo, QQ Weibo, etc).

Why implementing again?

  • I have used some OAuth implementation for iOS, such as OAuthConsumer, OAuthCore, each of them has its pros and cons.

    About one year and a half ago I started to learn iPhone development, I wanted to make a client for Then I tried OAuthConsumer, after some hard work, I got OAuthConsumer working. However, OAuthConsumer handles network with NSURLConnection internally. If I want to use the ASIHTTPRequest, it just can't help. Besides, OAuthConsumer simply handles error according to OATicket object is nil or non-nil ( it assumes returned format is form-url-encoded), programmers cannot get the real reason why it can't work.

    Then I turned to use OAuthCore. It is simply a C function, which generates a OAuth 1.0 Authorization Header, one can integrate it with any HTTP handling libs. But OAuthCore only generate OAuth 1.0 spec Authorization without an optional realm value, which may be required by a OAuth provider.

    So, I decide to re-implement it: OAuth 1.0 and 2.0 (draft 22), following the spec as strictly as I could.

  • A base64 implementation is just for fun at the beginning. When I finished the code, I had a profiling among NSData+Base64 (along with OAuthCore), josefsson's base64 and libb64. The NSData+Base64 is the slowest, and my implementation is the fastest (On Macbook pro and an iPod 3G). Although on a Mac, my implementation is not much faster than josefsson's base64 and libb64, on an iPod, it is faster enough (both on a Mac and iPod, tested for a nearly 1.5MB png file). (You can check the benchmark result, or run the benchmark yourself in ytoolkitdemo)

What can I do with it?

  • The ytoolkit generates ybase64.a, ybase64additions.a, yoauth.a, yoauthadditions.a, and ytoolkit.framework (statically linked faked framework. Thanks to the ios-Universal-Framework project). You can either use the lib(s) , or the framework alone. Just add ytoolkit project as a dependencies of your project, and link the libs. All required headers will be generated under {BUILT_PRODUCTS_DIR}/usr/local/include, you should add this path into your header search path (See how ytoolkitdemo does). If you want to use the framework, just link against it, all required headers are just bundled in it. add

    -ObjC -all_load

    linker flags into your project

  • You can only use its base64 lib, the core is a pure C implementation, with NSString/NSData categories, which is handy under Cocoa foundation. Just as:

    NSString * base64encoded = [data base64String];


    NSData * base64decode = [base64encodedString base64ToData];
  • You can use some cocoa additions, such as:

    • Parsing a URL string into component: scheme, host, relative, query, fragment. (similar functionalities than NSURL supplies is not working sometimes.
    • Handling escaped/unscaped URLs
    • Generating a URL's query from a dictionary's key/value pair, or get the parameters from a URL's query string
    • Adding duplicated key/value paires: when a key is existed, the addition will add a NSCountedSet object to contains the values
  • You can use OAuth 1.0 and 2.0 (draft 22) lib or their Cocoa additions

    • If you are using NSMutableURLRequest of ASIHTTPRequest, just use the related additions
    • If you are using other request lib, just use yoauth lib to generate an oauth authorization header (OAuth 1.0) or an oauth parameters (OAuth 2.0), and set them as spec accordingly.
    • With NSMutableURLRequest:
   NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@""]];

   // "old version of (Douban is using) requires an 'OAuth realm=' format pattern
   // So, the realm should be specified (even @"")
   [request prepareOAuthv1RequestUsingConsumerKey:kDoubanConsumerKey
   [NSURLConnection connectionWithRequest:request delegate:self];
   NSURL * url = [NSURL URLWithString:@""];
   ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL:url];
   request.delegate = self;
   [request prepareOAuthv1RequestUsingConsumerKey:kDoubanConsumerKey
   [request startAsynchronous];
  • Just see the ytoolkitdemo for usage example of most popular sites


The core of ytoolkit is distributed under LGPL v3.0, you can freely link ytoolkit against any part of your program.

Except that, the ASIHTTPRequest additions are under BSD license, you can use it without any restrictions ( Because this ASIHTTPRequest may be compiled into your project)

Hope this is useful for you.



一个cocoa附加库(包括词典到URL query,URL query到词典,URL分解为scheme, host, relative path, query, fragment,包括一个可以放置重复key-value的DuplicatableDictionary),


一个OAuthv2库(结构同OAuthv1,但是由于OAuthv2比较简单,所以并没有什么代码)及OAuthv2 HTTP-MAC的扩展的实现(由于没有相应的服务器,所以没有测试)




豆瓣 (OAuthv1),

新浪微博(OAuth及OAuth 2都跑通了)




Jump to Line
Something went wrong with that request. Please try again.