Giter VIP home page Giter VIP logo

chris124 / iossocial Goto Github PK

View Code? Open in Web Editor NEW
126.0 16.0 41.0 1.2 MB

This is a library that will hopefully make it easier for people to integrate with different Social Networks in their applications. Please check out the list of issues/milestones and feel free to contribute. Message me with any issues you have using the library and I will create an issue ticket for it and put it into an upcoming milestone. Thanks! Distributed under Apache 2.0 license.

Home Page: http://about.me/mrchristopher

Objective-C 99.78% C 0.22%

iossocial's Introduction

iOSSocial

iOSSocial is a collection of classes that makes it easy to integrate with one or more popular social networks. Includes OAuth authentication support for each service out-of-the box via view controllers and saving of OAuth tokens in the keychain. The code supports authenticating multiple accounts for a given service and also the concept of setting a particular service as the primary service to be used for authentication.

Supported services:

  • OAuth 2: Instagram, Foursquare, Facebook
  • OAuth 1: Twitter, Flickr, Tumblr

Supported iOS platforms:

  • iOS 4
  • iOS 5

Community

Hopefully coming soon!

Adding iOSSocial to your project

Prerequisites: XCode 4.2 with iOS 5 and ARC support

iOSSocial is intended to be used as drop-in code for your project. To use it in your project, do the following (refer to the InstaBeta sample application in the iOSSocial workspace to see all of the files in place and all of the correct project settings):

Fork this repo and add your fork as a submodule to your git repository. Perform the following commands from a terminal in the base directory of your repository:

  • 'git submodule add [email protected]:[your username]/iOSSocial.git iOSSocial'
  • 'git submodule update --init'
  • 'cd iOSSocial'
  • 'git submodule update --init'

Drag the iOSSocial source files into your project.

  • add submodule for iOSSocial and drag iOSSocial folder to project
  • make group facebook-ios-sdk and drag src code to group
  • make asi-http-request group and drag src to group (main files + oath + reachability)
  • make gtm-oauth group and drag src to group
  • make gtm-http-fetcher group and drag src to group
  • make gtm-oauth2 group and drag src to group
  • drag prp folder to project

Link your application against Security.framework, SystemConfiguration.framework, CFNetwork.framework, MobileCoreServices.framework, Twitter, Accounts, libz.1.2.5.dylib

Add '-ObjC' linker flag

Add '-fno-objc-arc' compile flag. The compiler will spit out ARC errors for some files from third party libraries so you have to turn off ARC for these files.

  • In Xcode, select your project and go to Build Phases > Compile Sources
  • You will have to add it individually for each file. Hopefully Apple will eventually make it possible to add the flag to multiple files at once.

Special Considerations:

