Twitter in iOS 5 leaking memory with ARC - iphone

Here is my code:
case kTweetIndex:
{
TWTweetComposeViewController *tweetSheet =
[[TWTweetComposeViewController alloc] init];
[tweetSheet setInitialText:[NSString stringWithFormat:#"%#",self.item.link]];
[self presentModalViewController:tweetSheet animated:YES];
if (![TWTweetComposeViewController canSendTweet])
{
NSLog(#"Can't Send tweet");
}
}
This is just a massive amount of leak caused by twitter, and i don't know how to handle it. Thanks!

Make Property of TWTweetComposeViewController or twitter class or you can one thing more alloc it in AppDelegate Class. It will work.

Related

Why is there no text being shown while tweeting?

The initial text isn't being shown, did I type something wrong or missing something? I followed a tutorial exactly and no beans.
if ([TWTweetComposeViewController canSendTweet]) {
TWTweetComposeViewController *tweetComposer=[[TWTweetComposeViewController alloc]init];
[self presentModalViewController:tweetComposer animated:YES];
[tweetComposer setInitialText:#"#VOX"];
}
I am trying to get this to run on ios5.1
Looks like you might have to call setInitialText before you present the TWTweetComposeViewController. so you would have:
if ([TWTweetComposeViewController canSendTweet]) {
TWTweetComposeViewController *tweetComposer=[[TWTweetComposeViewController alloc]init];
[tweetComposer setInitialText:#"#VOX"];
[self presentModalViewController:tweetComposer animated:YES];
}
According to the docs for TWTweetComposeViewController setInitialText: Will return a NO if text does not fit in the currently available character space or the view was presented to the user.

Attach a picture to twitter post

How can I attach a picture to a twitter post like the iPhone built in photo app does?
If any body has some samplecode that will be a great help.
Thanks.
The other answers are suggesting TWTweetComposeViewController, however you should if you can avoid using this class, it's now deprecated in iOS 6,
Please see here: TWTweetComposeViewController deprecated in IOS6
And from Apple themselves, WWDC 2012, session 306 presentation PDF:
Twitter Framework
• Twitter framework is deprecated
• Do not use TWTweetComposeViewController
To use Twitter now you should use the SLComposeViewController class of the Social framework, it's usage is almost identical to TWTweetComposeViewController.
You may need to support iOS 5, in which case you have no other option then to use the TWTweetComposeViewController class, but you should make the effort to check for SLComposeViewController and use that if it's available, simply because this will save you time and effort in the near future when support for iOS 5 is dropped, the TWTweetComposeViewController class really may be gone. If you rely on the Twitter framework now for simplicity as it does work on iOS 5 and 6, you're being short sighted and you will have problems sometime later, it's only a few more lines to do this and it will mean you won't need to worry about future iOS SDK releases.
You should import Twitter.framework and Social.framework, mark them both as optional imports (not required).
Example code:
UIImage *myImage = [...]; // an image
if( NSClassFromString(#"SLComposeViewController") ){
// We have the Social framework in our iOS system
// iOS 6 and later will use this
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]){
SLComposeViewController *twitterCompose = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[twitterCompose addImage:myImage]; // Adding your UIImage
twitterCompose.completionHandler = ^(SLComposeViewControllerResult result){
// Handle result, dismiss view controller
[self dismissViewControllerAnimated:YES
completion:nil];
};
[self presentViewController:twitterCompose
animated:YES
completion:nil];
}else{
// the user does not have Twitter set up
}
}else if( NSClassFromString(#"TWTweetComposeViewController") ){
// We don't have the Social framework, work with the Twitter framework
// iOS 5 only will use this
if( [TWTweetComposeViewController canSendTweet] ){
TWTweetComposeViewController *twitterCompose = [[TWTweetComposeViewController alloc] init];
[twitterCompose addImage:myImage];
twitterCompose.completionHandler = ^(TWTweetComposeViewControllerResult result){
// Handle result, dismiss view controller
[self dismissViewControllerAnimated:YES
completion:nil];
};
[self presentViewController:twitterCompose
animated:YES
completion:nil];
}else{
// the user hasn't go Twitter set up on their device.
}
}else{
// Wow you're going retro with this app,
// you must be on iOS 4 if you ever get here...
}
Here how i use it:
NSLog(#"Ready to Tweet.");
TWTweetComposeViewController *tweetComposer = [[TWTweetComposeViewController alloc] init];
[tweetComposer setInitialText:[NSString stringWithFormat:#"My message"]];
[tweetComposer addImage:[UIImage imageNamed:#"114x114"]];
[tweetComposer addURL:[NSURL URLWithString:#"http://myPage"]];
tweetComposer.completionHandler = ^(TWTweetComposeViewControllerResult result){
if(result == TWTweetComposeViewControllerResultDone){
NSLog(#"Tweeted.");
} else if(result == TWTweetComposeViewControllerResultCancelled) {
NSLog(#"Cancelled.");
}
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
};
[self presentModalViewController:tweetComposer animated:YES];
if you are using ios 5.0 then you can directly post image like
Add Framwork twitter.framework
import Twitter/TWTweetComposeViewController.h
-(void)postToTwittert
{
Class TWTweetComposeViewControllerClass = NSClassFromString(#"TWTweetComposeViewController");
if (TWTweetComposeViewControllerClass != nil) {
if([TWTweetComposeViewControllerClass respondsToSelector:#selector(canSendTweet)]) {
TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];
[twitter setInitialText:#"text"];
[twitter addImage:[UIImage imageNamed:#"imagename"]];
[twitter addURL:[NSURL URLWithString:#"http://www.google.com"]];
[self presentViewController:twitter animated:YES completion:nil];
twitter.completionHandler = ^(TWTweetComposeViewControllerResult res) {
if(res == TWTweetComposeViewControllerResultDone)
{
NSLog(#"success for twitter post");
}
else if(res == TWTweetComposeViewControllerResultCancelled)
{
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:#"Canceled" message:#"Your Tweet was not posted" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
[self dismissModalViewControllerAnimated:YES];
};
}
}
}
call this function where you wants twitter post
and do appropriate changes that you want..
Best luck..
I just use UIActivityViewController to post to Twitter now.
UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:#[#"Default Text", [UIImage imageNamed:#"imageName"]] applicationActivities:nil];
[self presentViewController:controller animated:YES completion:nil];
This will present a controller where the user can decide what to do (Post to Twitter, Post to Facebook, etc...)
It then uses the system tweet sheet etc... to do it.
You don't have to provide the default text. This can be overwritten anyway.
Oh, also, no frameworks required for this.

TWTeetComposeViewController execute custom code after posting a new tweet

So, using the new Twitter framework in iOS5, we can simply post Twitter updates with this simple code:
if ([TWTweetComposeViewController canSendTweet]) {
TWTweetComposeViewController *tweetSheet = [[TWTweetComposeViewController alloc] init];
[tweetSheet setInitialText:#"Tweeting from Steamerduck :)"];
[controller presentModalViewController:tweetSheet animated:YES];
}
However, I would like to execute some custom code after a user clicks on send here:
Is it possible to execute some custom code, some of my own methods after the TWTweetComposeViewController sends a tweet?
You can specify a completion handler that gets called after the user is done composing the tweet. It would look something like this (I haven't had a chance to test this on a device yet):
if ([TWTweetComposeViewController canSendTweet]) {
TWTweetComposeViewController *tweetSheet = [[TWTweetComposeViewController alloc] init];
[tweetSheet setInitialText:#"Tweeting from Steamerduck :)"];
// Set a completion handler to be called when the user is done
// composing their tweet
[tweetSheet setCompletionHandler:^(TWTweetComposeViewControllerResult result) {
if (result == TWTweetComposeViewControllerResultDone) {
// Tweet was sent, do something
} else if (result == TWTweetComposeViewControllerResultCancelled) {
// Do something else if the user cancelled
[controller dismissModalViewControllerAnimated:YES];
}
}];
[controller presentModalViewController:tweetSheet animated:YES];
}

App crashes on ios 4.X for framework Twitter.framework

I am using twitter using Sharekit and for IOS5, I am using its class TWTweetComposeViewControllerClass as,
Class TWTweetComposeViewControllerClass = NSClassFromString(#"TWTweetComposeViewController");
if (TWTweetComposeViewControllerClass != nil) {
if([TWTweetComposeViewControllerClass respondsToSelector:#selector(canSendTweet)]) {
UIViewController *twitterViewController = [[TWTweetComposeViewControllerClass alloc] init];
[twitterViewController performSelector:#selector(setInitialText:)
withObject:NSLocalizedString(#"TwitterMessage", #"")];
[self.navigationController presentModalViewController:twitterViewController animated:YES];
[twitterViewController release];
}
} else {
[SHK flushOfflineQueue];
SHKItem *item = [SHKItem text:text];
//[SHKTwitter shareItem:item];
SHKActionSheet *actionsheet = [SHKActionSheet actionSheetForItem:item];
[actionsheet showFromToolbar:self.navigationController.toolbar];
}
It is working fine with simulator 5.0 but crashes on 4.3 with below error.
dyld: Library not loaded: /System/Library/Frameworks/Twitter.framework/Twitter
Referenced from: /Users/indianic/Library/Application Support/iPhone Simulator/4.3.2/Applications/241167D0-62E0-4475-85FD-0B8253B4E456/demoFBTW.app/demoFBTW
Reason: image not found
How do I fix this.
I tried to change the dependency for the framework from Required to Optional but didn't find for that
It looks like you found how to weak link the framework. Assuming you are using Xcode 4.2 and LLVM3, you can also simplify this code a bit and fix a bug you have while you're at it:
#include <Twitter/Twitter.h>
// This line is no longer needed:
// Class TWTweetComposeViewControllerClass = NSClassFromString(#"TWTweetComposeViewController");
// this part can now be:
if ([TWTweetComposeViewController class] != nil) { // no need to look up class by string now
// note previously you were checking if this class responds to 'canSendTweet'
// but you never called the method to see if you can actually send the tweet
if([TWTweetComposeViewController canSendTweet]) {
// you can type this correctly now...
TWTweetComposeViewController *twitterViewController = [[TWTweetComposeViewController alloc] init];
// ... and call this method directly
[twitterViewController setInitialText:NSLocalizedString(#"TwitterMessage", #"")];
[self presentModalViewController:twitterViewController animated:YES];
[twitterViewController release];
}
}
... continue with the shareKit option

Send SMS iPhone

I was trying to send SMS from within my app. I wrote this piece of code but it seems not to work.
No crash, no error log, simply nothing happen (of course I tried to log canSendText and the result is 1).
- (void)viewDidLoad
{
[super viewDidLoad];
messageComposer = [[MFMessageComposeViewController alloc] init];
if ([MFMessageComposeViewController canSendText]) {
[messageComposer setBody:#"Messaggio generato da SMSTest"];
[messageComposer setRecipients:[NSArray arrayWithObject:#"3333333333"]];
[messageComposer setDelegate:self];
[self presentModalViewController:messageComposer animated:YES];
}
}
Can anyone explain me what I'm doing wrong?
The problem is that presentModalViewController does not work in viewDidLoad yet as the view is loaded but might not even be on screen yet. If you put your code in viewWillAppear:animated, this should work.
Edit: As per Saphrosit's comment: viewDidAppear: is an even better place to do this.
I use this successfully:
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
controller.messageComposeDelegate = self;
controller.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentModalViewController:controller animated:YES];
Note that it's messageComposeDelegate, not 'delegate' as you do.
Tim
When i try the code on simulator i get an UIAlert saying text messaging is not available, because simulator can´t send messages. Have you checked that your header file is a delegate of MFMessageComposeViewControllerDelegate ?
YourClassName : UIViewController <MFMessageComposeViewControllerDelegate>
//try this ... it will run ..
MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText])
{
controller.body = #"Hello from Kartik";
controller.recipients = [NSArray arrayWithObjects:#"12356478", nil];
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
}