/
Main.cs
210 lines (165 loc) · 5.82 KB
/
Main.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.Facebook.Authorization;
using System.Text;
using Newtonsoft.Json;
/*
Facebook doco
http://developers.facebook.com/docs/opengraph/
http://developers.facebook.com/docs/reference/api/user/
http://developers.facebook.com/docs/reference/api/
To create an app
http://www.facebook.com/developers/createapp.php
*/
// iOS stuff
// http://developers.facebook.com/docs/guides/mobile
//
// Binding to MT
// http://forums.monotouch.net/yaf_postst702.aspx
// http://btouch-library.googlecode.com/files/FacebookConnect.zip
// http://code.google.com/p/btouch-library/source/browse/trunk/FacebookConnect/FacebookConnect.cs
//
// Json.NET
// http://www.brettnagy.com/post/2009/11/21/Using-JsonNET-with-MonoTouch.aspx
//
namespace Facebook
{
/// <summary>
/// Example using Facebook
/// </summary>
public class Application
{
static void Main (string[] args)
{ UIApplication.Main (args, null, "AppDelegate"); }
}
[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
public UINavigationController NavController;
UIWindow window;
UITableViewController table; // news table
UIViewController usvc; // update status input
UIBarButtonItem statusButton; // [+] button
//string clientId = "";// TODO: You must create a Facebook application to get an Id -- http://www.facebook.com/developers/createapp.php
string token;
public AppDelegate()
{
Current = this;
}
public static AppDelegate Current
{
get; private set;
}
// This method is invoked when the application has loaded its UI and its ready to run
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
window = new UIWindow (UIScreen.MainScreen.Bounds);
window.BackgroundColor = UIColor.White;
table = new UITableViewController();
NavController = new UINavigationController();
// Add the [+] button
statusButton = new UIBarButtonItem (UIBarButtonSystemItem.Add);
statusButton.Clicked += delegate
{
usvc = new UpdateStatusViewController();
NavController.PushViewController(usvc, true);
};
table.NavigationItem.SetRightBarButtonItem (statusButton, false);
FacebookAuthorizationViewController fac =
new FacebookAuthorizationViewController(
clientId
, new string[] {"read_stream", "publish_stream"} //, "user_groups"}
, FbDisplayType.Touch);
fac.AccessToken +=
delegate(string accessToken, DateTime expires)
{
token = accessToken;
//Logged in, got your accessToken here and when it expires!
NavController.PopViewControllerAnimated(true);
//Do something else here (eg: Save the accessToken and expiry date to be used in your Graph API calls)
Console.WriteLine("### Get json");
System.Net.WebClient wc = new System.Net.WebClient();
var b = wc.DownloadData(
new Uri("https://graph.facebook.com/me/home?access_token=" + token)
);
var s = Encoding.UTF8.GetString(b);
//Console.WriteLine("### Output json");
Console.WriteLine(s);
// http://www.brettnagy.com/post/2009/11/21/Using-JsonNET-with-MonoTouch.aspx
var posts = JsonConvert.DeserializeObject<Posts>( s );
// foreach(var p in posts.data)
// {
// Console.WriteLine("name: " + p.from.name);
// }
table.Title = "Facebook";
table.TableView.Source = new TableViewSource(posts);
};
NavController.PushViewController(table, false);
NavController.PushViewController(fac, true);
NavController.NavigationBar.TintColor = new UIColor(0.27f,0.52f,0.73f,1f);
window.AddSubview(NavController.View);
window.MakeKeyAndVisible ();
return true;
}
/// <summary>
/// Update Facebook status
/// </summary>
public bool UpdateStatus (string status)
{
System.Net.WebClient wc = new System.Net.WebClient();
var result = wc.UploadString
("https://graph.facebook.com/me/feed?access_token=" + token
, "message=" + status);
// Expect the result to be a json string like this
// {"id":"689847836_129432987125279"}
return result.IndexOf ("id") > 0;
}
// This method is required in iPhoneOS 3.0
public override void OnActivated (UIApplication application)
{
}
/// <summary>
/// Extends the new UITableViewSource in MonoTouch 1.2 (4-Nov-09)
/// </summary>
private class TableViewSource : UITableViewSource
{
static NSString kCellIdentifier = new NSString ("MyIdentifier");
private Posts posts;
public TableViewSource (Posts p)
{
if (p == null)
this.posts = new Posts {data = new List<Post>()};
else
this.posts = p;
}
public override int RowsInSection (UITableView tableview, int section)
{
return posts.data.Count;
}
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
UITableViewCell cell = tableView.DequeueReusableCell (kCellIdentifier);
if (cell == null)
{
cell = new UITableViewCell (UITableViewCellStyle.Subtitle, kCellIdentifier);
}
cell.TextLabel.Text = posts.data[indexPath.Row].@from.name;
cell.DetailTextLabel.Text = posts.data[indexPath.Row].message;
cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
return cell;
}
/// <summary>
/// Display selected post details
/// </summary>
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
var uivc = new PostViewController(posts.data[indexPath.Row]);
uivc.Title = posts.data[indexPath.Row].@from.name;
AppDelegate.Current.NavController.PushViewController(uivc,true);
}
}
}
}