Facebook: iOSSocial makes use of the facebook-ios-sdk which uses Facebook's Single Sign On (SSO) feature. In order for the Facebook SDK to redirect back to your app after authorization, you will have to specify two URL Schemes in your application's main plist file (for instructions on adding these to your plist file go here https://github.com/facebook/facebook-ios-sdk OR look at the main plist file in the InstaBeta sample application in the iOSSocial workspace 'InstaBeta-Info.plist'.). In the example below, replace [appID] with the ID that Facebook assigned to the application that you created on their site and set [URLSuffix] to a useful name/identifier for your app. The URLSuffix allows the Facebook SDK to properly redirect back to the correct application even if more than one application on the device uses the same Facebook appID (for example, you have a trial and full version of the same app available within the app store):

  • fb[appID][URLSuffix]://authorize (refirect back from Safari)
  • fb[appID][URLSuffix] (redirect back from Facebook app via Single Sign On)

iOSSocialViewControllers

If you want to use the iOSSocialViewControllers, fork the repo and add a submodule for it and drag the source files into your project:

  • 'git submodule add [email protected]:[your username]/iOSSocialViewControllers.git iOSSocialViewControllers'
  • 'git submodule update --init'

An Overview of iOSSocial

iOSSServicesViewController


iOSSServicesViewController is a stand along class that can be used in conjunction with iOSSocial. It is shown here to give you an idea of how you can easily use iOSSocial to build a UI that presents a choice of services to a user to authenticate against. It lives in a separate repo here: https://github.com/chris124/iOSSocialViewControllers

Use iOSSServicesViewController to present a list of supported services to the user. The controller is evolving, but for now, it shows a list of available services to connect to (based on the services you choose to configure. Read below on iOSSocialServiceProtocol.):

When a service is chosen, the OAuth page for the service is shown:

If the user authenticates for the service, their user account is shown as connected under the accounts section:

Selecting an already connected account logs the user out of that account (ie, clears out their locally stored OAuth access token for their account on the given service). Selected a disconnected account will let the user log back in:

Here is the code in action:

iOSSServicesViewController *iossServicesViewController = [[iOSSServicesViewController alloc] initWithServicesFilter:nil];

[iossServicesViewController presentModallyFromViewController:self withServiceConnectedHandler:^(id localUser) { }
withCompletionHandler:^{ [self dismissModalViewControllerAnimated:YES]; } ];

If you want to filter which services are displayed in the view controller, pass in an array of service names.

NSArray *filter = [NSArray arrayWithObjects:@"Twitter", nil];

iOSSServicesViewController *iossServicesViewController = [[iOSSServicesViewController alloc] initWithServicesFilter:filter];

[iossServicesViewController presentModallyFromViewController:self withServiceConnectedHandler:^(id localUser) { }
withCompletionHandler:^{ [self dismissModalViewControllerAnimated:YES]; } ];

iOSSocialServiceProtocol


Which services you want to use is up to you. The underlying code will automatically pick-up which services are available based on what you configure. It is recommended that the services be configured in your application delegate before calling any other code. Here is an example of how the Instagram, Twitter, and Foursquare services are configured in an application delegate:

Note: For Instagram, when setting the object for the kSMOAuth2RedirectURI key in the OAuth params dictionary, the URL specified MUST match the callback URL you specified when you created your Instagram application on their site. It doesn't matter what the URL actually points to, but the value specified here HAS to match what you specified when you created your Instagram app. For example, http://www.mycompanyurl.com/instagram/callback for the Instagram oauth settings.

Note: Be passing YES to the asPrimary parameter, we are stating that Instagram is going to be the primary service for authentication.


NSMutableDictionary *params = [NSMutableDictionary dictionary];
[params setObject:@"instagram app client id" forKey:kSMOAuth2ClientID];
[params setObject:@"instagram app client secret" forKey:kSMOAuth2ClientSecret];
[params setObject:@"instagram app callback" forKey:kSMOAuth2RedirectURI];
[params setObject:@"unique name for keychain item" forKey:kSMOAuth2KeychainItemName];
[params setObject:@"https://api.instagram.com/oauth/authorize" forKey:kSMOAuth2AuthorizeURL];
[params setObject:@"https://api.instagram.com/oauth/access_token" forKey:kSMOAuth2AccessTokenURL];
[params setObject:@"Instagram Service" forKey:kSMOAuth2ServiceProviderName];
[params setObject:@"basic comments relationships likes" forKey:kSMOAuth2Scope];
[[Instagram sharedService] assignOAuthParams:params asPrimary:YES];

[params removeAllObjects];

[params setObject:@"twitter app client id" forKey:kSMOAuth1ClientID];
[params setObject:@"twitter app client secret" forKey:kSMOAuth1ClientSecret];
[params setObject:@"twitter app callback" forKey:kSMOAuth1RedirectURI];
[params setObject:@"unique name for keychain item" forKey:kSMOAuth1KeychainItemName];
[params setObject:@"https://api.twitter.com/oauth/request_token" forKey:kSMOAuth1RequestTokenURL];
[params setObject:@"https://api.twitter.com/oauth/access_token" forKey:kSMOAuth1AccessTokenURL];
[params setObject:@"https://api.twitter.com/oauth/authorize" forKey:kSMOAuth1AuthorizeURL];
[params setObject:@"Twitter Service" forKey:kSMOAuth1ServiceProviderName];
[[Twitter sharedService] assignOAuthParams:params asPrimary:NO];

[params removeAllObjects];

[params setObject:@"foursquare app client id" forKey:kSMOAuth2ClientID];
[params setObject:@"foursquare app client secret" forKey:kSMOAuth2ClientSecret];
[params setObject:@"foursquare app callback" forKey:kSMOAuth2RedirectURI];
[params setObject:@"unique name for keychain item" forKey:kSMOAuth2KeychainItemName];
[params setObject:@"https://foursquare.com/oauth2/authorize" forKey:kSMOAuth2AuthorizeURL];
[params setObject:@"https://foursquare.com/oauth2/access_token" forKey:kSMOAuth2AccessTokenURL];
[params setObject:@"Foursquare Service" forKey:kSMOAuth2ServiceProviderName];
[[Foursquare sharedService] assignOAuthParams:params asPrimary:NO];

iOSSocialLocalUserProtocol


Local user account objects are used to represent an authenticated user account for a given service. All of the local user account classes adhere to iOSSocialLocalUserProtocol. The available classes are LocalInstagramUser, LocalTwitterUser, LocalFoursquareUser, LocalFlickrUser, and LocalTumblrUser, (more to come as more services are supported). These derived classes can be created directly or if you just want to get an instance of a local user object for whichever service was set at the primary service you can call [[iOSSocialServicesStore sharedServiceStore] defaultAccount]. Once you have a local user account object, you can authenticate it, query its authentication state, log it out, get its OAuth access token and more. See the code below:


// The defaultAccount method returns an local user object for the service that was set as the primary service.
id localUser = [[iOSSocialServicesStore sharedServiceStore] defaultAccount];

BOOL isAuthenticated = [localUser isAuthenticated];

// The OAuth View Controller is shown modally from the current view controller so pass in 'self' for the 'fromViewController' param.

[localUser authenticateFromViewController:self 
                    withCompletionHandler:^(NSError *error){
                        if (error) {
                        } else {
						    NSString *accessToken = [localUser oAuthAccessToken];
                        }
                    }];

// Logging out is easy peasy. Note that this does NOT revoke the user's access to the actual service on their site. It just clears out 
// all of the local OAuth goodies.
[localUser logout];

iOSServicesDataSource


iOSServicesDataSource is the underlying data source (UITableViewDataSource) that you want to use for your UI (a UITableView) to present the list of available services to the user. For your convenience, there is an initializer that takes no params that will make all of the configured services available from the data source and there is a initWithServicesFilter: initializer that takes an array of iOSSocialServiceProtocol objects to let you specify which specific services you want to display at a given time.

iOSSServicesViewController is powered by iOSServicesDataSource. See iOSSServicesViewController's initWithServicesFilter: initializer to see how to filter which services are provided by the data source.

NSArray *filter = [NSArray arrayWithObjects: [[iOSSocialServicesStore sharedServiceStore] serviceWithType:@"Facebook"], [[iOSSocialServicesStore sharedServiceStore] serviceWithType:@"Twitter"], [[iOSSocialServicesStore sharedServiceStore] serviceWithType:@"Foursquare"], [[iOSSocialServicesStore sharedServiceStore] serviceWithType:@"Tumblr"], nil];

iOSServicesDataSource *dataSource = [[iOSServicesDataSource alloc] initWithServicesFilter:filter];

If you want to use your own UITableViewController, here is example tableView:didSelectRowAtIndexPath: code:


    switch (indexPath.section) {
            case 0:
        {
			//get back a local user object here.
            id localUser = [[iOSSocialServicesStore sharedServiceStore].accounts objectAtIndex:[indexPath row]];

			[[iOSSocialServicesStore sharedServiceStore] unregisterAccount:localUser];
		    [localUser logout];
			[self.tableView reloadData];
        }
            break;
        case 1:
        {
            //get back a service object here.
            
            id service = [[iOSSocialServicesStore sharedServiceStore].services objectAtIndex:[indexPath row]];

			id localUser = [service localUser];

			[localUser authenticateFromViewController:self withCompletionHandler:^(NSError *error) {
                if (!error) {
                    NSLog(@"authorized");
                    [[iOSSocialServicesStore sharedServiceStore] registerAccount:localUser];
                    [self.tableView reloadData];
                } else {
                    NSLog(@"authorization failed");
                }
            }];
        }
            break;
        default:
            break;
    }

Adding Support for a New Service

There are a lot of services to support and this is where you come in. :) To add support for a new service, fork the repo, create a branch and give it a good name (one that matches the service you want to integrate with would be perfect), create a sub folder and name it after the service and then create the three following files (You can copy and paste the content from one of the other services. Please bare with me for now. Lots of duplicate code. Need to do a major code cleanup.):

  1. ServiceName.m/.h: This class must implement iOSSocialServiceProtocol and be derived from iOSSocialServiceProtocol or iOSSocialServiceOAuth2Provider depending on if its an OAuth1 or OAuth2 service. See Instagram.m/.h.
  2. ServiceNameUser.m/.h: This class must implement iOSSocialUserProtocol. See InstagramUser.m/.h.
  3. LocalServiceNameUser.m/.h: This class must implement toiOSSocialLocalUserProtocol. See LocalInstagramUser.m/.h.

