Code Monkey home page Code Monkey logo

ytoolkit's Introduction

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 douban.com. 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 (which is now a GNU CoreUtils implementation) 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];

or

    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:@"http://www.douban.com/service/auth/request_token"]];
  
  // "old version of oauth.py (Douban is using) requires an 'OAuth realm=' format pattern
  // So, the realm should be specified (even @"")
  [request prepareOAuthv1RequestUsingConsumerKey:kDoubanConsumerKey
                               consumerSecretKey:kDoubanConsumerSecretKey
                                           token:nil
                                     tokenSecret:nil
                                           realm:kDoubanRealm
                                        verifier:nil
                                        callback:nil];
  [NSURLConnection connectionWithRequest:request delegate:self];
  NSURL * url = [NSURL URLWithString:@"http://api.douban.com/people/%40me/miniblog?alt=json"];
  ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL:url];
  request.delegate = self;
  [request prepareOAuthv1RequestUsingConsumerKey:kDoubanConsumerKey
                               consumerSecretKey:kDoubanConsumerSecretKey
                                           token:self.accesstoken
                                     tokenSecret:self.tokensecret
                                           realm:kDoubanRealm];
  [request startAsynchronous];
  • Just see the ytoolkitdemo for usage example of most popular sites

License

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.

这里面有什么?

一个快速的base64编码(纯C,及NSData/NSString的category),可以看项目页面的README和代码库里的benchmark,或者在ytoolkitdemo里跑profile

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

一个完善的OAuthv1库(C函数实现,但使用cocoa对象作参数)及相对应的NSMutableURLRequest和ASIHTTPRequest的扩展,

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

ytoolkitdemo里包含了下列服务的登录和获取数据的demo:

Twitter(OAuthv1),

Facebook(OAuthv2),

豆瓣 (OAuthv1),

新浪微博(OAuthv2跑通了,OAuthv1跑到最后一步,让用户输入PIN code,但是没有看到他们的文档里有相关的接口⋯⋯)

QQ微博(OAuthv1)

demo地址是 https://github.com/sprhawk/ytoolkitdemo

ytoolkit核心库是LGPLv3,ASIHTTPRequest的扩展未编译进核心库,使用BSD发布。ytoolkitdemo使用BSD发布。

ytoolkit's People

Contributors

sprhawk avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.