AppDelegate.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2014 Realm Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. // This app gives a simple example of retrieving data from the foursquare REST API
  19. // and persisting it in a Realm. To run this app, you will need to provide a foursquare
  20. // client ID and client secret. To get these, signup at https://developer.foursquare.com/
  21. #import "AppDelegate.h"
  22. #import "Venue.h"
  23. #import <Realm/Realm.h>
  24. #error Provide your foursquare client ID and client secret
  25. NSString *clientID = @"YOUR CLIENT ID";
  26. NSString *clientSecret = @"YOUR CLIENT SECRET";
  27. @implementation AppDelegate
  28. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  29. {
  30. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  31. self.window.rootViewController = [[UIViewController alloc] init];
  32. [self.window makeKeyAndVisible];
  33. // Ensure we start with an empty database
  34. [[NSFileManager defaultManager] removeItemAtURL:[RLMRealmConfiguration defaultConfiguration].fileURL error:nil];
  35. // Query Foursquare API
  36. NSDictionary *foursquareVenues = [self getFoursquareVenues];
  37. // Persist the results to Realm
  38. [self persistToDefaultRealm:foursquareVenues];
  39. return YES;
  40. }
  41. -(NSDictionary*)getFoursquareVenues
  42. {
  43. // Call the foursquare API - here we use an NSData method for our API request,
  44. // but you could use anything that will allow you to call the API and serialize
  45. // the response as an NSDictionary or NSArray
  46. NSData *apiResponse = [[NSData alloc] initWithContentsOfURL:
  47. [NSURL URLWithString:[NSString stringWithFormat:@"https://api.foursquare.com/v2/venues/search?near=San%@Francisco&client_id=%@&client_secret=%@&v=20140101&limit=50", @"%20", clientID, clientSecret]]];
  48. // Serialize the NSData object from the response into an NSDictionary
  49. NSDictionary *serializedResponse = [[NSJSONSerialization JSONObjectWithData:apiResponse
  50. options:kNilOptions
  51. error:nil]
  52. objectForKey:@"response"];
  53. // Extract the venues from the response as an NSDictionary
  54. return serializedResponse[@"venues"];
  55. }
  56. - (void)persistToDefaultRealm:(NSDictionary*)foursquareVenues
  57. {
  58. // Open the default Realm file
  59. RLMRealm *defaultRealm = [RLMRealm defaultRealm];
  60. // Begin a write transaction to save to the default Realm
  61. [defaultRealm beginWriteTransaction];
  62. for (id venue in foursquareVenues) {
  63. // Store the foursquare venue name and id in a Realm Object
  64. Venue *newVenue = [[Venue alloc] init];
  65. newVenue.foursquareID = venue[@"id"];
  66. newVenue.name = venue[@"name"];
  67. // Add the Venue object to the default Realm
  68. // (alternatively you could serialize the API response as an NSArray and call addObjectsFromArray)
  69. [defaultRealm addObject:newVenue];
  70. }
  71. // Persist all the Venues with a single commit
  72. [defaultRealm commitWriteTransaction];
  73. // Show all the venues that were persisted
  74. NSLog(@"Here are all the venues persisted to the default Realm: \n\n %@",
  75. [[Venue allObjects] description]);
  76. }
  77. @end