I used SLComposerController, but it is not disabling the post button when the character count exceeds 140 character . I want to post an URL ,image and initial text on Twitter via my iOS application. How to do this?
It is up to the user to set the correct length to send, but I think it won't send if it is too long, or give you a callback if it didn't work.
Here is what I did:
-(void) postOnTwitterInsideView:(UIViewController *)viewController
message:(NSString *)postMessage{
if( [SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter] ){
SLComposeViewController *twitterPost = [SLComposeViewController
composeViewControllerForServiceType:SLServiceTypeTwitter];
[twitterPost setCompletionHandler:^(SLComposeViewControllerResult result){
switch (result) {
case SLComposeViewControllerResultDone:
// tell user message was sent
break;
case SLComposeViewControllerResultCancelled:
// tell user message was not sent
break;
default:
break;
}
}];
[twitterPost addImage:self.imagePost];
[twitterPost setInitialText:postMessage];
[viewController presentViewController:twitterPost
animated:YES
completion:nil];
} else {
// tell user he has to activate this service in his settings of iOS
}
}
Related
i am new to objective C and im trying to make a share button for twitter in my game. Im using spritebuilder to create all my buttons and sprites. All of my code lies in MainScene.m while my code for twitter share sheet is in ShareViewController.m
In my MainScene.m file i have a method sendToController, which using an object of shareViewController calls a method postToTwitter which is in ShareViewController.m. Due to some reason this tweetsheet is not visbile and from what i gather is that there is a window heirachy problem which i am not able to solve.
Here is the code in MainScene.m
ShareViewController *_shareViewController;
#synthesize shareViewController;
(void)sendToController {
_shareViewController = [ShareViewController alloc];
[_shareViewController postToTwitter];
}
Here is the code for ShareViewController.m
#implementation ShareViewController {
MainScene *_mainSceneObj;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_mainSceneObj.ShareViewController = self;
}
- (void)postToTwitter {
NSLog(#"in postToTwitter %d", [SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]);
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController
composeViewControllerForServiceType:SLServiceTypeTwitter];
// Sets the completion handler. Note that we don't know which thread the
// block will be called on, so we need to ensure that any required UI
// updates occur on the main queue
tweetSheet.completionHandler = ^(SLComposeViewControllerResult result) {
switch(result) {
// This means the user cancelled without sending the Tweet
case SLComposeViewControllerResultCancelled:
break;
// This means the user hit 'Send'
case SLComposeViewControllerResultDone:
break;
}
};
// Set the initial body of the Tweet
[tweetSheet setInitialText:#"just setting up my twttr"];
if (![tweetSheet addImage:[UIImage imageNamed:#"screen.png"]]) {
NSLog(#"Unable to add the image!");
}
if (![tweetSheet addURL:[NSURL URLWithString:#"http://twitter.com/"]]){
NSLog(#"Unable to add the URL!");
}
// Presents the Tweet Sheet to the user
[self presentViewController:tweetSheet animated:NO completion:^{
NSLog(#"Tweet sheet has been presented.");
}]; }
}
I am using SLComposeViewController to send a tweet. I have all the necessary permissions from Twitter to send a tweet, however, when I present the tweet sheet it never shows. From debugging my code I can see that the SLComposeViewController's completion handler is automatically called at SLComposeViewControllerResultCancelled is the result.
Has anyone come across this before? As I said, I have all necessary permissions and my Twitter account and password are set up correctly in my iPhone settings.
EDIT
This only happens sometimes. Majority of the time I have no problem with the Tweet sheet, so the code does work, but for some users they are experiencing this.
thanks
My code here:
- (void)presentTweetSheet {
SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
tweetSheet.completionHandler = ^(SLComposeViewControllerResult result) {
switch (result) {
case SLComposeViewControllerResultCancelled:
SWLog(#"Tweet Cancelled");
break;
case SLComposeViewControllerResultDone:
SWLog(#"Tweet sent");
break;
default:
break;
}
};
UIWindow *window = [UIApplication sharedApplication].keyWindow;
ECSlidingViewController *slidingVC = (ECSlidingViewController *)window.rootViewController;
[slidingVC.topViewController presentViewController:tweetSheet animated:YES completion:^{
SWLog(#"Tweet sheet displayed");
[self removeActivityIndicatorFromCell];
}];
}
I checked multiple apps with the same code written in them, non of them worked. i tried sorting it out by code but it did not work. So i re-booted my iPhone and it WORKED!
Hope this helps
I have created an application that uses the SMS and after the user clicks the sms button it opens up with my number already in and no message (thats their job). But when it loads up the sms message page it set the curser thing is up where the recipients are not where the message is. To explain that better, after the sms loads if they were to just start typing they would be adding another person to send the message to, not typing the message. For example if I would like to load up to a specific row on my UIPicker on startup I would:
[picker selectRow:3 inComponent:0 animated:NO];
sms load up:
- (IBAction)sms {
MFMessageComposeViewController *textComposer = [[MFMessageComposeViewController alloc] init];
[textComposer setMessageComposeDelegate:self];
if ([MFMessageComposeViewController canSendText]) {
[textComposer setRecipients:[NSArray arrayWithObjects: #"support#nicmacengineering.com", nil]];
[textComposer setBody:#""];
[self presentViewController:textComposer animated:YES completion:NULL];
} else {
NSLog(#"Can't Open Text");
}
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
switch (result) {
case MessageComposeResultCancelled:
NSLog(#"Cancelled");
break;
case MessageComposeResultFailed:
break;
case MessageComposeResultSent:
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:NULL];
}
First, your question is fairly hard to understand. Here's my impression of it:
Can I set the cursor to the message field by default in an SMS controller?
And the answer is:
No.
Since it is an Apple framework, with no public method to switch fields (here's the class reference).
Because of this, you won't be able to automatically set the position of the cursor.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What are the steps I need to follow to use iOS 6's new SLComposeViewController to post to Facebook, Twitter or Sina Weibo?
For details on this framework please see Apple's Social Framework Class Reference
Additional tutorials:
http://soulwithmobiletechnology.blogspot.com/2012/07/tutorial-how-to-use-inbuilt.html
http://www.mobile.safilsunny.com/iphone/integrating-facebook-ios/
https://rudeboy-quickies.blogspot.com/2012/06/steps-to-integrate-facebook-in-ios6.html
For this example, we will be using the SLComposeViewController's SLServiceTypeFacebook. If you wish to use Twitter or SinaWeibo just change out the SLServiceType to one of the following:
SLServiceTypeFacebook
SLServiceTypeSinaWeibo
SLServiceTypeTwitter
iOS 6 has made it very easy to post directly to Facebook, Twitter or Sina Weibo using the SLComposeViewController. This works very similarly to iOS 5's TWTweetComposeViewController.
First, in your view controller's header file (.h) #import the Social Framework and the Accounts Framework.
#import <Social/Social.h>
#import <Accounts/Accounts.h>
Here we will declare a simple UIButton and an IBAction that we will later link to that button and a void (sharingStatus) which will be used to check that the selected sharing service is available.
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIButton *easyFacebookButton;
- (IBAction)facebookPost:(id)sender;
- (void)sharingStatus;
#end
#implementation ViewController
Then, in your implementation file (.m), we'll start by implementing the (sharingStatus) void that we declared in the header file. sharingStatus uses SLComposeViewController's isAvailableForServiceType BOOL to return whether or not you can post to the service specified in its argument. In this case, we will use the service type SLServiceTypeFacebook. If the service is available the post button will be enabled with an alpha value of 1.0f, and if the service isn't available the button will be disabled its alpha value set to 0.5f.
- (void)sharingStatus {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
NSLog(#"service available");
self.easyFacebookButton.enabled = YES;
self.easyFacebookButton.alpha = 1.0f;
} else {
self.easyFacebookButton.enabled = NO;
self.easyFacebookButton.alpha = 0.5f;
}
}
Here we will set up the IBAction that will call up the composer. For good practice, we will check isAvailableForServiceType again to avoid calling up the composer for a service type that isn't available. (Incase something went wrong during the last check, or if availability somehow changed in the fraction of a second in between tapping the post button and the composers all/init. The code below has been set up to display a Facebook composers sheet with text, an image, and a link. This action also utilises a completion handler for the composer's cancelled and done results.
- (IBAction)facebookPost:(id)sender {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[mySLComposerSheet setInitialText:#"iOS 6 Social Framework test!"];
[mySLComposerSheet addImage:[UIImage imageNamed:#"myImage.png"]];
[mySLComposerSheet addURL:[NSURL URLWithString:#"http://stackoverflow.com/questions/12503287/tutorial-for-slcomposeviewcontroller-sharing"]];
[mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
switch (result) {
case SLComposeViewControllerResultCancelled:
NSLog(#"Post Canceled");
break;
case SLComposeViewControllerResultDone:
NSLog(#"Post Sucessful");
break;
default:
break;
}
}];
[self presentViewController:mySLComposerSheet animated:YES completion:nil];
}
}
In viewWillAppear we will register an observer to ACAccountStoreDidChangeNotification so the application can be notified when account information changes. This observer will then be removed in viewDidDisappear.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(sharingStatus) name:ACAccountStoreDidChangeNotification object:nil];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:ACAccountStoreDidChangeNotification];
}
And finally, open up interface builder and add a UIButton which will be the post button. Then in the connections inspector link the IBOutlet and IBAction we created earlier to the button, and that's it! You're done!
Just use this code to share on Facebook.
SLComposeViewController *controllerSLC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[controllerSLC setInitialText:#"First post from my iPhone app"];
[controllerSLC addURL:[NSURL URLWithString:#"http://www.appcoda.com"]];
[controllerSLC addImage:[UIImage imageNamed:#"test.jpg"]];
[self presentViewController:controllerSLC animated:YES completion:Nil];
If you want this for Twitter then just change SLServiceTypeTwitter.
Safe Use of SLComposeViewController
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
SLComposeViewController *fbPost = [SLComposeViewController
composeViewControllerForServiceType: SLServiceTypeFacebook];
[fbPost setInitialText:#"Text You want to Share"];
[fbPost addImage:[UIImage imageNamed:#"shareImage.png"]];
[self presentViewController:fbPost animated:YES completion:nil];
[fbPost setCompletionHandler:^(SLComposeViewControllerResult result) {
switch (result) {
case SLComposeViewControllerResultCancelled:
NSLog(#"Post Canceled");
break;
case SLComposeViewControllerResultDone:
NSLog(#"Post Sucessful");
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}];
}
I am writing an app that involves sending emails, tweets, and Facebook messages (in that order) from a user's phone. The problem is that if the user multitasks and then returns to the app in the middle of editing a tweet sheet, when he/she returns, the tweet sheet cancels.
I am using this code to present the tweet sheet (in case it helps):
- (void)sendTweet {
TWTweetComposeViewControllerCompletionHandler
completionHandler =
^(TWTweetComposeViewControllerResult result) {
switch (result)
{
case TWTweetComposeViewControllerResultCancelled:
NSLog(#"Twitter Result: canceled");
break;
case TWTweetComposeViewControllerResultDone:
NSLog(#"Twitter Result: sent");
break;
default:
NSLog(#"Twitter Result: default");
break;
}
[rootViewController dismissModalViewControllerAnimated:YES];
};
[tweetViewController setCompletionHandler:completionHandler];
// ...Set up the tweet sheet with text and image...
[rootViewController presentModalViewController:tweetViewController animated:YES];
}
Is there any way to prevent the tweet sheet from canceling after the user switches apps? Any advice would be much appreciated!