Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I write a PhoneGap plug-in use <a href="http://developers.facebook.com/docs/reference/api/" rel="nofollow">Graph API</a> to post photos from iPhone/iPad to Facebook. This plug-in can also post photo with Facebook place id (means it can <strong>Checkin Facebook with photo</strong>) and works fine on iOS 6.0.</p> <p>It's based on PhoneGap 2.1 and <a href="https://github.com/davejohnson/phonegap-plugin-facebook-connect" rel="nofollow">phonegap-plugin-facebook-connect</a>, and <strong>not need</strong> any middle host, maybe it can solve your problem ;)</p> <p>First, the <strong>PhotoCheckin.h</strong> is...</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; #import "Facebook.h" #import "FBConnect.h" #import &lt;Cordova/CDV.h&gt; @interface PhotoCheckin : CDVPlugin &lt;FBDialogDelegate&gt; - (void) sendPost:(CDVInvokedUrlCommand*)command; @end </code></pre> <p>Second, <strong>PhotoCheckin.m</strong> as follow.</p> <pre><code>#import "PhotoCheckin.h" #import "FBSBJSON.h" @implementation PhotoCheckin - (void) sendPost:(CDVInvokedUrlCommand*)command { UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[command.arguments objectAtIndex:0]]]]; NSString* message = [command.arguments objectAtIndex:1]; NSString* place_id = [command.arguments objectAtIndex:2]; NSLog(@"%@",message); NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:image, @"source",message,@"message",place_id, @"place", nil]; if (FBSession.activeSession.isOpen) { [FBRequestConnection startWithGraphPath:@"me/photos" parameters:params HTTPMethod:@"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) { NSString *resultStr = nil; if (error) { //resultStr = [NSString stringWithFormat:@"error: domain = %@, code = %d",error.domain, error.code]; resultStr = [[CDVPluginResult resultWithStatus:CDVCommandStatus_JSON_EXCEPTION messageAsString:[NSString stringWithFormat:@"error: domain = %@, code = %d",error.domain, error.code]] toErrorCallbackString:command.callbackId]; }else{ //resultStr = [NSString stringWithFormat:@"Posted action, id: %@",[result objectForKey:@"id"]]; resultStr = [[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[NSString stringWithFormat:@"Posted action, id: %@",[result objectForKey:@"id"]]] toSuccessCallbackString:command.callbackId]; } //[[[UIAlertView alloc] initWithTitle:@"Check-in Result" message:resultStr delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; [self writeJavascript:resultStr]; }]; }else{ NSLog(@"no active session"); NSArray *permissions = [[NSArray alloc] initWithObjects:@"publish_stream",nil]; // more "permissions strings" at http://developers.facebook.com/docs/authentication/permissions/ // OPEN Session [FBSession openActiveSessionWithPermissions:permissions allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) { if (FB_ISSESSIONOPENWITHSTATE(status)) { [[[UIAlertView alloc] initWithTitle:@"Got FB session!" message:@"please check-in again." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; }else{ [self writeJavascript:[[CDVPluginResult resultWithStatus:CDVCommandStatus_JSON_EXCEPTION messageAsString:[NSString stringWithFormat:@"error: domain = %@, code = %d",error.domain, error.code]] toErrorCallbackString:command.callbackId]]; /*[[[UIAlertView alloc] initWithTitle:@"Login Fail" message:[NSString stringWithFormat: @"error: domain = %@, code = %d", error.domain, error.code] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] show];*/ } }]; } } @end </code></pre> <p>Notes: this method will callback when posted, some code lines were commented out, which can make alert to users.</p> <p>Third, the <strong>PhotoCheckin.js</strong> as follow.</p> <pre><code>var PhotoCheckin = { sendPost: function(photo_uri, message, place_id, success, fail) { return Cordova.exec(success, fail, "PhotoCheckin", "sendPost", [photo_uri, message, place_id]); } } </code></pre> <p>Finally, when we went to use the plug-in, we write js as follow. (in .html/.js)</p> <pre><code>PhotoCheckin.sendPost(photo_uri, message, place_id, function(result){ navigator.notification.confirm( 'message: '+result, null, 'Photo Posted', 'OK' ); }, function(error){ navigator.notification.confirm( 'message: '+error, null, 'Photo Fail', 'OK' ); } ); </code></pre> <p>Notes: <strong>photo_uri</strong> is the photo's uri (e.g. file:///...), <strong>message</strong> is user's comment and <strong>place_id</strong> is the place id in Facebook (e.g. 106522332718569).</p> <p>And like other plug-ins, we must to edit <em>Resources/Cordova.plist</em>, add <strong>photoCheckin</strong> as key and <strong>PhotoCheckin</strong> as value in plugins. And put <strong>PhotoCheckin.h</strong>&amp;<strong>PhotoCheckin.m</strong> to folder named <em>Plugins</em>, include <strong>PhotoCheckin.js</strong> file in your js code.</p> <p>have fun! from 台灣 :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload