-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
DemoAppViewController.m
264 lines (220 loc) · 7.65 KB
/
DemoAppViewController.m
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
* Copyright 2010 Facebook
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import "DemoAppViewController.h"
#import "FBConnect.h"
// Your Facebook APP Id must be set before running this example
// See http://www.facebook.com/developers/createapp.php
static NSString* kAppId = nil;
@implementation DemoAppViewController
@synthesize label = _label;
//////////////////////////////////////////////////////////////////////////////////////////////////
// UIViewController
/**
* initialization
*/
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
_permissions = [[NSArray arrayWithObjects:
@"read_stream", @"offline_access",nil] retain];
}
return self;
}
/**
* Set initial view
*/
- (void) viewDidLoad {
_facebook = [[Facebook alloc] init];
[self.label setText:@"Please log in"];
_getUserInfoButton.hidden = YES;
_getPublicInfoButton.hidden = YES;
_publishButton.hidden = YES;
_uploadPhotoButton.hidden = YES;
_fbButton.isLoggedIn = NO;
[_fbButton updateImage];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// NSObject
- (void) dealloc {
[_label release];
[_fbButton release];
[_getUserInfoButton release];
[_getPublicInfoButton release];
[_publishButton release];
[_uploadPhotoButton release];
[_facebook release];
[_permissions release];
[super dealloc];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// private
/**
* Example of facebook login and permission request
*/
- (void) login {
[_facebook authorize:kAppId permissions:_permissions delegate:self];
}
/**
* Example of facebook logout
*/
- (void) logout {
[_facebook logout:self];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// IBAction
/**
* Login/out button click
*/
- (IBAction) fbButtonClick: (id) sender {
if (_fbButton.isLoggedIn) {
[self logout];
} else {
[self login];
}
}
/**
* Example of graph API CAll
*
* This lets you make a Graph API Call to get the information of current logged in user.
*/
- (IBAction) getUserInfo: (id)sender {
[_facebook requestWithGraphPath:@"me" andDelegate:self];
}
/**
* Example of REST API CAll
*
* This lets you make a REST API Call to get a user's public information with FQL.
*/
- (IBAction) getPublicInfo: (id)sender {
NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"SELECT uid,name FROM user WHERE uid=4", @"query",
nil];
[_facebook requestWithMethodName: @"fql.query"
andParams: params
andHttpMethod: @"POST"
andDelegate: self];
}
/**
* Example of display Facebook dialogs
*
* This lets you publish a story to the user's stream. It uses UIServer, which is a consistent
* way of displaying user-facing dialogs
*/
- (IBAction) publishStream: (id)sender {
SBJSON *jsonWriter = [[SBJSON new] autorelease];
NSDictionary* actionLinks = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:
@"Always Running",@"text",@"http://itsti.me/",@"href", nil], nil];
NSString *actionLinksStr = [jsonWriter stringWithObject:actionLinks];
NSDictionary* attachment = [NSDictionary dictionaryWithObjectsAndKeys:
@"a long run", @"name",
@"The Facebook Running app", @"caption",
@"it is fun", @"description",
@"http://itsti.me/", @"href", nil];
NSString *attachmentStr = [jsonWriter stringWithObject:attachment];
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
kAppId, @"api_key",
@"Share on Facebook", @"user_message_prompt",
actionLinksStr, @"action_links",
attachmentStr, @"attachment",
nil];
[_facebook dialog: @"stream.publish"
andParams: params
andDelegate:self];
}
-(IBAction) uploadPhoto: (id)sender {
NSString *path = @"http://www.facebook.com/images/devsite/iphone_connect_btn.jpg";
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
img, @"picture",
nil];
[_facebook requestWithMethodName: @"photos.upload"
andParams: params
andHttpMethod: @"POST"
andDelegate: self];
[img release];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
/**
* Callback for facebook login
*/
-(void) fbDidLogin {
[self.label setText:@"logged in"];
_getUserInfoButton.hidden = NO;
_getPublicInfoButton.hidden = NO;
_publishButton.hidden = NO;
_uploadPhotoButton.hidden = NO;
_fbButton.isLoggedIn = YES;
[_fbButton updateImage];
}
/**
* Callback for facebook did not login
*/
- (void)fbDidNotLogin:(BOOL)cancelled {
NSLog(@"did not login");
}
/**
* Callback for facebook logout
*/
-(void) fbDidLogout {
[self.label setText:@"Please log in"];
_getUserInfoButton.hidden = YES;
_getPublicInfoButton.hidden = YES;
_publishButton.hidden = YES;
_uploadPhotoButton.hidden = YES;
_fbButton.isLoggedIn = NO;
[_fbButton updateImage];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// FBRequestDelegate
/**
* Callback when a request receives Response
*/
- (void)request:(FBRequest*)request didReceiveResponse:(NSURLResponse*)response{
NSLog(@"received response");
};
/**
* Called when an error prevents the request from completing successfully.
*/
- (void)request:(FBRequest*)request didFailWithError:(NSError*)error{
[self.label setText:[error localizedDescription]];
};
/**
* Called when a request returns and its response has been parsed into an object.
* The resulting object may be a dictionary, an array, a string, or a number, depending
* on thee format of the API response.
*/
- (void)request:(FBRequest*)request didLoad:(id)result {
if ([result isKindOfClass:[NSArray class]]) {
result = [result objectAtIndex:0];
}
if ([result objectForKey:@"owner"]) {
[self.label setText:@"Photo upload Success"];
} else {
[self.label setText:[result objectForKey:@"name"]];
}
};
///////////////////////////////////////////////////////////////////////////////////////////////////
// FBDialogDelegate
/**
* Called when a UIServer Dialog successfully return
*/
- (void)dialogDidComplete:(FBDialog*)dialog{
[self.label setText:@"publish successfully"];
}
@end