にこれを直接投稿するのPhoneGap APIを使用しますPhoneGapプラグインを使用してGraph API iPhone/iPadからFacebookに写真を投稿してください。このプラグインは、Facebookの場所ID(Checkin Facebook with photo)を持つ写真を投稿することもでき、iOS 6.0でも正常に動作します。
それは、PhoneGapの2.1とphonegap-plugin-facebook-connectに基づいて、およびは、多分それはあなたの問題を解決することができ、任意の中間のホストを必要としていない;)
まず、PhotoCheckin.hは...ある
#import <Foundation/Foundation.h>
#import "Facebook.h"
#import "FBConnect.h"
#import <Cordova/CDV.h>
@interface PhotoCheckin : CDVPlugin
<FBDialogDelegate>
- (void) sendPost:(CDVInvokedUrlCommand*)command;
@end
第2、PhotoCheckin.m以下のとおりです。
#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
注:このメソッドはポストされたときにコールバックされ、コード行がコメントアウトされ、ユーザーに警告が表示されます。
第3に、PhotoCheckin.jsを以下に示します。
var PhotoCheckin = {
sendPost: function(photo_uri, message, place_id, success, fail) {
return Cordova.exec(success, fail, "PhotoCheckin", "sendPost", [photo_uri, message, place_id]);
}
}
最後に、プラグインを使用するときは、次のようにjsを記述します。 (.htmlの中/の.js)
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'
);
}
);
注:photo_uriは写真のURI(例えばファイル:/// ...)で、メッセージは、ユーザーのコメントやplace_idですが、場所IDですFacebookで(例えば、106522332718569)。
そして、他のプラグインと同じように、私たちはプラグインに値としてキーとPhotoCheckinとしてphotoCheckinを追加し、リソース/ Cordova.plistを編集する必要があります。そして、PhotoCheckin.h & PhotoCheckin.mプラグインという名前のフォルダにを入れてPhotoCheckin.jsあなたのJSコードでファイルが含まれています。
楽しいです!台灣から
ダニエルありがとう。 PhoneGap APIには、キャプチャされたイメージをbase64でエンコードされた文字列として取得するオプションがあります: 'document.getElementById( 'image')。src =" data:image/jpeg; base64、 "+ imageData;' 。少なくとも私は '' picture: "document.getElementById( 'image')。src" ' –
を書きました。FacebookのAPIを素早く見て、URLだけを受け取りました。画像をアップロードする必要があります。これを行う良い方法はここで見つけることができます:http://stackoverflow.com/questions/4307877/uploading-files-with-phonegap-iphone –