Permalink
Browse files

Added methods Login, logout, Post Message, Public Timeline and User T…

…imeline
  • Loading branch information...
1 parent f2b09ac commit d69ea52cffa27cd782172f0306b11f0cc2d59bf0 @lucascorrea committed Mar 1, 2012
@@ -132,7 +132,6 @@
F99446DE14FEF6370017A5D8 = {
isa = PBXGroup;
children = (
- F99447D514FEFD590017A5D8 /* libxml2.2.7.3.dylib */,
F99446F314FEF6370017A5D8 /* SCTwitter */,
F994471114FEFB840017A5D8 /* Other Sources */,
);
@@ -149,6 +148,7 @@
F99446EC14FEF6370017A5D8 /* Frameworks */ = {
isa = PBXGroup;
children = (
+ F99447D514FEFD590017A5D8 /* libxml2.2.7.3.dylib */,
F99446ED14FEF6370017A5D8 /* UIKit.framework */,
F99446EF14FEF6370017A5D8 /* Foundation.framework */,
F99446F114FEF6370017A5D8 /* CoreGraphics.framework */,
@@ -159,14 +159,10 @@
F99446F314FEF6370017A5D8 /* SCTwitter */ = {
isa = PBXGroup;
children = (
- F99446FC14FEF6370017A5D8 /* SCTwitterAppDelegate.h */,
- F99446FD14FEF6370017A5D8 /* SCTwitterAppDelegate.m */,
- F99446FF14FEF6370017A5D8 /* MainStoryboard.storyboard */,
- F994470214FEF6370017A5D8 /* SCTwitterViewController.h */,
- F994470314FEF6370017A5D8 /* SCTwitterViewController.m */,
- F99446F414FEF6370017A5D8 /* Supporting Files */,
F994470E14FEFB1B0017A5D8 /* SCTwitter.h */,
F994470F14FEFB1B0017A5D8 /* SCTwitter.m */,
+ F99448F115001E4E0017A5D8 /* Classes */,
+ F99446F414FEF6370017A5D8 /* Supporting Files */,
);
path = SCTwitter;
sourceTree = "<group>";
@@ -295,6 +291,18 @@
path = SAOAuthTwitterEngine;
sourceTree = "<group>";
};
+ F99448F115001E4E0017A5D8 /* Classes */ = {
+ isa = PBXGroup;
+ children = (
+ F99446FC14FEF6370017A5D8 /* SCTwitterAppDelegate.h */,
+ F99446FD14FEF6370017A5D8 /* SCTwitterAppDelegate.m */,
+ F99446FF14FEF6370017A5D8 /* MainStoryboard.storyboard */,
+ F994470214FEF6370017A5D8 /* SCTwitterViewController.h */,
+ F994470314FEF6370017A5D8 /* SCTwitterViewController.m */,
+ );
+ name = Classes;
+ sourceTree = "<group>";
+ };
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
@@ -35,8 +35,6 @@
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
- <string>UIInterfaceOrientationLandscapeLeft</string>
- <string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>
@@ -11,4 +11,5 @@
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
+ UIView *loadingView;
#endif
@@ -3,31 +3,52 @@
// SCTwitter
//
// Created by Lucas Correa on 29/02/12.
-// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
+// Copyright (c) 2012 Siriuscode Solutions. All rights reserved.
//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
#import <Foundation/Foundation.h>
#import "SA_OAuthTwitterController.h"
#import "SA_OAuthTwitterEngine.h"
-#warning Your application App ID/API Key Twitter
+#warning Replace these with your own app credentials
#define kConsumerKey @"D6vneoIuMP0pdBZJAV7gg"
#define kConsumerSecret @"wWc59eahiaES9ZCZ7wp28Rw4hcURG4fmIXvvwJiaR8"
+#define kTwitterData @"twitterData"
-typedef void(^SCTwitterCallback)(BOOL success, id result);
-
+typedef void(^SCTwitterCallback)(BOOL success);
+typedef void(^SCTwitterStatusCallback)(BOOL success, id result);
@interface SCTwitter : NSObject <SA_OAuthTwitterEngineDelegate, SA_OAuthTwitterControllerDelegate>{
SA_OAuthTwitterEngine *_engine;
UIViewController *_viewController;
-
}
@property (nonatomic, copy) SCTwitterCallback loginCallback;
+@property (nonatomic, copy) SCTwitterStatusCallback statusCallback;
+ (SCTwitter *)shared;
-+ (void)loginViewControler:(UIViewController *)aViewController callBack:(void (^)(BOOL success, id result))aCallBack;
-//+ (void)logoutCallBack:(void(^)(BOOL success, id result))callBack;
++ (void)loginViewControler:(UIViewController *)aViewController callback:(void (^)(BOOL success))aCallback;
++ (void)logoutCallback:(void(^)(BOOL success))aCallback;
++ (void)postWithMessage:(NSString *)message callback:(void (^)(BOOL success, id result))aCallback;
++ (void)getPublicTimelineWithCallback:(void (^)(BOOL success, id result))aCallback;
++ (void)getUserTimelineFor:(NSString *)username sinceID:(unsigned long)sinceID startingAtPage:(int)page count:(int)count callback:(void (^)(BOOL success, id result))aCallback;
@end
@@ -3,21 +3,50 @@
// SCTwitter
//
// Created by Lucas Correa on 29/02/12.
-// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
+// Copyright (c) 2012 Siriuscode Solutions. All rights reserved.
//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
#import "SCTwitter.h"
@interface SCTwitter()
-- (void)loginViewControler:(UIViewController *)aViewController callBack:(void (^)(BOOL success, id result))aCallBack;
+- (void)loginViewControler:(UIViewController *)aViewController callback:(void (^)(BOOL success))aCallback;
+- (void)logoutCallback:(void (^)(BOOL success))aCallback;
+- (void)postWithMessage:(NSString *)message callback:(void (^)(BOOL success, id result))aCallback;
+- (void)getPublicTimelineWithCallback:(void (^)(BOOL success, id result))aCallback;
+- (void)getUserTimelineFor:(NSString *)username sinceID:(unsigned long)sinceID startingAtPage:(int)page count:(int)count callback:(void (^)(BOOL success, id result))aCallback;
- (BOOL)isSessionValid;
@end
+
+
@implementation SCTwitter
@synthesize loginCallback;
+@synthesize statusCallback;
+
+
+#pragma mark -
+#pragma mark Singleton
static SCTwitter *_scTwitter = nil;
@@ -44,79 +73,190 @@ - (SCTwitter *) init
_engine = [[SA_OAuthTwitterEngine alloc] initOAuthWithDelegate:self];
_engine.consumerKey = kConsumerKey;
_engine.consumerSecret = kConsumerSecret;
-
+
}
+
return self;
}
-+ (void)loginViewControler:(UIViewController *)aViewController callBack:(void (^)(BOOL, id))aCallBack
+
+#pragma mark -
+#pragma mark Public Methods Class
+
++ (void)loginViewControler:(UIViewController *)aViewController callback:(void (^)(BOOL success))aCallback
+{
+ [[SCTwitter shared] loginViewControler:aViewController callback:aCallback];
+}
+
++ (void)logoutCallback:(void (^)(BOOL success))aCallback
+{
+ [[SCTwitter shared] logoutCallback:aCallback];
+}
+
++ (void)postWithMessage:(NSString *)message callback:(void (^)(BOOL success, id result))aCallback
+{
+ [[SCTwitter shared] postWithMessage:message callback:aCallback];
+}
+
++ (void)getPublicTimelineWithCallback:(void (^)(BOOL success, id result))aCallback
+{
+ [[SCTwitter shared] getPublicTimelineWithCallback:aCallback];
+}
+
++ (void)getUserTimelineFor:(NSString *)username sinceID:(unsigned long)sinceID startingAtPage:(int)page count:(int)count callback:(void (^)(BOOL success, id result))aCallback
{
- [[SCTwitter shared] loginViewControler:aViewController callBack:aCallBack];
+ [[SCTwitter shared] getUserTimelineFor:username sinceID:sinceID startingAtPage:page count:count callback:aCallback];
}
-- (void)loginViewControler:(UIViewController *)aViewController callBack:(void (^)(BOOL, id))aCallBack
+
+
+#pragma mark -
+#pragma mark Private Methods
+
+- (BOOL)isSessionValid
+{
+ return _engine && [_engine isAuthorized];
+}
+
+- (void)loginViewControler:(UIViewController *)aViewController callback:(void (^)(BOOL success))aCallback
{
if ([self isSessionValid]) {
// Call the login callback if we have one
- if (aCallBack) {
- aCallBack(YES, nil);
+ if (aCallback) {
+ aCallback(YES);
}
} else {
// Autorize twitter
-
- self.loginCallback = aCallBack;
+ self.loginCallback = aCallback;
UIViewController *twitterController = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine:_engine delegate:self];
[aViewController presentModalViewController:twitterController animated: YES];
}
}
-- (BOOL)isSessionValid
+- (void)logoutCallback:(void (^)(BOOL success))aCallback
{
- return _engine && [_engine isAuthorized];
+ [_engine clearAccessToken];
+
+ // Remove the stored twitter credentials
+ NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
+
+ [defaults removeObjectForKey:kTwitterData];
+
+ [defaults synchronize];
+
+ // Call the logout callback
+ if (aCallback) {
+ aCallback(YES);
+ }
+}
+
+- (void)postWithMessage:(NSString *)message callback:(void (^)(BOOL success, id result))aCallback
+{
+ if (![self isSessionValid]) {
+
+ // Call the login callback if we have one
+ if (aCallback) {
+ aCallback(NO, nil);
+ }
+
+ }else{
+ self.statusCallback = aCallback;
+ [_engine sendUpdate:[NSString stringWithFormat:@"%@", message]];
+ }
+}
+
+- (void)getPublicTimelineWithCallback:(void (^)(BOOL success, id result))aCallback
+{
+ if (![self isSessionValid]) {
+
+ // Call the login callback if we have one
+ if (aCallback) {
+ aCallback(NO, @"Error");
+ }
+
+ }else{
+ self.statusCallback = aCallback;
+ [_engine getPublicTimeline];
+ }
}
+- (void)getUserTimelineFor:(NSString *)username sinceID:(unsigned long)sinceID startingAtPage:(int)page count:(int)count callback:(void (^)(BOOL, id))aCallback
+{
+ if (![self isSessionValid]) {
+
+ // Call the login callback if we have one
+ if (aCallback) {
+ aCallback(NO, @"Error");
+ }
+
+ }else{
+ self.statusCallback = aCallback;
+ [_engine getUserTimelineFor:username sinceID:sinceID startingAtPage:page count:count];
+ }
+}
-- (void) OAuthTwitterController: (SA_OAuthTwitterController *) controller authenticatedWithUsername: (NSString *) username {
-
+
+
+#pragma mark -
+#pragma mark - SA_OAuthTwitterControllerDelegate methods
+
+- (void) OAuthTwitterController: (SA_OAuthTwitterController *) controller authenticatedWithUsername: (NSString *) username
+{
// Call the login callback
if (self.loginCallback) {
- self.loginCallback(YES, nil);
+ self.loginCallback(YES);
}
-
}
-- (void) OAuthTwitterControllerFailed: (SA_OAuthTwitterController *) controller {
-
-// if (messageSuccessCallback) {
-// messageSuccessCallback();
-// }
-
-}
-- (void) storeCachedTwitterOAuthData: (NSString *) data forUsername: (NSString *) username {
+#pragma mark -
+#pragma mark - SA_OAuthTwitterEngineDelegate methods
+
+- (void) storeCachedTwitterOAuthData: (NSString *) data forUsername: (NSString *) username
+{
// Store the twitter credentials
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
- [defaults setObject:data forKey:@"twitter_oauth_data"];
+ [defaults setObject:data forKey:kTwitterData];
[defaults synchronize];
}
-- (NSString *) cachedTwitterOAuthDataForUsername: (NSString *) username {
+- (NSString *) cachedTwitterOAuthDataForUsername: (NSString *) username
+{
// Return the last stored auth data (we don't care about username)
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
- return [defaults objectForKey:@"twitter_oauth_data"];
- return nil;
+ return [defaults objectForKey:kTwitterData];
}
-- (void) twitterOAuthConnectionFailedWithData: (NSData *) data {
+- (void) twitterOAuthConnectionFailedWithData: (NSData *) data
+{
NSLog(@"Twitter connection failed");
}
+
+
+#pragma mark -
+#pragma mark - MGTwitterEngineDelegate methods
+
+- (void)requestFailed:(NSString *)connectionIdentifier withError:(NSError *)error
+{
+ if (self.statusCallback) {
+ self.statusCallback(NO, error);
+ }
+}
+
+- (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)connectionIdentifier
+{
+ if (self.statusCallback) {
+ self.statusCallback(YES, statuses);
+ }
+}
+
@end
Oops, something went wrong.

0 comments on commit d69ea52

Please sign in to comment.