FBRequest.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /*
  2. * Copyright 2010-present Facebook.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <CoreLocation/CoreLocation.h>
  17. #import <Foundation/Foundation.h>
  18. #import "FBGraphObject.h"
  19. #import "FBOpenGraphAction.h"
  20. #import "FBOpenGraphObject.h"
  21. #import "FBRequestConnection.h"
  22. #import "FBSDKMacros.h"
  23. /*! The base URL used for graph requests */
  24. FBSDK_EXTERN NSString *const FBGraphBasePath __attribute__((deprecated));
  25. // up-front decl's
  26. @protocol FBRequestDelegate;
  27. @class FBSession;
  28. @class UIImage;
  29. /*!
  30. @typedef FBRequestState
  31. @abstract
  32. Deprecated - do not use in new code.
  33. @discussion
  34. FBRequestState is retained from earlier versions of the SDK to give existing
  35. apps time to remove dependency on this.
  36. @deprecated
  37. */
  38. typedef NSUInteger FBRequestState __attribute__((deprecated));
  39. /*!
  40. @class FBRequest
  41. @abstract
  42. The `FBRequest` object is used to setup and manage requests to the Facebook Graph API.
  43. This class provides helper methods that simplify the connection and response handling.
  44. @discussion
  45. An <FBSession> object is required for all authenticated uses of `FBRequest`.
  46. Requests that do not require an unauthenticated user are also supported and
  47. do not require an <FBSession> object to be passed in.
  48. An instance of `FBRequest` represents the arguments and setup for a connection
  49. to Facebook. After creating an `FBRequest` object it can be used to setup a
  50. connection to Facebook through the <FBRequestConnection> object. The
  51. <FBRequestConnection> object is created to manage a single connection. To
  52. cancel a connection use the instance method in the <FBRequestConnection> class.
  53. An `FBRequest` object may be reused to issue multiple connections to Facebook.
  54. However each <FBRequestConnection> instance will manage one connection.
  55. Class and instance methods prefixed with **start* ** can be used to perform the
  56. request setup and initiate the connection in a single call.
  57. */
  58. @interface FBRequest : NSObject {
  59. @private
  60. id<FBRequestDelegate> _delegate;
  61. NSString * _url;
  62. NSString * _versionPart;
  63. NSURLConnection * _connection;
  64. NSMutableData * _responseText;
  65. #pragma GCC diagnostic push
  66. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  67. FBRequestState _state;
  68. #pragma GCC diagnostic pop
  69. NSError * _error;
  70. BOOL _sessionDidExpire;
  71. id<FBGraphObject> _graphObject;
  72. }
  73. /*!
  74. @methodgroup Creating a request
  75. @method
  76. Calls <initWithSession:graphPath:parameters:HTTPMethod:> with the default parameters.
  77. */
  78. - (instancetype)init;
  79. /*!
  80. @method
  81. Calls <initWithSession:graphPath:parameters:HTTPMethod:> with default parameters
  82. except for the ones provided to this method.
  83. @param session The session object representing the identity of the Facebook user making
  84. the request. A nil value indicates a request that requires no token; to
  85. use the active session pass `[FBSession activeSession]`.
  86. @param graphPath The Graph API endpoint to use for the request, for example "me".
  87. */
  88. - (instancetype)initWithSession:(FBSession *)session
  89. graphPath:(NSString *)graphPath;
  90. /*!
  91. @method
  92. @abstract
  93. Initializes an `FBRequest` object for a Graph API request call.
  94. @discussion
  95. Note that this only sets properties on the `FBRequest` object.
  96. To send the request, initialize an <FBRequestConnection> object, add this request,
  97. and send <[FBRequestConnection start]>. See other methods on this
  98. class for shortcuts to simplify this process.
  99. @param session The session object representing the identity of the Facebook user making
  100. the request. A nil value indicates a request that requires no token; to
  101. use the active session pass `[FBSession activeSession]`.
  102. @param graphPath The Graph API endpoint to use for the request, for example "me".
  103. @param parameters The parameters for the request. A value of nil sends only the automatically handled
  104. parameters, for example, the access token. The default is nil.
  105. @param HTTPMethod The HTTP method to use for the request. The default is value of nil implies a GET.
  106. */
  107. - (instancetype)initWithSession:(FBSession *)session
  108. graphPath:(NSString *)graphPath
  109. parameters:(NSDictionary *)parameters
  110. HTTPMethod:(NSString *)HTTPMethod;
  111. /*!
  112. @method
  113. @abstract
  114. Initialize a `FBRequest` object that will do a graph request.
  115. @discussion
  116. Note that this only sets properties on the `FBRequest`.
  117. To send the request, initialize a <FBRequestConnection>, add this request,
  118. and send <[FBRequestConnection start]>. See other methods on this
  119. class for shortcuts to simplify this process.
  120. @param session The session object representing the identity of the Facebook user making
  121. the request. A nil value indicates a request that requires no token; to
  122. use the active session pass `[FBSession activeSession]`.
  123. @param graphPath The Graph API endpoint to use for the request, for example "me".
  124. @param graphObject An object or open graph action to post.
  125. */
  126. - (instancetype)initForPostWithSession:(FBSession *)session
  127. graphPath:(NSString *)graphPath
  128. graphObject:(id<FBGraphObject>)graphObject;
  129. /*!
  130. @abstract
  131. The parameters for the request.
  132. @discussion
  133. May be used to read the parameters that were automatically set during
  134. the object initiliazation. Make any required modifications prior to
  135. sending the request.
  136. `NSString` parameters are used to generate URL parameter values or JSON
  137. parameters. `NSData` and `UIImage` parameters are added as attachments
  138. to the HTTP body and referenced by name in the URL and/or JSON.
  139. */
  140. @property (nonatomic, retain, readonly) NSMutableDictionary *parameters;
  141. /*!
  142. @abstract
  143. The <FBSession> session object to use for the request.
  144. @discussion
  145. May be used to read the session that was automatically set during
  146. the object initiliazation. Make any required modifications prior to
  147. sending the request.
  148. */
  149. @property (nonatomic, retain) FBSession *session;
  150. /*!
  151. @abstract
  152. The Graph API endpoint to use for the request, for example "me".
  153. @discussion
  154. May be used to read the Graph API endpoint that was automatically set during
  155. the object initiliazation. Make any required modifications prior to
  156. sending the request.
  157. */
  158. @property (nonatomic, copy) NSString *graphPath;
  159. /*!
  160. @abstract
  161. The HTTPMethod to use for the request, for example "GET" or "POST".
  162. @discussion
  163. May be used to read the HTTP method that was automatically set during
  164. the object initiliazation. Make any required modifications prior to
  165. sending the request.
  166. */
  167. @property (nonatomic, copy) NSString *HTTPMethod;
  168. /*!
  169. @abstract
  170. The graph object to post with the request.
  171. @discussion
  172. May be used to read the graph object that was automatically set during
  173. the object initiliazation. Make any required modifications prior to
  174. sending the request.
  175. */
  176. @property (nonatomic, retain) id<FBGraphObject> graphObject;
  177. /*!
  178. @methodgroup Instance methods
  179. */
  180. /*!
  181. @method
  182. @abstract
  183. Overrides the default version for a single request
  184. @discussion
  185. The SDK automatically prepends a version part, such as "v2.0" to API paths in order to simplify API versioning
  186. for applications. Sometimes it is preferable to explicitly set the version for a request, which can be
  187. accomplished in one of two ways. The first is to call this method and set an override version part. The second
  188. is approach is to include the version part in the api path, for example @"v2.0/me/friends"
  189. @param version This is a string in the form @"v2.0" which will be used for the version part of an API path
  190. */
  191. - (void)overrideVersionPartWith:(NSString *)version;
  192. /*!
  193. @method
  194. @abstract
  195. Starts a connection to the Facebook API.
  196. @discussion
  197. This is used to start an API call to Facebook and call the block when the
  198. request completes with a success, error, or cancel.
  199. @param handler The handler block to call when the request completes with a success, error, or cancel action.
  200. The handler will be invoked on the main thread.
  201. */
  202. - (FBRequestConnection *)startWithCompletionHandler:(FBRequestHandler)handler;
  203. /*!
  204. @methodgroup FBRequestConnection start methods
  205. @abstract
  206. These methods start an <FBRequestConnection>.
  207. @discussion
  208. These methods simplify the process of preparing a request and starting
  209. the connection. The methods handle initializing an `FBRequest` object,
  210. initializing a <FBRequestConnection> object, adding the `FBRequest`
  211. object to the to the <FBRequestConnection>, and finally starting the
  212. connection.
  213. */
  214. /*!
  215. @methodgroup FBRequest factory methods
  216. @abstract
  217. These methods initialize a `FBRequest` for common scenarios.
  218. @discussion
  219. These simplify the process of preparing a request to send. These
  220. initialize a `FBRequest` based on strongly typed parameters that are
  221. specific to the scenario.
  222. These method do not initialize an <FBRequestConnection> object. To initiate the API
  223. call first instantiate an <FBRequestConnection> object, add the request to this object,
  224. then call the `start` method on the connection instance.
  225. */
  226. // request
  227. //
  228. // Summary:
  229. // Helper methods used to create common request objects which can be used to create single or batch connections
  230. //
  231. // session: - the session object representing the identity of the
  232. // Facebook user making the request; nil implies an
  233. // unauthenticated request; default=nil
  234. /*!
  235. @method
  236. @abstract
  237. Creates a request representing a Graph API call to the "me" endpoint, using the active session.
  238. @discussion
  239. Simplifies preparing a request to retrieve the user's identity.
  240. This method does not initialize an <FBRequestConnection> object. To initiate the API
  241. call first instantiate an <FBRequestConnection> object, add the request to this object,
  242. then call the `start` method on the connection instance.
  243. A successful Graph API call will return an <FBGraphUser> object representing the
  244. user's identity.
  245. Note you may change the session property after construction if a session other than
  246. the active session is preferred.
  247. */
  248. + (FBRequest *)requestForMe;
  249. /*!
  250. @method
  251. @abstract
  252. Creates a request representing a Graph API call to the "me/friends" endpoint using the active session.
  253. @discussion
  254. Simplifies preparing a request to retrieve the user's friends.
  255. This method does not initialize an <FBRequestConnection> object. To initiate the API
  256. call first instantiate an <FBRequestConnection> object, add the request to this object,
  257. then call the `start` method on the connection instance.
  258. A successful Graph API call will return an array of <FBGraphUser> objects representing the
  259. user's friends.
  260. */
  261. + (FBRequest *)requestForMyFriends;
  262. /*!
  263. @method
  264. @abstract
  265. Creates a request representing a Graph API call to upload a photo to the app's album using the active session.
  266. @discussion
  267. Simplifies preparing a request to post a photo.
  268. To post a photo to a specific album, get the `FBRequest` returned from this method
  269. call, then modify the request parameters by adding the album ID to an "album" key.
  270. This method does not initialize an <FBRequestConnection> object. To initiate the API
  271. call first instantiate an <FBRequestConnection> object, add the request to this object,
  272. then call the `start` method on the connection instance.
  273. @param photo A `UIImage` for the photo to upload.
  274. */
  275. + (FBRequest *)requestForUploadPhoto:(UIImage *)photo;
  276. /*!
  277. @method
  278. @abstract
  279. Creates a request representing a status update.
  280. @discussion
  281. Simplifies preparing a request to post a status update.
  282. This method does not initialize an <FBRequestConnection> object. To initiate the API
  283. call first instantiate an <FBRequestConnection> object, add the request to this object,
  284. then call the `start` method on the connection instance.
  285. @param message The message to post.
  286. */
  287. + (FBRequest *)requestForPostStatusUpdate:(NSString *)message;
  288. /*!
  289. @method
  290. @abstract
  291. Creates a request representing a status update.
  292. @discussion
  293. Simplifies preparing a request to post a status update.
  294. This method does not initialize an <FBRequestConnection> object. To initiate the API
  295. call first instantiate an <FBRequestConnection> object, add the request to this object,
  296. then call the `start` method on the connection instance.
  297. @param message The message to post.
  298. @param place The place to checkin with, or nil. Place may be an fbid or a
  299. graph object representing a place.
  300. @param tags Array of friends to tag in the status update, each element
  301. may be an fbid or a graph object representing a user.
  302. */
  303. + (FBRequest *)requestForPostStatusUpdate:(NSString *)message
  304. place:(id)place
  305. tags:(id<NSFastEnumeration>)tags;
  306. /*!
  307. @method
  308. @abstract
  309. Creates a request representing a Graph API call to the "search" endpoint
  310. for a given location using the active session.
  311. @discussion
  312. Simplifies preparing a request to search for places near a coordinate.
  313. This method does not initialize an <FBRequestConnection> object. To initiate the API
  314. call first instantiate an <FBRequestConnection> object, add the request to this object,
  315. then call the `start` method on the connection instance.
  316. A successful Graph API call will return an array of <FBGraphPlace> objects representing
  317. the nearby locations.
  318. @param coordinate The search coordinates.
  319. @param radius The search radius in meters.
  320. @param limit The maxiumum number of results to return. It is
  321. possible to receive fewer than this because of the radius and because of server limits.
  322. @param searchText The text to use in the query to narrow the set of places
  323. returned.
  324. */
  325. + (FBRequest *)requestForPlacesSearchAtCoordinate:(CLLocationCoordinate2D)coordinate
  326. radiusInMeters:(NSInteger)radius
  327. resultsLimit:(NSInteger)limit
  328. searchText:(NSString *)searchText;
  329. /*!
  330. @method
  331. @abstract
  332. Creates a request representing the Graph API call to retrieve a Custom Audience "thirdy party ID" for the app's Facebook user.
  333. Callers will send this ID back to their own servers, collect up a set to create a Facebook Custom Audience with,
  334. and then use the resultant Custom Audience to target ads.
  335. @param session The FBSession to use to establish the user's identity for users logged into Facebook through this app.
  336. If `nil`, then the activeSession is used.
  337. @discussion
  338. This method will throw an exception if <[FBSettings defaultAppID]> is `nil`. The appID won't be nil when the pList
  339. includes the appID, or if it's explicitly set.
  340. The JSON in the request's response will include an "custom_audience_third_party_id" key/value pair, with the value being the ID retrieved.
  341. This ID is an encrypted encoding of the Facebook user's ID and the invoking Facebook app ID.
  342. Multiple calls with the same user will return different IDs, thus these IDs cannot be used to correlate behavior
  343. across devices or applications, and are only meaningful when sent back to Facebook for creating Custom Audiences.
  344. The ID retrieved represents the Facebook user identified in the following way: if the specified session (or activeSession if the specified
  345. session is `nil`) is open, the ID will represent the user associated with the activeSession; otherwise the ID will represent the user logged into the
  346. native Facebook app on the device. If there is no native Facebook app, no one is logged into it, or the user has opted out
  347. at the iOS level from ad tracking, then a `nil` ID will be returned.
  348. This method returns `nil` if either the user has opted-out (via iOS) from Ad Tracking, the app itself has limited event usage
  349. via the `[FBAppEvents setLimitEventUsage]` flag, or a specific Facebook user cannot be identified.
  350. */
  351. + (FBRequest *)requestForCustomAudienceThirdPartyID:(FBSession *)session;
  352. /*!
  353. @method
  354. @abstract
  355. Returns a newly initialized request object that can be used to make a Graph API call for the active session.
  356. @discussion
  357. This method simplifies the preparation of a Graph API call.
  358. This method does not initialize an <FBRequestConnection> object. To initiate the API
  359. call first instantiate an <FBRequestConnection> object, add the request to this object,
  360. then call the `start` method on the connection instance.
  361. @param graphPath The Graph API endpoint to use for the request, for example "me".
  362. */
  363. + (FBRequest *)requestForGraphPath:(NSString *)graphPath;
  364. /*!
  365. @method
  366. @abstract
  367. Creates request representing a DELETE to a object.
  368. @discussion
  369. This method simplifies the preparation of a Graph API call.
  370. This method does not initialize an <FBRequestConnection> object. To initiate the API
  371. call first instantiate an <FBRequestConnection> object, add the request to this object,
  372. then call the `start` method on the connection instance.
  373. @param object This can be an NSString, NSNumber or NSDictionary representing an object to delete
  374. */
  375. + (FBRequest *)requestForDeleteObject:(id)object;
  376. /*!
  377. @method
  378. @abstract
  379. Creates a request representing a POST for a graph object.
  380. @param graphPath The Graph API endpoint to use for the request, for example "me".
  381. @param graphObject An object or open graph action to post.
  382. @discussion This method is typically used for posting an open graph action. If you are only
  383. posting an open graph object (without an action), consider using `requestForPostOpenGraphObject:`
  384. */
  385. + (FBRequest *)requestForPostWithGraphPath:(NSString *)graphPath
  386. graphObject:(id<FBGraphObject>)graphObject;
  387. /*!
  388. @method
  389. @abstract
  390. Returns a newly initialized request object that can be used to make a Graph API call for the active session.
  391. @discussion
  392. This method simplifies the preparation of a Graph API call.
  393. This method does not initialize an <FBRequestConnection> object. To initiate the API
  394. call first instantiate an <FBRequestConnection> object, add the request to this object,
  395. then call the `start` method on the connection instance.
  396. @param graphPath The Graph API endpoint to use for the request, for example "me".
  397. @param parameters The parameters for the request. A value of nil sends only the automatically handled parameters, for example, the access token. The default is nil.
  398. @param HTTPMethod The HTTP method to use for the request. A nil value implies a GET.
  399. */
  400. + (FBRequest *)requestWithGraphPath:(NSString *)graphPath
  401. parameters:(NSDictionary *)parameters
  402. HTTPMethod:(NSString *)HTTPMethod;
  403. /*!
  404. @method
  405. @abstract
  406. Returns a newly initialized request object that can be used to create a user owned
  407. Open Graph object for the active session.
  408. @discussion
  409. This method simplifies the preparation of a Graph API call.
  410. This method does not initialize an <FBRequestConnection> object. To initiate the API
  411. call first instantiate an <FBRequestConnection> object, add the request to this object,
  412. then call the `start` method on the connection instance.
  413. @param object The Open Graph object to create. Some common expected fields include "title", "image", "url", etc.
  414. */
  415. + (FBRequest *)requestForPostOpenGraphObject:(id<FBOpenGraphObject>)object;
  416. /*!
  417. @method
  418. @abstract
  419. Returns a newly initialized request object that can be used to create a user owned
  420. Open Graph object for the active session.
  421. @discussion
  422. This method simplifies the preparation of a Graph API call.
  423. This method does not initialize an <FBRequestConnection> object. To initiate the API
  424. call first instantiate an <FBRequestConnection> object, add the request to this object,
  425. then call the `start` method on the connection instance.
  426. @param type The fully-specified Open Graph object type (e.g., my_app_namespace:my_object_name)
  427. @param title The title of the Open Graph object.
  428. @param image The link to an image to be associated with the Open Graph object.
  429. @param url The url to be associated with the Open Graph object.
  430. @param description The description to be associated with the object.
  431. @param objectProperties Any additional properties for the Open Graph object.
  432. */
  433. + (FBRequest *)requestForPostOpenGraphObjectWithType:(NSString *)type
  434. title:(NSString *)title
  435. image:(id)image
  436. url:(id)url
  437. description:(NSString *)description
  438. objectProperties:(NSDictionary *)objectProperties;
  439. /*!
  440. @method
  441. @abstract
  442. Returns a newly initialized request object that can be used to update a user owned
  443. Open Graph object for the active session.
  444. @discussion
  445. This method simplifies the preparation of a Graph API call.
  446. This method does not initialize an <FBRequestConnection> object. To initiate the API
  447. call first instantiate an <FBRequestConnection> object, add the request to this object,
  448. then call the `start` method on the connection instance.
  449. @param object The Open Graph object to update the existing object with.
  450. */
  451. + (FBRequest *)requestForUpdateOpenGraphObject:(id<FBOpenGraphObject>)object;
  452. /*!
  453. @method
  454. @abstract
  455. Returns a newly initialized request object that can be used to update a user owned
  456. Open Graph object for the active session.
  457. @discussion
  458. This method simplifies the preparation of a Graph API call.
  459. This method does not initialize an <FBRequestConnection> object. To initiate the API
  460. call first instantiate an <FBRequestConnection> object, add the request to this object,
  461. then call the `start` method on the connection instance.
  462. @param objectId The id of the Open Graph object to update.
  463. @param title The updated title of the Open Graph object.
  464. @param image The updated link to an image to be associated with the Open Graph object.
  465. @param url The updated url to be associated with the Open Graph object.
  466. @param description The updated description of the Open Graph object.
  467. @param objectProperties Any additional properties to update for the Open Graph object.
  468. */
  469. + (FBRequest *)requestForUpdateOpenGraphObjectWithId:(id)objectId
  470. title:(NSString *)title
  471. image:(id)image
  472. url:(id)url
  473. description:(NSString *)description
  474. objectProperties:(NSDictionary *)objectProperties;
  475. /*!
  476. @method
  477. @abstract
  478. Returns a newly initialized request object that can be used to upload an image
  479. to create a staging resource. Staging resources allow you to post binary data
  480. such as images, in preparation for a post of an open graph object or action
  481. which references the image. The URI returned when uploading a staging resource
  482. may be passed as the image property for an open graph object or action.
  483. @discussion
  484. This method simplifies the preparation of a Graph API call.
  485. This method does not initialize an <FBRequestConnection> object. To initiate the API
  486. call first instantiate an <FBRequestConnection> object, add the request to this object,
  487. then call the `start` method on the connection instance.
  488. @param image A `UIImage` for the image to upload.
  489. */
  490. + (FBRequest *)requestForUploadStagingResourceWithImage:(UIImage *)image;
  491. @end