iossocial's People

Contributors

mrchristopher124 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

iossocial's Issues

Xcode project is broken

The xcode project for the develop branch is completely broken (the workspace file won't even load).

Using the older master branch, the workspace loads, but everything is a mess. the Instabeta example project has files from the iOSSocial static library added (yet the iOSSocial static library is not included), the project has both ARC and non-ARC code mixed together without any segregating of code, and in general the project is a mess.

Any plans to get this cleaned up into something that's usable? The project looks quite awesome, but in it's current state it's not easy to adopt.

RedirectURI - Please provide some documentation

Will be great if you can provide some documentation on what should be the RedirectURI for your example app in iOSSocial

This help us understand how to set it up for our own apps.

//kSMOAuth2RedirectURI

Resolve importing issue

2011-07-26 18:10:52.702 InstaBeta[61446:ef03] -[NSUserDefaults ioss_instagramUserDictionary]: unrecognized selector sent to instance 0x7085470

2011-07-26 18:10:52.727 InstaBeta[61446:ef03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSUserDefaults ioss_instagramUserDictionary]: unrecognized selector sent to instance 0x7085470'

*** First throw call stack:

(0x10de142 0x126cca7 0x10dfddd 0x1044f5f 0x1044d42 0x5bef 0x5adb 0x29c4 0x240181 0x1a1d1e 0x1a1f72 0x1a20cd 0x1a8f8d 0x23e1 0x179ade 0x17a98e 0x189624 0x18a08c 0x17dac4 0x1889876 0x10b2305 0x1016eb2 0x101579a 0x1014c44 0x1014b5b 0x17a38d 0x17bab6 0x20c2 0x2035)

terminate called throwing an exceptionkill

Update gtm-oauth2 to Play Nice with Some services

Not sure if you've experienced an issue where certain services don't actually play nicely with GTM-Oauth2 -- the ones I had issues with were Facebook and Github -- but a guy just said he patched it (haven't tested it myself yet though).

Thread of the issue can be seen here http://groups.google.com/group/gtm-oauth2/browse_thread/thread/2c0ae0bc9f3a8dfc

Link to patch is here http://code.google.com/p/gtm-oauth2/issues/detail?id=1

Not sure if that's helpful to you at all, just wanted to give you a heads up though in case it is!

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.