Facebook Graph API Upload Photo Problem - objective-c

I've searched all through the web and have downloaded the Facebook SDK thing to try out the upload photo function and it worked there.
Exact same codes over to my application and it doesn't work.
Help please???
These are my codes:
- (IBAction)pushUpload:(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 requestWithGraphPath:#"me/photos"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];
[img release];
[loadingIcon startAnimating];
}
As you can see I've placed the loadingIcon there to start animating when its uploading.. and at the request didload i did the stopanimating command when it has successfully uploaded.
-(void)request:(FBRequest *)request didLoad:(id)result{
[loadingIcon stopAnimating];
if ([result isKindOfClass:[NSArray class]]) {
result = [result objectAtIndex:0];
}
if ([result objectForKey:#"owner"]) {
[loadingIcon stopAnimating];
UIAlertView *alert;
alert = [[UIAlertView alloc] initWithTitle:#""
message:#"Photo uploaded."
delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
[alert release];
} else {
// [self.label setText:[result objectForKey:#"name"]];
}
}
The thing is, the loading icon just keeps animating and no errors and all but still no picture uploaded to my facebook account. Any ideas why??
This are the codes in my .h file:
#interface UploadPhotosViewController :
UIViewController(FBRequestDelegate, FBDialogDelegate,
FBSessionDelegate){
IBOutlet UIImageView *imageView;
IBOutlet UIActivityIndicatorView *loadingIcon;
IBOutlet UIBarButtonItem *_pushPick;
IBOutlet UIBarButtonItem *_pushUpload;
Facebook * _facebook; }
#property(readonly) Facebook *facebook;
(IBAction)pushUpload:(id)sender;
(IBAction)pushPick:(id)sender;
//UINavigationControllerDelegate, UIImagePickerControllerDelegate,
#end
Another thing to note is that there are no colors indicator for the (FBRequestDelegate, FBDialogDelegate, FBSessionDelegate) when there are supposed to be.. is it a problem?

Check if you have written delegates of Facebook in your .h file.
Also check this code
It worked for me..
NSString *string=#"Images of me";
SBJSON *jsonWriter = [[SBJSON new] autorelease];
NSDictionary* attachment = [NSDictionary dictionaryWithObjectsAndKeys:
#"My name", #"name",
string, #"description", nil];
NSString *attachmentStr = [jsonWriter stringWithObject:attachment];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"This is image",#"description",
#"Share on Facebook", #"user_message_prompt",
//actionLinksStr, #"action_links",
attachmentStr, #"attachment",
/*Your image here", #"picture",
nil];
Try this code
Hope it helps....

Try me/feed instead of me/photos.
[_facebook requestWithGraphPath:#"me/feed"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];

I think it is because you're not declaring your protocols correctly. They go in angle brackets, not in parenthesis.
#interface UploadPhotosViewController : UIViewController<FBRequestDelegate, FBDialogDelegate, FBSessionDelegate>

Related

Send any file using xmpp in cocoa application. Is it possible?

In my chat application, I am unable to send any image or file while chat. What i tried is ---
Method 1...
NSXMLElement *body = [NSXMLElement elementWithName:#"body"];
[body setStringValue:#"Send Image Testing"];
NSXMLElement *message = [NSXMLElement elementWithName:#"message"];
[message addAttributeWithName:#"type" stringValue:#"chat"];
[message addAttributeWithName:#"to" stringValue:[jid full]];
[message addAttributeWithName:#"from" stringValue:[[xmppStream myJID] full]];
[message addChild:body];
NSImage *img = [NSImage imageNamed:#"loginLogo.png"];
NSData *imageData = [img TIFFRepresentation];
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData];
NSData *data = [imageRep representationUsingType:NSJPEGFileType properties:nil];
NSString *imgStr = [NSString encodeBase64WithData:data];
NSXMLElement *ImgAttachement = [NSXMLElement elementWithName:#"attachment"];
[ImgAttachement setStringValue:imgStr];
[message addChild:ImgAttachement];
[xmppStream sendElement:message];
I added a "xmlElement" named "attachment" in "message" xmlElement. String value of "attachment" is ImageDataString encoded in "Base64" format. But this code is sending only the text to other end(not image).
Don't know the cause of failure, may be i should send NSImage or server link of the image in place of image data.
Method 2...
I also tried "XMPPOutgoingFileTransfer" classes, with following code.
[_fileTransfer sendData:decodedData
named:#"hello"
toRecipient:[XMPPJID jidWithString:#"MYUSERNAME#chat.facebook.com/RESOURCENAME"]
description:#"Baal's Soulstone, obviously."
error:&err])
But every time this is giving the same error - Error Domain=XMPPOutgoingFileTransferErrorDomain Code=-1 "Unable to send SI offer; the recipient doesn't have the required features."
Please help, if any idea
Thanks in advance
I got it working this way-
Inside setupStrem method, set up the incoming end like this -
xmppIncomingFileTransfer = [[XMPPIncomingFileTransfer alloc] init];
xmppIncomingFileTransfer.disableIBB = NO;
xmppIncomingFileTransfer.disableSOCKS5 = NO;
[xmppIncomingFileTransfer activate:xmppStream];
[xmppIncomingFileTransfer addDelegate:self delegateQueue:dispatch_get_main_queue()];
Implement the incoming end delegate methods-
- (void)xmppIncomingFileTransfer:(XMPPIncomingFileTransfer *)sender didFailWithError:(NSError *)error
{
DDLogVerbose(#"%#: Incoming file transfer failed with error: %#", THIS_FILE, error);
}
- (void)xmppIncomingFileTransfer:(XMPPIncomingFileTransfer *)sender didReceiveSIOffer:(XMPPIQ *)offer
{
DDLogVerbose(#"%#: Incoming file transfer did receive SI offer. Accepting...", THIS_FILE);
[sender acceptSIOffer:offer];
}
- (void)xmppIncomingFileTransfer:(XMPPIncomingFileTransfer *)sender didSucceedWithData:(NSData *)data
named:(NSString *)name
{
DDLogVerbose(#"%#: Incoming file transfer did succeed.", THIS_FILE);
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *fullPath = [[paths lastObject] stringByAppendingPathComponent:name];
[data writeToFile:fullPath options:0 error:nil];
DDLogVerbose(#"%#: Data was written to the path: %#", THIS_FILE, fullPath);
}
Incoming files will be written to the documents directory, you can update UI when it is done.
On the sending side-
if (!_fileTransfer) {
_fileTransfer = [[XMPPOutgoingFileTransfer alloc] initWithDispatchQueue:dispatch_get_main_queue()];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[_fileTransfer activate:appDelegate.xmppStream];
_fileTransfer.disableIBB = NO;
_fileTransfer.disableSOCKS5 = NO;
[_fileTransfer addDelegate:self delegateQueue:dispatch_get_main_queue()];
}
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *fullPath = [[paths lastObject] stringByAppendingPathComponent:filename];
NSData *data = [NSData dataWithContentsOfFile:fullPath];
NSError *err;
if (![_fileTransfer sendData:data named:filename toRecipient:[XMPPJID jidWithString:self.contact.primaryResource.jidStr] description:#"Baal's Soulstone, obviously." error:&err]) {
DDLogInfo(#"You messed something up: %#", err);
}
Implement the outgoing delegate methods -
- (void)xmppOutgoingFileTransfer:(XMPPOutgoingFileTransfer *)sender didFailWithError:(NSError *)error
{
DDLogInfo(#"Outgoing file transfer failed with error: %#", error);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"There was an error sending your file. See the logs." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
- (void)xmppOutgoingFileTransferDidSucceed:(XMPPOutgoingFileTransfer *) sender
{
DDLogVerbose(#"File transfer successful.");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Success!" message:#"Your file was sent successfully." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
Note that the disableIBB and disableSOCKS5 should match at both ends.
If the problem still exists, go to XMPPOutgoingFileTransfer.m and then to the method-
- (void)handleRecipientDiscoInfoQueryIQ:(XMPPIQ *)iq withInfo:(XMPPBasicTrackingInfo *)info
Then put a NSLOG/Breakpoint at this line -
hasSOCKS5 = hasSI && hasFT && hasSOCKS5;
hasIBB = hasSI && hasFT && hasIBB;
Both values should become TRUE when sending a file. Check which one is FALSE (causing the error), you will get an idea why the incoming end is sending FALSE instantly. Try to fix that.

saving images inside apps

Hey guys I'm coding a tweak and need a help with expert users (using theos and logos mixed with objective C) I'm adding an option to save photos in instagram (third party app) i added a save button
-(void)actionSheetDismissedWithButtonTitled:(NSString *)title{if([title isEqualtToString:#"Save"])
added the button successfully and prepared the save image code which is the following:
%hook IGFeedItemActionCell -(void)actionSheetDismissedWithButtonTitled:(NSString *)title { if ([title isEqualToString:#"Save"]) IGFeedItem *post = self.feedItem;{ UIImageWriteToSavedAlbum(post, nil,nil,nil);UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Image Saved" message:#"The image was saved."delegate:self cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];[alert show];} } %end
my question is how to link the saving code with the pictures in the app (the class for pictures is IGFeedItemPhotoView)
thanks in advance
So I wrote a full program that does what you want and something extra :)
#import <UIKit/UIKit.h>
#interface IGPost: NSObject{}
#property int mediaType;
+ (int)videoVersionForCurrentNetworkConditions;
+ (int)fullSizeImageVersionForDevice;
- (id)imageURLForImageVersion:(int)arg1;
- (id)videoURLForVideoVersion:(int)arg1;
#end
#interface IGFeedItem: IGPost{}
#end
#interface IGFeedItemActionCell: NSObject{}
#property (nonatomic,retain) IGFeedItem* feedItem;
-(void)actionSheetDismissedWithButtonTitled:(id)arg1;
#end
%hook IGFeedItemActionCell
-(void)actionSheetDismissedWithButtonTitled:(id)arg1 {
NSString *title = (NSString *)arg1;
if ([title isEqualToString:#"Save"]) {
IGFeedItem *post = self.feedItem;
if (post.mediaType == 1) {
int version = [[post class] fullSizeImageVersionForDevice];
NSURL *link = [post imageURLForImageVersion:version];
NSData *imageData = [NSData dataWithContentsOfURL:link];
UIImage *image = [UIImage imageWithData:imageData];
UIImageWriteToSavedPhotosAlbum(image, nil,nil,nil);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Image Saved" message:#"The image was saved."delegate:self cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
[alert show];
}
else {
int version = [[post class] videoVersionForCurrentNetworkConditions];
NSURL *link = [post videoURLForVideoVersion:version];
NSURLSessionTask *download = [[NSURLSession sharedSession] downloadTaskWithURL:link completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
NSURL *documentsURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] firstObject];
NSURL *tempURL = [documentsURL URLByAppendingPathComponent:[link lastPathComponent]];
[[NSFileManager defaultManager] moveItemAtURL:location toURL:tempURL error:nil];
UISaveVideoAtPathToSavedPhotosAlbum(tempURL.path, nil, NULL, NULL);
}];
[download resume];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Video Saved" message:#"The video was saved."delegate:self cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
[alert show];
}
}
else {
%orig(arg1);
}
}
%end

how to Uploading image with text to facebook using FBRequestConnection?

I am trying to upload the text with image using FBRequestConnection like as follow but I am only able to upload text,not the image.
But when I am giving any image link in place of img1(image) then I am able to see the image on the facebook wall.
UIImage *img1 = [UIImage imageNamed:#"Default#2x.png"];
NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
#"https://developers.facebook.com/ios", #"link",
img1, #"picture",
#"Facebook SDK for iOS", #"name",
#"build apps.", #"caption",
#"imagae description.", #"description",
nil];
[params setObject:#"post message" forKey:#"message"];
[FBRequestConnection
startWithGraphPath:#"me/feed"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
NSString *alertText;
if (error) {
alertText = [NSString stringWithFormat:
#"error: domain = %#, code = %d",
error.domain, error.code];
} else {
alertText = #"Posted successfully.";
}
// Show the result in an alert
[[[UIAlertView alloc] initWithTitle:#"Result"
message:alertText
delegate:self
cancelButtonTitle:#"OK!"
otherButtonTitles:nil]
show];
}];
I have taken the two permission #"publish_actions",#"user_photos"
Please let me know where I am wrong in this code.
Check out my answer in the IOS Facebook SDK 3 upload image with message
It is the similar way which you required. Just add some more parameters as per your requirements.
Change #"picture" to #"source".
If you are using new Facebook 3.1 SDK you can do it simpler:
FBRequest *req = [FBRequest requestForUploadPhoto:img1];
[req.parameters addEntriesFromDictionary:[NSMutableDictionary dictionaryWithObjectsAndKeys:#"post message", #"message", nil]];
FBRequestConnection *con = [[FBRequestConnection alloc] init];
[con addRequest:req completionHandler....

IOS Fb wall post method not working

I am using following code. It takes me to FB Dialog box where I authorize the app and after authorizing app it takes me back to my applicaiton. However It NEVER post anything on the wall.
Any ideas what is wrong?
- (void) login
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.facebook = [[Facebook alloc] initWithAppId:#"MY_APP_ID" andDelegate:self];
NSArray* permissions = [NSArray arrayWithObjects:#"publish_stream", nil];
[appDelegate.facebook authorize:permissions];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#".", #"name",
#".", #"caption",
nil];
/*
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#".", #"name",
#"Build great social apps and get more installs.", #"caption",
#"The Facebook SDK for iOS makes it easier and faster to develop Facebook integrated iOS apps.", #"description",
#"https://developers.facebook.com/ios", #"link",
#"https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png", #"picture",
nil];
*/
// Invoke the dialog
[appDelegate.facebook dialog:#"feed" andParams:params andDelegate:self];
}
You should do it like this:
graphPath = #"me/feed";
[params setObject:#"link" forKey:#"type"];
[params setObject:pict forKey:#"picture"];
[params setObject:self.url forKey:#"link"];
[params setObject:self.message forKey:#"message"];
[params setObject:self.urlName forKey:#"name"];
[params setObject:self.caption forKey:#"caption"];
[params setObject:self.urlDescription forKey:#"description"];
[appDelegate.facebook requestWithGraphPath:graphPath andParams:params andHttpMethod:#"POST" andDelegate:self];

Posting Screenshot of GameOver to FaceBook via Cocos2d

I have searched a lot of forums and thread about posting screenshot on facebook. I have found the functon which is to be added in CCDirector which creates Screenshot. I have also seen the method which post the screenshot on facebook in an album or wall. But that is not working for me.
UIImage *tempImage = [[CCDirector sharedDirector] screenshotUIImage];
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
tempImage,#"message",
nil];
[facebook requestWithGraphPath:#"me/photos"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];
screenshotUIImage is the function that makes screenshot and it is working alright.
requestWithGraphPath does not do any thing for me. I have used different keywords in params like "tempImage, #"source"," and "tempImage, #"image"," but nothing happens.
However posting text on wall, like high score with predefined image link is working alright.
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
imageSrc, #"picture",
serverLink, #"link",
postName, #"name",
customMessage, #"caption",
nil];
// Post on Facebook.
[_facebook dialog:#"feed" andParams:params andDelegate:self];
Can any one guide me through how to do the picture posting?
In your params, you can try specifying a UIImage for the the key "picture":
UIImage *tempImage = [[CCDirector sharedDirector] screenshotUIImage];
NSMutableDictionary *params = [NSMutableDictionary dictionary];
[params setObject:myCaption forKey:#"caption"];
[params setObject:myMessage forKey:#"message"];
...
// UIImage object goes here
[params setObject:tempImage forKey:#"picture"];
[facebook requestWithGraphPath:#"me/photos" andParams:params andHttpMethod:#"POST" andDelegate:self];