Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add controllers for the FB example app + update BubbleWrap.

  • Loading branch information...
commit 7fe67103651ccdfbe0cfb3594e210271519a4dde 1 parent f32db94
Clay Allsopp authored
2  .gitignore
... ... @@ -1,2 +1,4 @@
1 1 .repl_history
2 2 build
  3 +examples/FacebookGraph/build
  4 +examples/FacebookGraph/vendor
32 examples/FacebookGraph/README.md
Source Rendered
... ... @@ -0,0 +1,32 @@
  1 +# Facebook Graph Example
  2 +
  3 +The Facebook Graph API is a great example of how powerful RemoteModel is. Facebook auth code adapted from [facebook-auth-ruby-motion-example](https://github.com/aaronfeng/facebook-auth-ruby-motion-example)
  4 +
  5 +## Running
  6 +
  7 +You need [motion-cocoapods](https://github.com/HipByte/motion-cocoapods) installed to load the Facebook iOS SDK.
  8 +
  9 +It also appears that (as of May 9 2011), motion-cocoapods doesn't play nice with the FB SDK and you need to use `rake --trace` to get it to load correctly.
  10 +
  11 +You need to specify an FB app ID, which you can create [in FB's Developer app](https://www.facebook.com/developers):
  12 +
  13 +###### app_delegate.rby
  14 +
  15 +```ruby
  16 +def application(application, didFinishLaunchingWithOptions:launchOptions)
  17 + ...
  18 + @facebook = Facebook.alloc.initWithAppId("YOUR-APP-ID", andDelegate:self)
  19 + ...
  20 +end
  21 +```
  22 +
  23 +###### Rakefile
  24 +
  25 +```ruby
  26 +Motion::Project::App.setup do |app|
  27 + ...
  28 + fb_app_id = "YOUR-APP-ID"
  29 + app.info_plist['CFBundleURLTypes'] = [{'CFBundleURLSchemes' => ["fb#{fb_app_id}"]}]
  30 + ...
  31 +end
  32 +```
13 examples/FacebookGraph/Rakefile
... ... @@ -1,5 +1,6 @@
1 1 $:.unshift("/Library/RubyMotion/lib")
2 2 require 'motion/project'
  3 +require 'motion-cocoapods'
3 4
4 5 Motion::Project::App.setup do |app|
5 6 # Use `rake config' to see complete project settings.
@@ -7,4 +8,14 @@ Motion::Project::App.setup do |app|
7 8 app.files = Dir.glob(File.join(app.project_dir, '../../vendor/BubbleWrap/lib/**/*.rb')) +
8 9 Dir.glob(File.join(app.project_dir, '../../lib/**/*.rb')) +
9 10 app.files
10   -end
  11 + app.files_dependencies 'app/controllers/facebook_login_controller.rb' => 'app/initializers/remote_model.rb'
  12 + fb_app_id = "YOUR-APP-ID"
  13 + if fb_app_id == "YOUR-APP-ID"
  14 + raise "You need to specify a Facebook App ID in ./Rakefile"
  15 + end
  16 + app.info_plist['CFBundleURLTypes'] = [{'CFBundleURLSchemes' => ["fb#{fb_app_id}"]}]
  17 +
  18 + app.pods do
  19 + dependency 'Facebook-iOS-SDK'
  20 + end
  21 +end
44 examples/FacebookGraph/app/app_delegate.rb
... ... @@ -1,5 +1,49 @@
1 1 class AppDelegate
  2 + attr_accessor :facebook
  3 + attr_accessor :navigationController
  4 +
2 5 def application(application, didFinishLaunchingWithOptions:launchOptions)
  6 + @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
  7 + @navigationController = UINavigationController.alloc.init
  8 + @window.rootViewController = @navigationController
  9 +
  10 + fb_app_id = "YOUR-APP-ID"
  11 + if fb_app_id == "YOUR-APP-ID"
  12 + raise "You need to specify a Facebook App ID in ./app/app_delegate.rb"
  13 + end
  14 + @facebook = Facebook.alloc.initWithAppId(fb_app_id, andDelegate:self)
  15 +
  16 + defaults = NSUserDefaults.standardUserDefaults
  17 +
  18 + if defaults["FBAccessTokenKey"] && defaults["FBExpirationDateKey"]
  19 + @facebook.accessToken = defaults["FBAccessTokenKey"]
  20 + @facebook.expirationDate = defaults["FBExpirationDateKey"]
  21 + openFriendsContorller
  22 + else
  23 + @navigationController.pushViewController(FacebookLoginController.alloc.init, animated: false)
  24 + end
  25 +
  26 + @window.rootViewController.wantsFullScreenLayout = true
  27 + @window.makeKeyAndVisible
3 28 true
4 29 end
  30 +
  31 + def openFriendsContorller
  32 + @navigationController.setViewControllers([FriendsController.alloc.initWithUserId], animated: false)
  33 + end
  34 +
  35 + def fbDidLogin
  36 + defaults = NSUserDefaults.standardUserDefaults
  37 + defaults["FBAccessTokenKey"] = @facebook.accessToken
  38 + defaults["FBExpirationDateKey"] = @facebook.expirationDate
  39 + defaults.synchronize
  40 + openFriendsContorller
  41 + end
  42 +
  43 + def application(application,
  44 + openURL:url,
  45 + sourceApplication:sourceApplication,
  46 + annotation:annotation)
  47 + @facebook.handleOpenURL(url)
  48 + end
5 49 end
21 examples/FacebookGraph/app/controllers/facebook_login_controller.rb
... ... @@ -0,0 +1,21 @@
  1 +class FacebookLoginController < UIViewController
  2 + def viewDidLoad
  3 + self.title = "Login"
  4 + self.view.backgroundColor = UIColor.whiteColor
  5 +
  6 + button = UIButton.buttonWithType UIButtonTypeRoundedRect
  7 + button.when(UIControlEventTouchUpInside) do
  8 + UIApplication.sharedApplication.delegate.facebook.authorize nil
  9 + end
  10 + button.setTitle("FB Login", forState: UIControlStateNormal)
  11 + button.sizeToFit
  12 +
  13 + # ugly, dont really do this.
  14 + width, height = button.frame.size.width, button.frame.size.height
  15 + button.frame = CGRectMake(((self.view.frame.size.width - width) / 2).round,
  16 + ((self.view.frame.size.height - height) / 2).round,
  17 + width,
  18 + height)
  19 + self.view.addSubview button
  20 + end
  21 +end
60 examples/FacebookGraph/app/controllers/friends_controller.rb
... ... @@ -0,0 +1,60 @@
  1 +class FriendsController < UITableViewController
  2 + attr_reader :user
  3 +
  4 + def initWithUserId(id = "me")
  5 + @user = User.new(id: id)
  6 + self
  7 + end
  8 +
  9 + def initWithUser(user)
  10 + raise "User cannot be nil" if user.nil?
  11 + @user = user
  12 + self
  13 + end
  14 +
  15 + def viewDidLoad
  16 + super
  17 + self.title = @user.name ? "Friends of #{@user.name}" : "Friends of #{@user.id}"
  18 +
  19 + defaults = NSUserDefaults.standardUserDefaults
  20 + RemoteModule::RemoteModel.set_access_token(defaults["FBAccessTokenKey"])
  21 +
  22 + @activity = UIActivityIndicatorView.alloc.initWithActivityIndicatorStyle(UIActivityIndicatorViewStyleGray)
  23 + self.view.addSubview @activity
  24 + @activity.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2)
  25 + @activity.startAnimating
  26 +
  27 + @user.find_friends do |user|
  28 + @activity.stopAnimating
  29 + @activity.removeFromSuperview
  30 + self.tableView.reloadData
  31 + end
  32 + end
  33 +
  34 + def tableView(tableView, numberOfRowsInSection:section)
  35 + return @user.friends.count
  36 + end
  37 +
  38 + def tableView(tableView, cellForRowAtIndexPath:indexPath)
  39 + reuseIdentifier = "FriendCell"
  40 +
  41 + cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) || begin
  42 + cell = UITableViewCell.alloc.initWithStyle(UITableViewCellStyleSubtitle, reuseIdentifier:reuseIdentifier)
  43 + cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton
  44 + cell
  45 + end
  46 +
  47 + friend = @user.friends[indexPath.row]
  48 + cell.textLabel.text = friend.name
  49 + cell.detailTextLabel.text = friend.id
  50 +
  51 + cell
  52 + end
  53 +
  54 + def tableView(tableView, didSelectRowAtIndexPath:indexPath)
  55 + tableView.deselectRowAtIndexPath(indexPath, animated:true)
  56 + friend = @user.friends[indexPath.row]
  57 +
  58 + UIApplication.sharedApplication.delegate.navigationController.pushViewController(FriendsController.alloc.initWithUser(friend), animated: true)
  59 + end
  60 +end
6 examples/FacebookGraph/app/initializers/remote_model.rb
@@ -3,10 +3,12 @@ class RemoteModel
3 3 self.root_url = "https://graph.facebook.com/"
4 4 self.extension = ""
5 5
6   - self.default_url_options = {
  6 + def self.set_access_token(token)
  7 + self.default_url_options = {
7 8 :query => {
8   - "access_token" => "AAAAAAITEghMBAOKs0vDSPuxPZAZBZBUcP1ruJaZC2ZARYKU0j5goBI3ZCZB9K5qgzvIzwwGM1zvDxJlnbOk8Bv6PZAlButKV5cp7NRTbviyWU4cwlCIwjZCdB",
  9 + "access_token" => token
9 10 }
10 11 }
  12 + end
11 13 end
12 14 end
9 examples/FacebookGraph/app/models/User.rb
@@ -16,7 +16,14 @@ class User < RemoteModule::RemoteModel
16 16 # end
17 17 def find_friends(&block)
18 18 get(self.friends_url) do |response, json|
19   - self.friends = json[:data]
  19 + self.friends = (json && json[:data]) || []
  20 + if json.nil?
  21 + alert = UIAlertView.new
  22 + alert.title = "Friends not given"
  23 + alert.message = "Denied privacy permissions."
  24 + alert.addButtonWithTitle "OK"
  25 + alert.show
  26 + end
20 27 if block
21 28 block.call self
22 29 end
2  examples/FacebookGraph/app/models/WallPost.rb
... ... @@ -1,6 +1,8 @@
1 1 class WallPost < RemoteModule::RemoteModel
2 2 attr_accessor :id, :message
3 3
  4 + # if we encounter "from" in the JSON return,
  5 + # use the User class.
4 6 has_one :from => :user
5 7
6 8 collection_url ""
2  vendor/BubbleWrap
... ... @@ -1 +1 @@
1   -Subproject commit d2f1002340304cfcc3b287116c7b69eeefc5fbdc
  1 +Subproject commit ef429db21e5b90e8fedef75e3453717ef254d402

0 comments on commit 7fe6710

Please sign in to comment.
Something went wrong with that request. Please try again.