I developed with WinDev Mobile , which generates a project I then opened with XCode to compile .
Only in WM sharing functionality on facebook has not yet been developed, I must to develop in ObjC
I started with the code:
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *controllerSLC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook] ;
[controllerSLC setInitialText:myMessage];
[controllerSLC addURL:[NSURL URLWithString:myUrl]];
[controllerSLC addImage:[UIImage imageNamed:myImage]];
// NSLog (#"% #", [UIApplication sharedApplication] keyWindow.rootViewController.)
[self presentViewController:controllerSLC animated:YES completion:Nil];
controllerSLC.completionHandler = ^(SLComposeViewControllerResult result) {
NSString *output = nil;
switch(result) {
SLComposeViewControllerResultDone box:
output = # "Your tweet has-been sent" ;
status = 2;
// NSLog (#" SENT ");
break;
SLComposeViewControllerResultCancelled box:
output = #"Your tweet has-been canceled ";
status = 3;
// NSLog (#" canceled ");
break;
default:
break;
}
[controllerSLC dismissViewControllerAnimated:YES completion:nil] ;
};
}
I only got an error :
Warning Attempt to present <SLComposeViewController > on <CFenPrincipaleViewController> Whose view is not in the window hierarchy !
I added the code :
UIViewController *activeController = [UIApplication sharedApplication] keyWindow.rootViewController. ;
if ([ activeController isKindOfClass:[UINavigationController class]]) {
activeController = [(UINavigationController *)activeController visibleViewController] ;
}
[activeController presentViewController:controllerSLC animated:YES completion:Nil];
It works on the window home application, I have no error .
But when I call the same function on another window , the error returns ....
Thank you in advance for your help
I had the same issue...give this a shot!
First get the root view controller....
-(UIViewController*) getRootViewController {
return [UIApplication sharedApplication].keyWindow.rootViewController;
}
Now instead of activeController = [(UINavigationController *)activeController visibleViewController] ;
Do this:
UIViewController* activeController = [self getRootViewController];
Now your last line [activeController presentViewController:controllerSLC animated:YES completion:Nil]; should work.
Hope this helps.
Related
Previously on iOS7, I have tested my SMS module and it worked nicely. After updating the iOS version, I noticed that the SMS module have some problems.
In my .h file
#import <MessageUI/MFMessageComposeViewController.h>
#interface ViewController : UIViewController<UITextFieldDelegate,MFMessageComposeViewControllerDelegate>
In my .m file
- (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSArray *)recipients
{
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
if([MFMessageComposeViewController canSendText])
{
controller.body = bodyOfMessage;
controller.recipients = recipients;
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
}
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
if (result == MessageComposeResultCancelled){
NSLog(#"Message cancelled");
}
else if (result == MessageComposeResultSent){
NSLog(#"Message sent");
}
else{
NSLog(#"Message failed");
}
}
After I press send, in the log have show "Message sent" but the view is still at the message screen. I have no idea why it will not go back to my application.
Need help to find the problem to why it will not go back to my application.
Thanks in advance.
It seems you are not dismissing the mailcomposer after it is presented. You will have to dismiss the presented MFMessageComposeViewController in the following method:
-(void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
if (result == MessageComposeResultCancelled){
NSLog(#"Message cancelled");
}
else if (result == MessageComposeResultSent){
NSLog(#"Message sent");
}
else{
NSLog(#"Message failed");
}
[self dismissViewControllerAnimated:YES completion:nil]; //<---- This line
}
Moreover, - (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated is deprecated since iOS 6. Use - (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion instead like this:
[self presentViewController:mailComposer animated:YES completion:nil];
I am facing one issue for send message to multiple contacts :
301 up contacts : Message app is not opening, If we click invite on multiple time then iMessage is opening but it will take more time (2 minute) to load message
351 up contacts : Message is open with black new message screen then come back to our application with out opening message screen.
Here is my code:
contacts is the array of phone number
NSMutableArray *contacts = [NSMutableArray array];
for (User *user in users) {
if (user.phone.length) {
NSString *strphonenumber = [NSString stringWithFormat:#"%#",user.phone];
[contacts addObject:strphonenumber];
}
}
MFMessageComposeViewController *messanger = [[MFMessageComposeViewController alloc]init];
messanger.messageComposeDelegate = self;
messanger.recipients = contacts;
messanger.body = [NSString stringWithFormat:#“body”;
[self presentViewController:messanger animated:YES completion:NULL];
I am getting this error:
<CKSMSComposeRemoteViewController: 0x12802f810> timed out waiting for fence barrier from com.apple.mobilesms.compose
Try this...
NSString *strContacts = [NSString stringWithFormat:#"%#",add multipal contacts];
MFMessageComposeViewController *message = [[MFMessageComposeViewController new];
message.recipients = #[strContacts];
I to had the same problem then figured
messanger.recipients = // should always be an array of strings.
Make sure the phone numbers you send to messanger.recipients are NSString.
this works for me:
set delegate to interface:
#interface ViewController <MFMessageComposeViewControllerDelegate>{}
check if device is able to send message
if([MFMessageComposeViewController canSendText] ){
//device is possible to send messages
}else{
//device can't send messages
}
prepare message:
MFMessageComposeViewController* comp = [[MFMessageComposeViewController alloc] init];
//set properties
comp.body = #"body";
comp.recipients = [NSArray arrayWithObjects:phone1, phone2, nil];
comp.messageComposeDelegate = self;
open dialog:
[self presentViewController:comp animated:YES completion:nil];
determine result
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
//test result
switch (result) {
case MessageComposeResultCancelled:
[self makeAlert:#"Result canceled"];
break;
//message was sent
case MessageComposeResultSent:
[self makeAlert:#"Result sent"];
break;
case MessageComposeResultFailed:
[self makeAlert:#"Result Failed"];
break;
default:
break;
}
//dismiss view
[self dismissViewControllerAnimated:YES completion:nil];
}
Just use this method:
- (IBAction)sendSMS:(id)sender {
MFMessageComposeViewController *controller =
[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText]){
controller.body = #"Hello";
controller.recipients = [NSArray arrayWithObjects:#"12345678",#"87654321", nil];
controller.messageComposeDelegate = self;
[self presentViewController:controller animated:YES completion:nil];
}
}
Callback method:
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
switch (result) {
case MessageComposeResultCancelled:
NSLog(#"Cancelled");
break;
case MessageComposeResultFailed:
NSLog(#"Error occured");
break;
case MessageComposeResultSent:
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}
I'm looking to add functionality that when an event happens on my app, it will post to my facebook timeline. That said, I'm trying to get it to look very similar to how when you post a youtube video to the timeline where there is a thumbnail image to the left and a title and subtitle next to it.
Anyone have any ideas how I can accomplish this?
You can't make a post automatically from code, but you can open a system window with predefined message and image attachment.
Sample code:
- (BOOL)postToFacebook:(NSString*)title andDescription:(NSString*)description {
UIWindow *frontWindow = [[UIApplication sharedApplication] keyWindow];
UIViewController *vc = [frontWindow rootViewController];
BOOL success = NO;
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result) {
if (result == SLComposeViewControllerResultCancelled) {
} else {
}
[controller dismissViewControllerAnimated:YES completion:Nil];
};
controller.completionHandler = myBlock;
[controller setTitle:title];
[controller setInitialText:description];
[controller addURL:[NSURL URLWithString:#"http://example.com"]];
[controller addImage:[UIImage imageNamed:#"sampleImage.png"]];
[vc presentViewController:controller animated:YES completion:^{
}];
success = YES;
}
return success;
}
Remember to add two frameworks:
Social.framework
Accounts.framework
I have a problem and can't know the reason or solution
I have AddDelegate and another ViewController, in this ViewController I add FBFriendPickerViewController, and ViewController will be added to AppDelegate ..
I add ViewController to AppDelegate like this: ( this ViewController is SocialSharing class )
if( socialSharing == nil )
socialSharing = [[SocialSharing alloc] init];
[_window.rootViewController.view addSubview:socialSharing.view];
And add FBFriendPickerViewController in ViewController(SocialSharing) like this:
-(void)shareOnFaceBook:(NSNotification *) notification
{
if( shareOnUserOrFriendWallBtnIndex == 0 )
{
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *facebookSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
NSURL *candidateURL = [NSURL URLWithString:contentDataToShare];
if (candidateURL && candidateURL.scheme && candidateURL.host) {
[facebookSheet setInitialText:#"See this link .."];
[facebookSheet addURL:candidateURL];
}
else // Event ***
{
[facebookSheet setInitialText:[NSString stringWithFormat:#"%#%#%#",[[appDelegate.extrasOverlayView.markerData.btnsArr objectAtIndex:0] objectForKey:#"markerName"],#" .. \n", contentDataToShare]];
[facebookSheet addURL:[NSURL URLWithString:lastVideoLink]];
}
//Brings up the little share card with the test we have pre defind.
[appDelegate.window.rootViewController presentViewController:facebookSheet animated:YES completion:nil];
} else {
//This alreat tells the user that they can't use built in socal interegration and why they can't.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Sorry" message:#"You can't send a tweet right now, make sure you have at least one Facebook account setup and your device is using iOS." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
}
else if( shareOnUserOrFriendWallBtnIndex == 1 )// post on friend's wall
{
if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"fb://"]] )
{
NSLog(#"Facebook is inastalled on this device.");
if( friendPickerController == nil )
{
friendPickerController = [[FBFriendPickerViewController alloc] init];
friendPickerController.delegate = self;
if( !( [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone ) )
friendPickerController.title = #"Select Friends";
}
[friendPickerController loadData];
// Present view controller modally
[self presentViewController:friendPickerController animated:YES completion:nil];
}
else{
NSLog(#"This device has no Facebook app.");
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Sorry" message:#"You can't invite friends, Facebook app isn't installed, please install it and retry again." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
}
}
and dismiss friendPickerController from SocialSharing like:
- (void)facebookViewControllerCancelWasPressed:(id)sender
{
NSLog(#"Friend selection cancelled.");
[self dismissViewControllerAnimated:YES completion:nil];
}
I used:
[friendPickerController dismissModalViewControllerAnimated:YES];
OR //[self.view.window.rootViewController dismissViewControllerAnimated:YES completion:Nil];
OR //[self dismissModalViewControllerAnimated:YES];
It works good for only first time, once, friendPickerController appears and dismiss when close, BUT when call to open again, I have this error:
"Thread 1: signal SIGBRT"
FOR the line of :
[self presentViewController:friendPickerController animated:YES completion:nil];
Note: when I add friendPickerController on AppDelegate directly (in class of AppDelegate) without another class (here is SocialSharing ViewController), it works great ..
I'm working on IOS 7, and Facebook SDK 3.13..
So, What is the problem, How can I solve it ?
Thank you
What I did to solve my problem, I create friendPickerController in NORMAL CLASS , NOT ViewController, and add it on AppDelegate
[appDelegate.window.rootViewController presentViewController:friendPickerController animated:YES completion:nil];
and dismiss by:
[appDelegate.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
And call this Class like:
if( socialSharing == nil )
{
socialSharing = [[SocialSharingClass alloc] init];
[socialSharing initSocialSharing];
}
Still, I don't know the problem, but that solved it. May that help anyone.
Hi I have two UIButtons in an iOS app. One is to post to twitter the second is to post to Facebook. The facebook button works perfectly however the tweet is casing me some problems, the tweet sheet will open with the populated text, however it takes two taps of the cancel button to dismiss. If I tap on send the tweet will be sent and the sheet dismissed but my app freezes and becomes unresponsive. I have included both bits of code
- (IBAction)postTweet:(id)sender {
// if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]){
myTweet = [[SLComposeViewController alloc]init];
myTweet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
NSString *tweetString = [[NSString alloc]initWithFormat:#"%#\n%#\nvia #ValuatorApp", pdOne.text, pdTwo.text];
[myTweet setInitialText:tweetString];
[myTweet addURL:[NSURL URLWithString:#"http://sjb007.me/TheValuator"]];
[self presentViewController:myTweet animated:YES completion:nil];
// }
[myTweet setCompletionHandler:^(SLComposeViewControllerResult result) {
NSString *output = [[NSString alloc]init];
switch (result) {
case SLComposeViewControllerResultCancelled:
output = #"Twitter Post Cancelled";
break;
case SLComposeViewControllerResultDone:
output = #"Twitter post Succesful";
break;
default:
break;
}
NSLog(#"%#",output);
}];
}
- (IBAction)postFacebook:(id)sender {
// if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]){
myTweet = [[SLComposeViewController alloc]init];
myTweet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
if (pd3 != 0) {
NSString *facebookString = [[NSString alloc]initWithFormat:#"%#\n%#\n%#", pdOne.text,pdTwo.text, pdThree.text];
[myTweet setInitialText:facebookString];
}
else if (pd3 == 0){
NSString *facebookString = [[NSString alloc]initWithFormat:#"%#\n%#\n", pdOne.text,pdTwo.text];
[myTweet setInitialText:facebookString];
}
// [myTweet addImage:[UIImage imageNamed:#"Photo Jun 02, 22 46 37.jpg"]];
[myTweet addURL:[NSURL URLWithString:#"http://sjb007.me/TheValuator"]];
[self presentViewController:myTweet animated:YES completion:nil];
// }
[myTweet setCompletionHandler:^(SLComposeViewControllerResult result) {
NSString *output = [[NSString alloc]init];
switch (result) {
case SLComposeViewControllerResultCancelled:
output = #"Facebook Post Cancelled";
break;
case SLComposeViewControllerResultDone:
output = #"Facebook post Succesful";
break;
default:
break;
}
NSLog(#"%#",output);
}];
}
You are presenting the ViewController "myTweet"
[self presentViewController:myTweet animated:YES completion:nil];
but there is no dismiss... statement in your completionHandler
[self dismissViewControllerAnimated:YES completion:nil];