Posting an image from a file instead to Facebook? - iphone

How would I go about posting an image from a file instead to Facebook? This works fine:
SLComposeViewController *controllerSLC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[controllerSLC setInitialText:#" iPhone app"];
[controllerSLC addURL:[NSURL URLWithString:#"http://www.example.com"]];
[controllerSLC addImage:[UIImage imageNamed:#"icon.png"]];
However I'm trying to send an image from:
self.imageView.image = [self.photo objectForKey:photoPictureKey];

Try this mate (iOS6),
send your image to this method,
Make Sure you add Social.framework and #import <Social/Social.h>
-(void) uploadToFaceBook:(UIImage *) image{
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]){
SLComposeViewController *fbController =[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
SLComposeViewControllerCompletionHandler __block completionHandler=
^(SLComposeViewControllerResult result){
[fbController dismissViewControllerAnimated:YES completion:nil];
switch(result){
case SLComposeViewControllerResultCancelled:
default:
{
NSLog(#"Cancelled.....");
}
break;
case SLComposeViewControllerResultDone:
{
NSLog(#"Posted....");
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Sent" message:nil delegate:nil cancelButtonTitle:#"Dismiss" otherButtonTitles: nil];
[alert show];
}
break;
}};
[fbController addImage:image];
[fbController setInitialText:#" iPhone App"];
[fbController addURL:[NSURL URLWithString:#"http://www.example.com"]];
[fbController setCompletionHandler:completionHandler];
[self presentViewController:fbController animated:YES completion:nil];
}
else{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Sorry"
message:#"You can't post on Facebook right now, make sure your device has an internet connection and you have at least one Facebook account setup"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}

if this [self.photo objectForKey:photoPictureKey] code returns the path of the image you can use UIImage class method that is imageWithContentsOfFile:
UIImage *fbImage = [UIImage imageWithContentsOfFile:[self.photo objectForKey:photoPictureKey]];
Then check fbImage is it is not nil before adding it to the instance of the class SLComposeViewController
if(fbImage!=nil){
[controllerSLC addImage: image];
}else{
NSLog('image is nil');
}
if this "[self.photo objectForKey:photoPictureKey]" code returns the a UIImage instance then do this:
UIImage *fbImage = [self.photo objectForKey:photoPictureKey];
if(fbImage!=nil){
[controllerSLC addImage: fbImage];
}else{
NSLog('image is nil');
}
you can also check if fbImage is null by:
if(fbImage!=nil && !([fbImage isKindOfClass:[NSNull class]])){
[controllerSLC addImage: fbImage];
}

Related

Unable to post image into facebook using SLComposeViewController?

I would like to post image into facebook and twitter. I am fine with twitter but not with facebook using SLComposeViewController class. With out add image i am able to post text and url into facebook. The problem is when i use add image i was unable to post this image and also text, url. SLComposeViewController shows image, text and url when i send. I have correct appId and i did not get any errors. But the problem is still there. I don't where the problem is. Please help me.
- (void)performFBRequestUploadForImage{
[self showListOfFaceBookAccountsFromStore];
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
SLComposeViewController *mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
SLComposeViewControllerCompletionHandler __block completionHandler=^(SLComposeViewControllerResult result){
NSString *output;
switch (result) {
case SLComposeViewControllerResultCancelled:
output = #"ACtionCancelled";
break;
case SLComposeViewControllerResultDone:
output = #"Post Successfull";
[self dismissViewControllerAnimated:YES completion:nil];
break;
default:
break;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Face Book Message" message:output delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
};
[mySLComposerSheet addImage:[UIImage imageNamed:#"images4.jpg"]];
[mySLComposerSheet setInitialText:#"I am developer."];
[mySLComposerSheet addURL:[NSURL URLWithString:#"http://stackoverflow.com/"]];
[mySLComposerSheet setCompletionHandler:completionHandler];
[self presentViewController:mySLComposerSheet animated:YES completion:nil];
}
}
- (void)showListOfFaceBookAccountsFromStore
{
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
if( [SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook] )
{
NSDictionary *options = #{
#"ACFacebookAppIdKey" : myAppId,
#"ACFacebookPermissionsKey" : #[#"publish_stream"],
#"ACFacebookAudienceKey" : ACFacebookAudienceFriends};
[accountStore requestAccessToAccountsWithType:accountType options:options completion:^(BOOL granted, NSError *error){
if(granted) {
ACAccount * account = [[accountStore accountsWithAccountType:accountType] lastObject];
NSLog(#"Facebook user: %#",[account username]);
if([account username]==NULL){
[self facebookAlert];
} else {
}
}
else{
NSLog(#"Read permission error: %#", [error localizedDescription]);
}
}];
} else {
[self facebookAlert];
}
}
I used below code in my projects to post the image it works fine.
if([SLComposeViewController instanceMethodForSelector:#selector(isAvailableForServiceType)] != nil)
{
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){
if (result == SLComposeViewControllerResultCancelled) {
NSLog(#"Cancelled");
} else
{
NSLog(#"Done");
}
[controller dismissViewControllerAnimated:YES completion:Nil];
};
controller.completionHandler =myBlock;
[controller setInitialText:#"Check out my Christmas Gift!"];
[controller addImage:#"gift.jpg"];
[self presentViewController:controller animated:YES completion:Nil];
}
You just try follow below tutorials
Tutorial 1
2.Tutorial 2
I checked the internet and noticed a few discussion in the recent 1 or 2 days related to this issue and they seem to related to Facebook bug. So if your code post messages and images successfully to Twitter, then don't worry! you are doing correctly.
https://stackoverflow.com/questions/14868518/sharekit-not-sharing-link-to-facebook/
https://discussions.apple.com/thread/4805777
I have resolved through open present-view controller in navigation-bar.it may help you.
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[controller setInitialText:shareFrom];
[controller addImage:self.photoImageView.image];
[controller addURL:[NSURL URLWithString:dayCareWebsite]];
dispatch_async(dispatch_get_main_queue(), ^ {
[self.navigationController presentViewController:controller animated:YES completion:nil];
});

Present Social View Controller Cocos2d

Im trying to use the social framework to present the "Post To Facebook" view controller from within Cocos2d. This is the code I would usually use in a storyboard app
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *facebook = [[SLComposeViewController alloc] init];
facebook = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[facebook setInitialText:[NSString stringWithFormat:#"text"]];
[self presentViewController:facebook animated:YES completion:nil];
[facebook setCompletionHandler:^(SLComposeViewControllerResult result) {
NSString *output;
switch (result) {
case SLComposeViewControllerResultCancelled:
output = #"Action Cancelled";
break;
case SLComposeViewControllerResultDone:
output = #"Posted";
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
[ud setInteger:1 forKey:#"Shared"];
[ud synchronize];
default:
break;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Facebook" message:output delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
[alert show];
}];
}
How would I get this up and running from within Cocos2d? currently it throws up a warning for the line
[self presentViewController:facebook animated:YES completion:nil];
Thanks in advance
In cocos2d 2.0 you can use [CCDirector sharedDirector] instead of self.
[[CCDirector sharedDirector] presentViewController:facebook animated:YES completion:nil];
This works because CCDirector inherits from UIViewController.
This works for me....
-(void) facebookWithInitialText:(NSString*) text {
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
CCLOG( #"can post to Facebook");
AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[controller setInitialText:text]; // set initial text
[controller addImage:[UIImage imageNamed:#"Icon-72.png"]]; //add an image
[controller addURL:[NSURL URLWithString:#"http://www.cartoonsmart.com"]]; //add a URL to it
[[app navController] presentViewController:controller animated:YES completion:nil ];
[controller setCompletionHandler:^(SLComposeViewControllerResult result){
[[app navController] dismissModalViewControllerAnimated:YES];
NSString *outout = [[NSString alloc] init];
switch (result) {
case SLComposeViewControllerResultCancelled:
outout = #"Post Cancled";
break;
case SLComposeViewControllerResultDone:
outout = #"Post Done";
default:
break;
}
UIAlertView *myalertView = [[UIAlertView alloc]initWithTitle:#"Facebook" message:outout delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[myalertView show];
}];
} else {
CCLOG( #"Facebook not accessible or one account not setup.");
}
}

How to have a precomposed SMS message for the user in iOS SDK

I used the sample code on the Apple Dev site to learn how to set pre-composed emails, but is there a way to set precomposed SMS messages, similarly?
First you have to add the framework MessageUI to your project and import the library "MessageUI/MessageUI.h". Then conform to the protocol <MFMessageComposeViewControllerDelegate>.
Now to send an SMS:
- (IBAction) sendSMS:(id)sender
{
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
if([MFMessageComposeViewController canSendText])
{
controller.body = #"The body of the SMS you want";
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
}
[controller release];
}
To catch the result of the sending operation:
- (void) messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
switch(result)
{
case MessageComposeResultCancelled: break; //handle cancelled event
case MessageComposeResultFailed: break; //handle failed event
case MessageComposeResultSent: break; //handle sent event
}
[self dismissModalViewControllerAnimated:YES];
}
The body property on MFMessageComposeViewController allows you to set the message body just like you can for an email.
Check out the documentation: http://developer.apple.com/library/ios/#documentation/MessageUI/Reference/MFMessageComposeViewController_class/Reference/Reference.html
See this article on Apple's Dev Center:
Sending an SMS Message
PresentModalViewController is now deprecated in IOS 6. So i used
[self presentViewController:controller animated:YES completion:nil];
whole code is as following
-(IBAction)sendSMSButtonTouchupInside:(id)sender
{
MFMessageComposeViewController *controller =
[[MFMessageComposeViewController alloc] init];
if([MFMessageComposeViewController canSendText])
{
controller.body = #"Whatever you want";
controller.recipients = [NSArray arrayWithObjects:#"03136602888", nil];
controller.messageComposeDelegate = self;
[self presentViewController:controller animated:YES completion:nil];
}
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"MyApp" message:#"Unknown Error"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
switch (result)
{
case MessageComposeResultCancelled:
NSLog(#"Cancelled");
[alert show];
break;
case MessageComposeResultFailed:
[alert show];
break;
case MessageComposeResultSent:
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}

Trouble opening the MFMailComposeViewController on the device, works in simulator.

I am doing this its working in simulator but when we try to open in device then program is terminating.
Plz suggess me fast.
MFMailComposeViewController *mail=[[MFMailComposeViewController alloc]init];
mail.mailComposeDelegate=self;
[mail setToRecipients:[NSArray arrayWithObjects:#"marketing#realestateinvestar.com.au",nil]];
//[self becomeFirstResponder];
mail.navigationBar.tintColor=[UIColor blackColor];
[self presentModalViewController:mail animated:YES];
if ([MFMessageComposeViewController canSendText])
Your problem is here. You are trying to check whether device will be able to send Text messages and not email Message. you should try using
if([MFMailComposeViewController canSendMail])
The problem might be that your device is not configured to any accounts in the mail.Please check this once.
Have you implement MFMailComposeViewControllerdelegate methods in your code??
#pragma mark --------------------------------------------
#pragma mark MFMailComposeViewController delegate Methods
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
switch (result) {
case MFMailComposeResultCancelled:
NSLog(#"Mail send canceled.");
/*
Execute your code for canceled event here ...
*/
break;
case MFMailComposeResultSaved:
NSLog(#"Mail saved.");
/*
Execute your code for email saved event here ...
*/
break;
case MFMailComposeResultSent: {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Mail Sent" message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
alert.tag = 1;
alert.delegate = self;
[alert show];
[alert release];
break;
}
case MFMailComposeResultFailed: {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Mail Sending Failed" message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
alert.tag = 2;
alert.delegate = self;
[alert show];
[alert release];
break;
}
default:
break;
}
[controller dismissModalViewControllerAnimated:YES];//dismissing modal view controller
}
Your code looks ok, but did you check if the device can send mail:
if ([MFMailComposeViewController canSendText]) {
MFMailComposeViewController *mail=[[MFMailComposeViewController alloc]init];
mail.mailComposeDelegate=self;
[mail setToRecipients:[NSArray arrayWithObjects:#"marketing#realestateinvestar.com.au",nil]];
mail.navigationBar.tintColor=[UIColor blackColor];
[self presentModalViewController:mail animated:YES];
[mail release], mail = nil;
} else {
// show message to the use that he can't send an email.
}

How to take picture from Camera & saved in Photo Gallery by programmatically?

In iPhone Apps, I want to take pictures from Camera & save into Photo Gallery by programmatically. Is it possible in iPhone Simulator?
so please tell me any link or materials you have.
Thanks in advance
You would use uiimagepickercontroller for this purpose. First of all, import the MobileCoreServices.framework. Then pull up the camera:
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
NSArray *media = [UIImagePickerController
availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera];
if ([media containsObject:(NSString*)kUTTypeImage] == YES) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
//picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
[picker setMediaTypes:[NSArray arrayWithObject:(NSString *)kUTTypeImage]];
picker.delegate = self;
[self presentModalViewController:picker animated:YES];
//[picker release];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Unsupported!"
message:#"Camera does not support photo capturing."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Unavailable!"
message:#"This device does not have a camera."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
Then, save the photo by implementing the uiimagepickercontrollerdelegate and a save function:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(#"Media Info: %#", info);
NSString *mediaType = [info valueForKey:UIImagePickerControllerMediaType];
if([mediaType isEqualToString:(NSString*)kUTTypeImage]) {
UIImage *photoTaken = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
//Save Photo to library only if it wasnt already saved i.e. its just been taken
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
UIImageWriteToSavedPhotosAlbum(photoTaken, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
}
}
[picker dismissModalViewControllerAnimated:YES];
[picker release];
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
UIAlertView *alert;
//NSLog(#"Image:%#", image);
if (error) {
alert = [[UIAlertView alloc] initWithTitle:#"Error!"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissModalViewControllerAnimated:YES];
}
More info on image:didFinishSaving.... can be found here
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIKitFunctionReference/Reference/reference.html
Also have a look at this post
https://stackoverflow.com/questions/1282830/uiimagepickercontroller-uiimage-memory-and-more
it is not possible in simulator.You have to use the device.
I do hope you realize that the simulator does not have a camera..... So I guess thats not possible. You can definitely use a device.... You can make use of UIImagePickerController for capturing images.
There are 3 prerequesites needed to take a picture programmatically:
The device needs to have a camera check by
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
Camera controls need to be hidden (picker is a UIImagePickerController)
picker.showsCameraControls = NO;
use the takePicture from UIImagePickerController
[picker takePicture];
Side note because it did bug me for a few minutes, the takePicture method also dismisses the view.
Also posted a more complete answer here Other Stack answer