6
6
// Copyright (c) 2013 Facebook Inc. All rights reserved.
7
7
//
8
8
9
+ #import < FacebookSDK/FacebookSDK.h>
9
10
#import " OGShareViewController.h"
10
11
11
12
@interface OGShareViewController ()
@@ -14,25 +15,138 @@ @interface OGShareViewController ()
14
15
15
16
@implementation OGShareViewController
16
17
17
- - (id ) initWithNibName : ( NSString *) nibNameOrNil bundle : ( NSBundle *) nibBundleOrNil
18
+ - (IBAction ) publishOGStoryWithImage : ( id ) sender
18
19
{
19
- self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil];
20
- if (self) {
21
- // Custom initialization
22
- }
23
- return self;
20
+ // Retrieve a picture from the device's photo library
21
+ /*
22
+ NOTE: SDK Image size limits are 480x480px minimum resolution to 12MB maximum file size.
23
+ In this app we're not making sure that our image is within those limits but you should.
24
+ Error code for images that go below or above the size limits is 102.
25
+ */
26
+ UIImagePickerController *imagePicker = [[UIImagePickerController alloc ] init ];
27
+ [imagePicker setSourceType: UIImagePickerControllerSourceTypePhotoLibrary];
28
+ [imagePicker setDelegate: self ];
29
+ [self presentViewController: imagePicker animated: YES completion: nil ];
30
+
24
31
}
25
32
26
- - (void )viewDidLoad
33
+ // When the user is done picking the image
34
+ - (void )imagePickerController : (UIImagePickerController *)picker didFinishPickingMediaWithInfo : (NSDictionary *)info
27
35
{
28
- [super viewDidLoad ];
29
- // Do any additional setup after loading the view from its nib.
36
+
37
+ // / Package the image inside a dictionary
38
+ NSArray * image = @[@{@" url" : [info objectForKey: UIImagePickerControllerOriginalImage], @" user_generated" : @" true" }];
39
+
40
+ // Create an object
41
+ id <FBGraphObject> object =
42
+ [FBGraphObject openGraphObjectForPostWithType: @" share-sample:tutorial"
43
+ title: @" Sharing Tutorial"
44
+ image: @" http://i.imgur.com/g3Qc1HN.png"
45
+ url: @" https://developers.facebook.com/docs/ios/share/"
46
+ description: @" Allow your users to share stories on Facebook from your app using the iOS SDK." ];
47
+
48
+ // Create an action
49
+ id <FBOpenGraphAction> action = (id <FBOpenGraphAction>)[FBGraphObject graphObject ];
50
+
51
+ // Set image on the action
52
+ // [action setObject:image forKey:@"image"];
53
+
54
+ // Link the object to the action
55
+ [action setObject: object forKey: @" tutorial" ];
56
+
57
+ // Tag one or multiple users using the users' ids
58
+ // [action setTags:@[<user-ids>]];
59
+
60
+ // Tag a place using the place's id
61
+ id <FBGraphPlace> place = (id <FBGraphPlace>)[FBGraphObject graphObject ];
62
+ [place setId: @" 141887372509674" ]; // Facebook Seattle
63
+ [action setPlace: place];
64
+
65
+ // Dismiss the image picker off the screen
66
+ [self dismissViewControllerAnimated: YES completion: nil ];
67
+
68
+ // Check if the Facebook app is installed and we can present the share dialog
69
+ FBOpenGraphActionShareDialogParams *params = [[FBOpenGraphActionShareDialogParams alloc ] init ];
70
+ params.action = action;
71
+ params.actionType = @" share-sample:complete" ;
72
+
73
+ // If the Facebook app is installed and we can present the share dialog
74
+ if ([FBDialogs canPresentShareDialogWithOpenGraphActionParams: params]) {
75
+ // Show the share dialog
76
+ [FBDialogs presentShareDialogWithOpenGraphAction: action
77
+ actionType: @" share-sample:complete"
78
+ previewPropertyName: @" tutorial"
79
+ handler: ^(FBAppCall *call, NSDictionary *results, NSError *error) {
80
+ if (error) {
81
+ // There was an error
82
+ NSLog ([NSString stringWithFormat: @" Error publishing story: %@ " , error.description]);
83
+ [[[UIAlertView alloc ] initWithTitle: @" Error posting to Facebook"
84
+ message: @" Please try again later"
85
+ delegate: self
86
+ cancelButtonTitle: @" OK"
87
+ otherButtonTitles: nil ] show ];
88
+ } else {
89
+ // Success
90
+ NSLog (@" result %@ " , results);
91
+ }
92
+ }];
93
+
94
+ // If the Facebook app is NOT installed and we can't present the share dialog
95
+ } else {
96
+ // FALLBACK: publish just a link using the Feed dialog
97
+
98
+ // Put together the dialog parameters
99
+ NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
100
+ @" Sharing Tutorial" , @" name" ,
101
+ @" Build great social apps and get more installs." , @" caption" ,
102
+ @" Allow your users to share stories on Facebook from your app using the iOS SDK." , @" description" ,
103
+ @" https://developers.facebook.com/docs/ios/share/" , @" link" ,
104
+ @" http://i.imgur.com/g3Qc1HN.png" , @" picture" ,
105
+ nil ];
106
+
107
+ // Show the feed dialog
108
+ [FBWebDialogs presentFeedDialogModallyWithSession: nil
109
+ parameters: params
110
+ handler: ^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
111
+ if (error) {
112
+ // Error launching the dialog or publishing a story.
113
+ NSLog ([NSString stringWithFormat: @" Error publishing story: %@ " , error.description]);
114
+ } else {
115
+ if (result == FBWebDialogResultDialogNotCompleted) {
116
+ // User canceled.
117
+ NSLog (@" User cancelled." );
118
+ } else {
119
+ // Handle the publish feed callback
120
+ NSDictionary *urlParams = [self parseURLParams: [resultURL query ]];
121
+
122
+ if (![urlParams valueForKey: @" post_id" ]) {
123
+ // User canceled.
124
+ NSLog (@" User cancelled." );
125
+
126
+ } else {
127
+ // User clicked the Share button
128
+ NSString *result = [NSString stringWithFormat: @" Posted story, id: %@ " , [urlParams valueForKey: @" post_id" ]];
129
+ NSLog (@" result %@ " , result);
130
+ }
131
+ }
132
+ }
133
+ }];
134
+
135
+ }
136
+
30
137
}
31
138
32
- - (void )didReceiveMemoryWarning
33
- {
34
- [super didReceiveMemoryWarning ];
35
- // Dispose of any resources that can be recreated.
139
+ // A function for parsing URL parameters.
140
+ - (NSDictionary *)parseURLParams : (NSString *)query {
141
+ NSArray *pairs = [query componentsSeparatedByString: @" &" ];
142
+ NSMutableDictionary *params = [[NSMutableDictionary alloc ] init ];
143
+ for (NSString *pair in pairs) {
144
+ NSArray *kv = [pair componentsSeparatedByString: @" =" ];
145
+ NSString *val =
146
+ [kv[1 ] stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
147
+ params[kv[0 ]] = val;
148
+ }
149
+ return params;
36
150
}
37
151
38
152
@end
0 commit comments