Browse files

added tweet function example

  • Loading branch information...
1 parent dbcce3d commit cb16f10e54846c70d8dc94502703a76ca329443c @typester committed Nov 8, 2010
Showing with 256 additions and 12 deletions.
  1. +141 −10 resources/RootView.xib
  2. +1 −0 src/OAuth.h
  3. +47 −0 src/OAuth.m
  4. +5 −0 src/RootViewController.h
  5. +58 −0 src/RootViewController.m
  6. +4 −2 twitter-oauth-example.xcodeproj/project.pbxproj
View
151 resources/RootView.xib
@@ -1,17 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<archive type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="7.10">
<data>
- <int key="IBDocument.SystemTarget">1024</int>
+ <int key="IBDocument.SystemTarget">1056</int>
<string key="IBDocument.SystemVersion">10F569</string>
- <string key="IBDocument.InterfaceBuilderVersion">804</string>
+ <string key="IBDocument.InterfaceBuilderVersion">823</string>
<string key="IBDocument.AppKitVersion">1038.29</string>
<string key="IBDocument.HIToolboxVersion">461.00</string>
<object class="NSMutableDictionary" key="IBDocument.PluginVersions">
<string key="NS.key.0">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
- <string key="NS.object.0">123</string>
+ <string key="NS.object.0">132</string>
</object>
<object class="NSMutableArray" key="IBDocument.EditedObjectIDs">
<bool key="EncodedWithXMLCoder">YES</bool>
+ <integer value="1"/>
</object>
<object class="NSArray" key="IBDocument.PluginDependencies">
<bool key="EncodedWithXMLCoder">YES</bool>
@@ -37,7 +38,7 @@
<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
</object>
<object class="IBUIView" id="191373211">
- <nil key="NSNextResponder"/>
+ <reference key="NSNextResponder"/>
<int key="NSvFlags">274</int>
<object class="NSMutableArray" key="NSSubviews">
<bool key="EncodedWithXMLCoder">YES</bool>
@@ -113,6 +114,53 @@
</object>
<reference key="IBUINormalTitleShadowColor" ref="799240176"/>
</object>
+ <object class="IBUIButton" id="20793561">
+ <reference key="NSNextResponder" ref="191373211"/>
+ <int key="NSvFlags">292</int>
+ <string key="NSFrame">{{228, 122}, {72, 37}}</string>
+ <reference key="NSSuperview" ref="191373211"/>
+ <bool key="IBUIOpaque">NO</bool>
+ <string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
+ <int key="IBUIContentHorizontalAlignment">0</int>
+ <int key="IBUIContentVerticalAlignment">0</int>
+ <object class="NSFont" key="IBUIFont">
+ <string key="NSName">Helvetica-Bold</string>
+ <double key="NSSize">15</double>
+ <int key="NSfFlags">16</int>
+ </object>
+ <int key="IBUIButtonType">1</int>
+ <string key="IBUINormalTitle">tweet</string>
+ <reference key="IBUIHighlightedTitleColor" ref="914061868"/>
+ <object class="NSColor" key="IBUINormalTitleColor">
+ <int key="NSColorSpace">1</int>
+ <bytes key="NSRGB">MC4xOTYwNzg0MzQ2IDAuMzA5ODAzOTMyOSAwLjUyMTU2ODY1NgA</bytes>
+ </object>
+ <reference key="IBUINormalTitleShadowColor" ref="799240176"/>
+ </object>
+ <object class="IBUITextField" id="929623836">
+ <reference key="NSNextResponder" ref="191373211"/>
+ <int key="NSvFlags">292</int>
+ <string key="NSFrame">{{20, 125}, {200, 31}}</string>
+ <reference key="NSSuperview" ref="191373211"/>
+ <bool key="IBUIOpaque">NO</bool>
+ <bool key="IBUIClipsSubviews">YES</bool>
+ <string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
+ <int key="IBUIContentVerticalAlignment">0</int>
+ <string key="IBUIText"/>
+ <int key="IBUIBorderStyle">3</int>
+ <object class="NSColor" key="IBUITextColor">
+ <int key="NSColorSpace">3</int>
+ <bytes key="NSWhite">MAA</bytes>
+ <object class="NSColorSpace" key="NSCustomColorSpace" id="435572509">
+ <int key="NSID">2</int>
+ </object>
+ </object>
+ <bool key="IBUIAdjustsFontSizeToFit">YES</bool>
+ <float key="IBUIMinimumFontSize">17</float>
+ <object class="IBUITextInputTraits" key="IBUITextInputTraits">
+ <string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
+ </object>
+ </object>
<object class="IBUIView" id="1064435681">
<reference key="NSNextResponder" ref="191373211"/>
<int key="NSvFlags">-2147483356</int>
@@ -135,14 +183,13 @@
<object class="NSColor" key="IBUIBackgroundColor" id="82119440">
<int key="NSColorSpace">3</int>
<bytes key="NSWhite">MQA</bytes>
- <object class="NSColorSpace" key="NSCustomColorSpace">
- <int key="NSID">2</int>
- </object>
+ <reference key="NSCustomColorSpace" ref="435572509"/>
</object>
<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
</object>
</object>
<string key="NSFrameSize">{320, 416}</string>
+ <reference key="NSSuperview"/>
<reference key="IBUIBackgroundColor" ref="82119440"/>
<object class="IBUISimulatedStatusBarMetrics" key="IBUISimulatedStatusBarMetrics"/>
<object class="IBUISimulatedNavigationBarMetrics" key="IBUISimulatedTopBarMetrics">
@@ -212,6 +259,31 @@
</object>
<int key="connectionID">15</int>
</object>
+ <object class="IBConnectionRecord">
+ <object class="IBCocoaTouchOutletConnection" key="connection">
+ <string key="label">tweetButton</string>
+ <reference key="source" ref="372490531"/>
+ <reference key="destination" ref="20793561"/>
+ </object>
+ <int key="connectionID">18</int>
+ </object>
+ <object class="IBConnectionRecord">
+ <object class="IBCocoaTouchOutletConnection" key="connection">
+ <string key="label">tweetField</string>
+ <reference key="source" ref="372490531"/>
+ <reference key="destination" ref="929623836"/>
+ </object>
+ <int key="connectionID">19</int>
+ </object>
+ <object class="IBConnectionRecord">
+ <object class="IBCocoaTouchEventConnection" key="connection">
+ <string key="label">onPushTweetButton:</string>
+ <reference key="source" ref="20793561"/>
+ <reference key="destination" ref="372490531"/>
+ <int key="IBEventType">7</int>
+ </object>
+ <int key="connectionID">20</int>
+ </object>
</object>
<object class="IBMutableOrderedSet" key="objectRecords">
<object class="NSArray" key="orderedObjects">
@@ -231,6 +303,8 @@
<reference ref="834072399"/>
<reference ref="65253808"/>
<reference ref="1064435681"/>
+ <reference ref="929623836"/>
+ <reference ref="20793561"/>
</object>
<reference key="parent" ref="0"/>
</object>
@@ -274,6 +348,16 @@
<reference key="object" ref="615818260"/>
<reference key="parent" ref="1064435681"/>
</object>
+ <object class="IBObjectRecord">
+ <int key="objectID">16</int>
+ <reference key="object" ref="20793561"/>
+ <reference key="parent" ref="191373211"/>
+ </object>
+ <object class="IBObjectRecord">
+ <int key="objectID">17</int>
+ <reference key="object" ref="929623836"/>
+ <reference key="parent" ref="191373211"/>
+ </object>
</object>
</object>
<object class="NSMutableDictionary" key="flattenedProperties">
@@ -286,6 +370,10 @@
<string>1.IBPluginDependency</string>
<string>10.IBPluginDependency</string>
<string>11.IBPluginDependency</string>
+ <string>16.IBPluginDependency</string>
+ <string>16.IBViewBoundsToFrameTransform</string>
+ <string>17.IBPluginDependency</string>
+ <string>17.IBViewBoundsToFrameTransform</string>
<string>4.IBPluginDependency</string>
<string>4.IBViewBoundsToFrameTransform</string>
<string>5.IBPluginDependency</string>
@@ -303,6 +391,14 @@
<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<object class="NSAffineTransform">
+ <bytes key="NSTransformStruct">AUNkAABDiAAAA</bytes>
+ </object>
+ <string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
+ <object class="NSAffineTransform">
+ <bytes key="NSTransformStruct">AUGgAABDiYAAA</bytes>
+ </object>
+ <string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
+ <object class="NSAffineTransform">
<bytes key="NSTransformStruct">P4AAAL+AAABDCwAAw0MAAA</bytes>
</object>
<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
@@ -331,12 +427,26 @@
</object>
</object>
<nil key="sourceID"/>
- <int key="maxID">15</int>
+ <int key="maxID">20</int>
</object>
<object class="IBClassDescriber" key="IBDocument.Classes">
<object class="NSMutableArray" key="referencedPartialClassDescriptions">
<bool key="EncodedWithXMLCoder">YES</bool>
<object class="IBPartialClassDescription">
+ <string key="className">NSObject</string>
+ <object class="IBClassDescriptionSource" key="sourceIdentifier">
+ <string key="majorKey">IBProjectSource</string>
+ <string key="minorKey">src/JSON/NSObject+SBJSON.h</string>
+ </object>
+ </object>
+ <object class="IBPartialClassDescription">
+ <string key="className">NSObject</string>
+ <object class="IBClassDescriptionSource" key="sourceIdentifier">
+ <string key="majorKey">IBProjectSource</string>
+ <string key="minorKey">src/JSON/SBJsonWriter.h</string>
+ </object>
+ </object>
+ <object class="IBPartialClassDescription">
<string key="className">RootViewController</string>
<string key="superclassName">UIViewController</string>
<object class="NSMutableDictionary" key="actions">
@@ -345,11 +455,13 @@
<bool key="EncodedWithXMLCoder">YES</bool>
<string>onPushHomeTimelineButton:</string>
<string>onPushSigninButton:</string>
+ <string>onPushTweetButton:</string>
</object>
<object class="NSMutableArray" key="dict.values">
<bool key="EncodedWithXMLCoder">YES</bool>
<string>id</string>
<string>id</string>
+ <string>id</string>
</object>
</object>
<object class="NSMutableDictionary" key="actionInfosByName">
@@ -358,6 +470,7 @@
<bool key="EncodedWithXMLCoder">YES</bool>
<string>onPushHomeTimelineButton:</string>
<string>onPushSigninButton:</string>
+ <string>onPushTweetButton:</string>
</object>
<object class="NSMutableArray" key="dict.values">
<bool key="EncodedWithXMLCoder">YES</bool>
@@ -369,6 +482,10 @@
<string key="name">onPushSigninButton:</string>
<string key="candidateClassName">id</string>
</object>
+ <object class="IBActionInfo">
+ <string key="name">onPushTweetButton:</string>
+ <string key="candidateClassName">id</string>
+ </object>
</object>
</object>
<object class="NSMutableDictionary" key="outlets">
@@ -379,13 +496,17 @@
<string>loadingView</string>
<string>signinButton</string>
<string>statusLabel</string>
+ <string>tweetButton</string>
+ <string>tweetField</string>
</object>
<object class="NSMutableArray" key="dict.values">
<bool key="EncodedWithXMLCoder">YES</bool>
<string>UIButton</string>
<string>UIView</string>
<string>UIButton</string>
<string>UILabel</string>
+ <string>UIButton</string>
+ <string>UITextField</string>
</object>
</object>
<object class="NSMutableDictionary" key="toOneOutletInfosByName">
@@ -396,6 +517,8 @@
<string>loadingView</string>
<string>signinButton</string>
<string>statusLabel</string>
+ <string>tweetButton</string>
+ <string>tweetField</string>
</object>
<object class="NSMutableArray" key="dict.values">
<bool key="EncodedWithXMLCoder">YES</bool>
@@ -415,6 +538,14 @@
<string key="name">statusLabel</string>
<string key="candidateClassName">UILabel</string>
</object>
+ <object class="IBToOneOutletInfo">
+ <string key="name">tweetButton</string>
+ <string key="candidateClassName">UIButton</string>
+ </object>
+ <object class="IBToOneOutletInfo">
+ <string key="name">tweetField</string>
+ <string key="candidateClassName">UITextField</string>
+ </object>
</object>
</object>
<object class="IBClassDescriptionSource" key="sourceIdentifier">
@@ -428,7 +559,7 @@
<string key="IBDocument.TargetRuntimeIdentifier">IBCocoaTouchFramework</string>
<object class="NSMutableDictionary" key="IBDocument.PluginDeclaredDependencyDefaults">
<string key="NS.key.0">com.apple.InterfaceBuilder.CocoaTouchPlugin.iPhoneOS</string>
- <integer value="1024" key="NS.object.0"/>
+ <integer value="1056" key="NS.object.0"/>
</object>
<object class="NSMutableDictionary" key="IBDocument.PluginDeclaredDevelopmentDependencies">
<string key="NS.key.0">com.apple.InterfaceBuilder.CocoaTouchPlugin.InterfaceBuilder3</string>
@@ -437,6 +568,6 @@
<bool key="IBDocument.PluginDeclaredDependenciesTrackSystemTargetVersion">YES</bool>
<string key="IBDocument.LastKnownRelativeProjectPath">../twitter-oauth-example.xcodeproj</string>
<int key="IBDocument.defaultPropertyAccessControl">3</int>
- <string key="IBCocoaTouchPluginVersion">123</string>
+ <string key="IBCocoaTouchPluginVersion">132</string>
</data>
</archive>
View
1 src/OAuth.h
@@ -22,5 +22,6 @@
+(NSString*)getAuthHeaderForMethod:(NSString*)method URL:(NSString*)url params:(NSDictionary*)params;
+(BOOL)getHomeTimelineParams:(NSDictionary*)params onComplete:(void (^)(OAuth*))callback;
++(BOOL)tweetParams:(NSDictionary*)params onComplete:(void (^)(OAuth*))callback;
@end
View
47 src/OAuth.m
@@ -204,6 +204,53 @@ +(BOOL)getHomeTimelineParams:(NSDictionary*)params onComplete:(void (^)(OAuth*))
return YES;
}
++(BOOL)tweetParams:(NSDictionary*)params onComplete:(void (^)(OAuth*))callback {
+ NSString* url = @"http://api.twitter.com/1/statuses/update.json";
+
+ dispatch_queue_t mq = dispatch_get_main_queue();
+ dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
+
+ dispatch_async(q, ^{
+ OAuth* api = [OAuth oauth];
+ CURL* curl = curl_init(api);
+ CURLcode res = 0;
+
+ struct curl_slist* slist = NULL;
+ slist = curl_slist_append(slist, "Expect:");
+ slist = curl_slist_append(slist, "Content-Type: application/x-www-form-urlencoded");
+ slist = curl_slist_append(slist, [[OAuth getAuthHeaderForMethod:@"POST" URL:url params:params] UTF8String]);
+ curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
+
+ NSString* content = [NSString stringWithFormat:@"status=%@", [[params objectForKey:@"status"] URLEncodedString]];
+ LOG(@"content: %@", content);
+
+ curl_easy_setopt(curl, CURLOPT_URL, [url UTF8String]);
+ curl_easy_setopt(curl, CURLOPT_POST, 1);
+ curl_easy_setopt(curl, CURLOPT_POSTFIELDS, [content UTF8String]);
+ curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen([content UTF8String]));
+
+ res = curl_easy_perform(curl);
+
+ if (0 != res) {
+ api.error = [NSString stringWithUTF8String:curl_easy_strerror(res)];
+ }
+ else {
+ NSString* content = [[NSString alloc] initWithData:api.readBuf
+ encoding:NSUTF8StringEncoding];
+ api.content = content;
+ [content release];
+ }
+
+ curl_easy_cleanup(curl);
+
+ dispatch_async(mq, ^{
+ callback(api);
+ });
+ });
+
+ return YES;
+}
+
+(NSString*)getAuthHeaderForMethod:(NSString*)method URL:(NSString*)url params:(NSDictionary*)params {
NSNumber* epoch = [NSNumber numberWithInt:(int)[[NSDate date] timeIntervalSince1970]];
View
5 src/RootViewController.h
@@ -3,16 +3,21 @@
@interface RootViewController : UIViewController {
UIButton* signinButton_;
UIButton* homeTimelineButton_;
+ UITextField* tweetField_;
+ UIButton* tweetButton_;
UILabel* statusLabel_;
UIView* loadingView_;
}
@property (nonatomic, retain) IBOutlet UIButton* signinButton;
@property (nonatomic, retain) IBOutlet UIButton* homeTimelineButton;
+@property (nonatomic, retain) IBOutlet UITextField* tweetField;
+@property (nonatomic, retain) IBOutlet UIButton* tweetButton;
@property (nonatomic, retain) IBOutlet UILabel* statusLabel;
@property (nonatomic, retain) IBOutlet UIView* loadingView;
-(IBAction)onPushSigninButton:(id)sender;
-(IBAction)onPushHomeTimelineButton:(id)sender;
+-(IBAction)onPushTweetButton:(id)sender;
@end
View
58 src/RootViewController.m
@@ -11,6 +11,8 @@ @implementation RootViewController
@synthesize signinButton = signinButton_;
@synthesize homeTimelineButton = homeTimelineButton_;
+@synthesize tweetField = tweetField_;
+@synthesize tweetButton = tweetButton_;
@synthesize statusLabel = statusLabel_;
@synthesize loadingView = loadingView_;
@@ -30,11 +32,15 @@ -(void)viewWillAppear:(BOOL)animated {
if (nil == access_token) {
self.homeTimelineButton.hidden = YES;
+ self.tweetButton.hidden = YES;
+ self.tweetField.hidden = YES;
self.statusLabel.text = @"Status: not signed in";
}
else {
self.signinButton.hidden = YES;
self.homeTimelineButton.hidden = NO;
+ self.tweetButton.hidden = NO;
+ self.tweetField.hidden = NO;
self.statusLabel.text = [NSString stringWithFormat:@"Status: signed in as %@", [d stringForKey:@"screen_name"]];
}
@@ -49,6 +55,8 @@ -(void)dealloc {
-(void)releaseIBOutlets {
self.signinButton = nil;
self.homeTimelineButton = nil;
+ self.tweetField = nil;
+ self.tweetButton = nil;
self.statusLabel = nil;
self.loadingView = nil;
}
@@ -108,4 +116,54 @@ -(IBAction)onPushHomeTimelineButton:(id)sender {
[next release];
}
+-(IBAction)onPushTweetButton:(id)sender {
+ LOG_CURRENT_METHOD;
+
+ NSUserDefaults* d = [NSUserDefaults standardUserDefaults];
+ NSDictionary* access_token = [d dictionaryForKey:@"access_token"];
+
+ NSString* status = self.tweetField.text;
+ if (nil == status || [status isEqualToString:@""]) {
+ UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"please input tweet"
+ message:nil
+ delegate:nil
+ cancelButtonTitle:@"OK"
+ otherButtonTitles:nil];
+ [alert show];
+ [alert release];
+
+ return;
+ }
+
+
+ NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
+ access_token, @"token",
+ status, @"status",
+ nil];
+
+ [OAuth tweetParams:params onComplete:^(OAuth* res) {
+ if (nil != res.error) {
+ LOG(@"error: %@", res.error);
+ UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Error"
+ message:res.error
+ delegate:nil
+ cancelButtonTitle:@"OK"
+ otherButtonTitles:nil];
+ [alert show];
+ [alert release];
+ }
+ else {
+ LOG(@"res: %@", res.content);
+ UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Success!"
+ message:nil
+ delegate:nil
+ cancelButtonTitle:@"OK"
+ otherButtonTitles:nil];
+ [alert show];
+ [alert release];
+ }
+ }];
+
+}
+
@end
View
6 twitter-oauth-example.xcodeproj/project.pbxproj
@@ -279,13 +279,14 @@
"$HOME/dev/iphone/lib/openssl-1.0.0a/include",
);
INFOPLIST_FILE = Info.plist;
+ IPHONEOS_DEPLOYMENT_TARGET = 4.1;
LIBRARY_SEARCH_PATHS = (
"$(inherited)",
"$HOME/dev/iphone/lib/curl-7.21.1/lib",
"$HOME/dev/iphone/lib/openssl-1.0.0a/lib",
);
PRODUCT_NAME = "twitter-oauth-example";
- SDKROOT = iphoneos4.1;
+ SDKROOT = iphoneos;
};
name = Debug;
};
@@ -302,13 +303,14 @@
"$HOME/dev/iphone/lib/openssl-1.0.0a/include",
);
INFOPLIST_FILE = Info.plist;
+ IPHONEOS_DEPLOYMENT_TARGET = 4.1;
LIBRARY_SEARCH_PATHS = (
"$(inherited)",
"$HOME/dev/iphone/lib/curl-7.21.1/lib",
"$HOME/dev/iphone/lib/openssl-1.0.0a/lib",
);
PRODUCT_NAME = "twitter-oauth-example";
- SDKROOT = iphoneos4.1;
+ SDKROOT = iphoneos;
VALIDATE_PRODUCT = YES;
};
name = Release;

0 comments on commit cb16f10

Please sign in to comment.