FHSTwitterEngine.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. //
  2. // FHSTwitterEngine.h
  3. // FHSTwitterEngine
  4. //
  5. // Created by Nathaniel Symer on 8/22/12.
  6. // Copyright (C) 2012 Nathaniel Symer.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. //
  26. //
  27. // //// FHSTwitterEngine Version 1.6.1 ////
  28. // Modified OAuthConsumer Version 1.2.2
  29. //
  30. //
  31. //
  32. // FHSTwitterEngine
  33. // The synchronous Twitter engine that doesn’t suck!!
  34. //
  35. // FHSTwitterEngine is Synchronous
  36. // That means you will have to thread. Boo Hoo.
  37. // Setup
  38. // Just add the FHSTwitterEngine folder to you project.
  39. // USAGE
  40. // See README.markdown
  41. //
  42. // NOTE TO CONTRIBUTORS
  43. // Use NSJSONSerialization with removeNull(). Life is easy that way.
  44. //
  45. #import <Foundation/Foundation.h>
  46. // These are for the dispatch_async() calls that you use to get around the synchronous-ness
  47. #define GCDBackgroundThread dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
  48. #define GCDMainThread dispatch_get_main_queue()
  49. // oEmbed align modes
  50. typedef enum {
  51. FHSTwitterEngineAlignModeLeft,
  52. FHSTwitterEngineAlignModeRight,
  53. FHSTwitterEngineAlignModeCenter,
  54. FHSTwitterEngineAlignModeNone
  55. } FHSTwitterEngineAlignMode;
  56. // Image sizes
  57. typedef enum {
  58. FHSTwitterEngineImageSizeMini, // 24px by 24px
  59. FHSTwitterEngineImageSizeNormal, // 48x48
  60. FHSTwitterEngineImageSizeBigger, // 73x73
  61. FHSTwitterEngineImageSizeOriginal // original size of image
  62. } FHSTwitterEngineImageSize;
  63. typedef enum {
  64. FHSTwitterEngineResultTypeMixed,
  65. FHSTwitterEngineResultTypeRecent,
  66. FHSTwitterEngineResultTypePopular
  67. } FHSTwitterEngineResultType;
  68. // Remove NSNulls from NSDictionary and NSArray
  69. // Credit for this function goes to Conrad Kramer
  70. id removeNull(id rootObject);
  71. extern NSString * const FHSProfileBackgroundColorKey;
  72. extern NSString * const FHSProfileLinkColorKey;
  73. extern NSString * const FHSProfileSidebarBorderColorKey;
  74. extern NSString * const FHSProfileSidebarFillColorKey;
  75. extern NSString * const FHSProfileTextColorKey;
  76. extern NSString * const FHSProfileNameKey;
  77. extern NSString * const FHSProfileURLKey;
  78. extern NSString * const FHSProfileLocationKey;
  79. extern NSString * const FHSProfileDescriptionKey;
  80. @protocol FHSTwitterEngineAccessTokenDelegate <NSObject>
  81. - (void)storeAccessToken:(NSString *)accessToken;
  82. - (NSString *)loadAccessToken;
  83. @end
  84. @class OAToken;
  85. @class OAConsumer;
  86. @class OAMutableURLRequest;
  87. @interface FHSTwitterEngine : NSObject <UIWebViewDelegate>
  88. //
  89. // REST API
  90. //
  91. // statuses/update
  92. - (NSError *)postTweet:(NSString *)tweetString;
  93. - (NSError *)postTweet:(NSString *)tweetString inReplyTo:(NSString *)inReplyToString;
  94. // statuses/home_timeline
  95. - (id)getHomeTimelineSinceID:(NSString *)sinceID count:(int)count;
  96. // help/test
  97. - (id)testService;
  98. // blocks/create
  99. - (NSError *)block:(NSString *)username;
  100. // blocks/destroy
  101. - (NSError *)unblock:(NSString *)username;
  102. // users/lookup
  103. - (id)lookupUsers:(NSArray *)users areIDs:(BOOL)areIDs;
  104. // users/search
  105. - (id)searchUsersWithQuery:(NSString *)q andCount:(int)count;
  106. // account/update_profile_image
  107. - (NSError *)setProfileImageWithImageAtPath:(NSString *)file;
  108. - (NSError *)setProfileImageWithImageData:(NSData *)data;
  109. // account/settings GET and POST
  110. // See FHSTwitterEngine.m For details
  111. - (id)getUserSettings;
  112. - (NSError *)updateSettingsWithDictionary:(NSDictionary *)settings;
  113. // account/update_profile
  114. // See FHSTwitterEngine.m for details
  115. - (NSError *)updateUserProfileWithDictionary:(NSDictionary *)settings;
  116. // account/update_profile_background_image
  117. - (NSError *)setProfileBackgroundImageWithImageData:(NSData *)data tiled:(BOOL)isTiled;
  118. - (NSError *)setProfileBackgroundImageWithImageAtPath:(NSString *)file tiled:(BOOL)isTiled;
  119. - (NSError *)setUseProfileBackgroundImage:(BOOL)shouldUseProfileBackgroundImage;
  120. // account/update_profile_colors
  121. // See FHSTwitterEngine.m for details
  122. // If the dictionary is nil, FHSTwitterEngine resets the values
  123. - (NSError *)updateProfileColorsWithDictionary:(NSDictionary *)dictionary;
  124. // account/rate_limit_status
  125. - (id)getRateLimitStatus;
  126. // favorites/create, favorites/destroy
  127. - (NSError *)markTweet:(NSString *)tweetID asFavorite:(BOOL)flag;
  128. // favorites/list
  129. - (id)getFavoritesForUser:(NSString *)user isID:(BOOL)isID andCount:(int)count;
  130. - (id)getFavoritesForUser:(NSString *)user isID:(BOOL)isID andCount:(int)count sinceID:(NSString *)sinceID maxID:(NSString *)maxID;
  131. // account/verify_credentials
  132. - (id)verifyCredentials;
  133. // friendships/create
  134. - (NSError *)followUser:(NSString *)user isID:(BOOL)isID;
  135. // friendships/destroy
  136. - (NSError *)unfollowUser:(NSString *)user isID:(BOOL)isID;
  137. // friendships/lookup
  138. - (id)lookupFriendshipStatusForUsers:(NSArray *)users areIDs:(BOOL)areIDs;
  139. // friendships/incoming
  140. - (id)getPendingIncomingFollowers;
  141. // friendships/outgoing
  142. - (id)getPendingOutgoingFollowers;
  143. // friendships/update
  144. - (NSError *)enableRetweets:(BOOL)enableRTs andDeviceNotifs:(BOOL)devNotifs forUser:(NSString *)user isID:(BOOL)isID;
  145. // friendships/no_retweet_ids
  146. - (id)getNoRetweetIDs;
  147. // help/tos
  148. - (id)getTermsOfService;
  149. // help/privacy
  150. - (id)getPrivacyPolicy;
  151. // direct_messages
  152. - (id)getDirectMessages:(int)count;
  153. // direct_messages/destroy
  154. - (NSError *)deleteDirectMessage:(NSString *)messageID;
  155. // direct_messages/sent
  156. - (id)getSentDirectMessages:(int)count;
  157. // direct_messages/new
  158. - (NSError *)sendDirectMessage:(NSString *)body toUser:(NSString *)user isID:(BOOL)isID;
  159. // direct_messages/show
  160. - (id)showDirectMessage:(NSString *)messageID;
  161. // users/report_spam
  162. - (NSError *)reportUserAsSpam:(NSString *)user isID:(BOOL)isID;
  163. // help/configuration
  164. - (id)getConfiguration;
  165. // help/languages
  166. - (id)getLanguages;
  167. // blocks/blocking/ids
  168. - (id)listBlockedIDs;
  169. // blocks/blocking
  170. - (id)listBlockedUsers;
  171. // blocks/exists
  172. - (id)authenticatedUserIsBlocking:(NSString *)user isID:(BOOL)isID;
  173. // users/profile_image
  174. - (id)getProfileImageForUsername:(NSString *)username andSize:(FHSTwitterEngineImageSize)size;
  175. // statuses/user_timeline
  176. - (id)getTimelineForUser:(NSString *)user isID:(BOOL)isID count:(int)count;
  177. - (id)getTimelineForUser:(NSString *)user isID:(BOOL)isID count:(int)count sinceID:(NSString *)sinceID maxID:(NSString *)maxID;
  178. // statuses/retweet
  179. - (NSError *)retweet:(NSString *)identifier;
  180. // statuses/oembed
  181. - (id)oembedTweet:(NSString *)identifier maxWidth:(float)maxWidth alignmentMode:(FHSTwitterEngineAlignMode)alignmentMode;
  182. // statuses/show
  183. - (id)getDetailsForTweet:(NSString *)identifier;
  184. // statuses/destroy
  185. - (NSError *)destroyTweet:(NSString *)identifier;
  186. // statuses/update_with_media
  187. - (NSError *)postTweet:(NSString *)tweetString withImageData:(NSData *)theData;
  188. - (NSError *)postTweet:(NSString *)tweetString withImageData:(NSData *)theData inReplyTo:(NSString *)irt;
  189. // statuses/mentions_timeline
  190. - (id)getMentionsTimelineWithCount:(int)count;
  191. - (id)getMentionsTimelineWithCount:(int)count sinceID:(NSString *)sinceID maxID:(NSString *)maxID;
  192. // statuses/retweets_of_me
  193. - (id)getRetweetedTimelineWithCount:(int)count;
  194. - (id)getRetweetedTimelineWithCount:(int)count sinceID:(NSString *)sinceID maxID:(NSString *)maxID;
  195. // statuses/retweets
  196. - (id)getRetweetsForTweet:(NSString *)identifier count:(int)count;
  197. // lists/list
  198. - (id)getListsForUser:(NSString *)user isID:(BOOL)isID;
  199. // lists/statuses
  200. - (id)getTimelineForListWithID:(NSString *)listID count:(int)count;
  201. - (id)getTimelineForListWithID:(NSString *)listID count:(int)count sinceID:(NSString *)sinceID maxID:(NSString *)maxID;
  202. - (id)getTimelineForListWithID:(NSString *)listID count:(int)count excludeRetweets:(BOOL)excludeRetweets excludeReplies:(BOOL)excludeReplies;
  203. - (id)getTimelineForListWithID:(NSString *)listID count:(int)count sinceID:(NSString *)sinceID maxID:(NSString *)maxID excludeRetweets:(BOOL)excludeRetweets excludeReplies:(BOOL)excludeReplies;
  204. // lists/members/create_all
  205. - (NSError *)addUsersToListWithID:(NSString *)listID users:(NSArray *)users;
  206. // lists/members/destroy_all
  207. - (NSError *)removeUsersFromListWithID:(NSString *)listID users:(NSArray *)users;
  208. // lists/members
  209. - (id)listUsersInListWithID:(NSString *)listID;
  210. // lists/update
  211. - (NSError *)setModeOfListWithID:(NSString *)listID toPrivate:(BOOL)isPrivate;
  212. - (NSError *)changeNameOfListWithID:(NSString *)listID toName:(NSString *)newName;
  213. - (NSError *)changeDescriptionOfListWithID:(NSString *)listID toDescription:(NSString *)newName;
  214. // lists/show
  215. - (id)getListWithID:(NSString *)listID;
  216. // lists/create
  217. - (NSError *)createListWithName:(NSString *)name isPrivate:(BOOL)isPrivate description:(NSString *)description;
  218. // tweets/search
  219. - (id)searchTweetsWithQuery:(NSString *)q count:(int)count resultType:(FHSTwitterEngineResultType)resultType unil:(NSDate *)untilDate sinceID:(NSString *)sinceID maxID:(NSString *)maxID;
  220. // followers/ids
  221. - (id)getFollowersIDs;
  222. // followers/list
  223. - (id)listFollowersForUser:(NSString *)user isID:(BOOL)isID withCursor:(NSString *)cursor;
  224. // friends/ids
  225. - (id)getFriendsIDs;
  226. // friends/list
  227. - (id)listFriendsForUser:(NSString *)user isID:(BOOL)isID withCursor:(NSString *)cursor;
  228. //
  229. // Login and Auth
  230. //
  231. // XAuth login
  232. - (NSError *)getXAuthAccessTokenForUsername:(NSString *)username password:(NSString *)password;
  233. // OAuth login
  234. - (void)showOAuthLoginControllerFromViewController:(UIViewController *)sender;
  235. - (void)showOAuthLoginControllerFromViewController:(UIViewController *)sender withCompletion:(void(^)(BOOL success))completionBlock;
  236. // Access Token Mangement
  237. - (void)clearAccessToken;
  238. - (void)loadAccessToken;
  239. - (BOOL)isAuthorized;
  240. // Clear Keys
  241. - (void)clearConsumer;
  242. // sendRequest methods, use these for every request
  243. - (NSError *)sendPOSTRequest:(OAMutableURLRequest *)request withParameters:(NSArray *)params;
  244. - (id)sendGETRequest:(OAMutableURLRequest *)request withParameters:(NSArray *)params;
  245. //
  246. // Misc Methods
  247. //
  248. // Date parser
  249. - (NSDate *)getDateFromTwitterCreatedAt:(NSString *)twitterDate;
  250. // id list generator - returns an array of id/username list strings
  251. - (NSArray *)generateRequestStringsFromArray:(NSArray *)array;
  252. // Temporaryily set keys
  253. // if you don't want your keys in memory, simply use
  254. // this method. You will have to use it before
  255. // making any API calls.
  256. - (void)temporarilySetConsumerKey:(NSString *)consumerKey andSecret:(NSString *)consumerSecret;
  257. // Use to set the consumer key when using the singleton
  258. - (void)permanentlySetConsumerKey:(NSString *)consumerKey andSecret:(NSString *)consumerSecret;
  259. // Singleton, NEVER use the -init method. Ever.
  260. + (FHSTwitterEngine *)sharedEngine;
  261. // Determines your internet status
  262. + (BOOL)isConnectedToInternet;
  263. // Determines if entities should be included
  264. @property (nonatomic, assign) BOOL includeEntities;
  265. // Logged in user's username
  266. @property (nonatomic, retain) NSString *loggedInUsername;
  267. // Logged in user's Twitter ID
  268. @property (nonatomic, retain) NSString *loggedInID;
  269. // Will be called to store the accesstoken
  270. @property (nonatomic, assign) id<FHSTwitterEngineAccessTokenDelegate> delegate;
  271. // Access Token
  272. @property (nonatomic, retain) OAToken *accessToken;
  273. @end
  274. @interface NSData (Base64)
  275. + (NSData *)dataWithBase64EncodedString:(NSString *)string;
  276. - (id)initWithBase64EncodedString:(NSString *)string;
  277. - (NSString *)base64EncodingWithLineLength:(unsigned int)lineLength;
  278. @end
  279. @interface NSString (FHSTwitterEngine)
  280. - (NSString *)fhs_trimForTwitter;
  281. - (NSString *)fhs_stringWithRange:(NSRange)range;
  282. - (BOOL)fhs_isNumeric;
  283. @end