Using Twitter integration (Tweet Sheet) in iOS Simulator - ios5

When I use TWTweetComposeViewController addImage: method to tweet with a image, it always show "Cannot Send Tweet" alert (like following image). But same tweet can be sent on the actual iOS device.
Anyone know what happened?
Code:
TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];
[tweetViewController setInitialText:#"sent from MyTwitterApp"];
[tweetViewController addImage:imageToSave];
[self presentModalViewController:tweetViewController animated:YES];
*imageToSave is an UIImage object selected from photo library

Related

How to share app

I'm developing an app. I'm going to publish to an app store. But client needs to share this app with friends. I have a button to share my app. How to share the app? I read some doc they mentioned, use [[UIApplication SharedApplication] openURL:#" url"]. I don't have my app URL. Because I didn't submit the app. Is it possible to share my app?
If you want to know what the URL of your application will be before it has been submitted/approved, Apple provides a shorthand URL you can use, of the form:
http://itunes.com/apps/appname
For example:
http://itunes.com/apps/AngryBirds
If you want the specific URL with the application ID, you could submit v1.0.0 without the "Share" link, then immediately submit a 1.0.1 update with the link included.
Url can be generated via app id. Its the best way to do it coz if app name changes, url could change. But app id wont change.
You can get your app id on itunesConnect.apple.com or via itunes.
//https://itunes.apple.com/app/id_hereisyourappID
Let see how to do the sharing.
I will show you 4 ways to share your link. Via Facebook, Twitter, Mail and SMS.
For Facebook and Twitter.
Add Accounts and Social framework. And in your header import.
#import <Social/Social.h>
#import <Accounts/Accounts.h>
when you want to share link use the following code
SLComposeViewController *mySLComposerSheet = [[SLComposeViewController alloc] init];
mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];//for twitter use SLServiceTypeTwitter
[mySLComposerSheet addURL:[NSURL URLWithString:#"https://itunes.apple.com/app/id123456789"]];
[self presentViewController:mySLComposerSheet animated:YES completion:nil];
Lets do it with mail add MessageUI framework and import
#import <MessageUI/MessageUI.h>
And the delegate MFMailComposeViewControllerDelegate
in your code
MFMailComposeViewController* mailComposer = [[MFMailComposeViewController alloc] init];
[mailComposer setMailComposeDelegate:self];
[mailComposer setModalPresentationStyle:UIModalPresentationFormSheet];
[mailComposer setMessageBody:#"https://itunes.apple.com/app/id123456789" isHTML:NO];
[self presentViewController:mailComposer animated:YES completion:nil];
And add delegate method
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[controller dismissViewControllerAnimated:YES completion:nil];
}
You can create html content for mail body.
Lets share it via SMS
Add delegate MFMessageComposeViewControllerDelegate
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
if([MFMessageComposeViewController canSendText])
{
controller.body = #"https://itunes.apple.com/app/id123456789";
controller.messageComposeDelegate = self;
[self presentViewController:controller animated:YES completion:nil];
}
And add delegate method.
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[controller dismissViewControllerAnimated:YES completion:nil];
}
I hope this helps.

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.

Starting an iPhone app in camera mode shows just a blank screen

I'm creating an app for the iPhone which is supposed to start in camera mode (with a custom layout on top but that comes later). I've already created a version of this app where I press a button, that allows me to either choose a photo from the iPhone album or take a new photo.
Trying to use the same code in a different fails.
Originally I had a function 'takePicture' which I used to start the camera
- (void)takePicture
{
isInCaptureMode = YES;
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.mediaTypes =
[UIImagePickerController availableMediaTypesForSourceType:
UIImagePickerControllerSourceTypeCamera];
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
imagePicker.videoQuality = UIImagePickerControllerQualityTypeMedium;
// set delegate
[imagePicker setDelegate:self];
// Place image picker on the screen
[self presentModalViewController:imagePicker animated:YES];
[imagePicker release];
}
So I thought to myself "I can probably create a single view app, throw this code into the view controller's viewDidLoad function and use that as a starting point". But alas, I've been stuck there. When I do that the application starts and (if the device has a camera) the screen goes blackish blank, if the device does not have a camera (iPad or the simulator) the app crashes.
I originally created the app using Xcode 3 but have now migrated over to 4.2 the original app runs fine in 4.2 though (after I did a little bit of tweaking)
Can anyone explain why this happens?
can you try the following code to see what happen?
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
} else {
//you can get picture from Library is there is no camera
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
picker.delegate = self;
[self presentModalViewController:picker animated:YES];
[picker release];
So, like I said in the comment. As soon as I called the code from 'viewDidAppear' instead of 'viewDidLoad' it finally ran...

iPhone: my Video Recorder API code doesn't work

I am trying to implement Video recording functionality for my iPhone 3.0 and iPhone 3.1.2 device as well. I created a sample app for that and kept a button called "Start Video" in a UIView. Then i call the below function code when click Start Video button clicked.
-(IBAction) StartVideo
{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.allowsEditing = YES;
//imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
imagePickerController.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeMovie];
imagePickerController.delegate = self;
[self presentModalViewController:imagePickerController animated:YES];
[imagePickerController release];
}
UPDATE: When i check the below code snippet to find video is supported or not, it returns me as Video not support alert as mentioned below. I don't understand what is wrong here?
NSArray *media = [UIImagePickerController availableMediaTypesForSourceType:
UIImagePickerControllerSourceTypeCamera];
if([media containsObject:(id)kUTTypeMovie])
{
imagePickerController.mediaTypes = [NSArray
arrayWithObjects:(id)kUTTypeMovie,nil];
[self presentModalViewController:imagePickerController animated:YES];
} else {
NSLog(#"Video not supported");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Nothing"
message:#"Video not supported"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"Ok", nil];
[alert show];
[alert release];
}
I tested this code on 3.0 2G and 3.1.2 3G as well, but it is crashing when i click on "Start Video" and going to device home screen. I downloaded some third party free video recording app called "Record Video", it works awesome, does video recording and share files very well.
I want to know how to implement recording video on iPhone 3.0, 3.1.2 and 4.0 devices.
NOTE: I am testing on 2G and 3G devices only.
Could somenone guide me?
Thank you.
UPDATED:
I tested the code (function: StartVideo) with iPhone 3GS device and it's working as expected. I don't know why does it say as "video not supported" on 2G and 3G phone's.
are you setting a delegate to handle returns by the UIImagePickerController ? you need to set a delegate (possibly the view controller you are presenting the picker from) that implements the UIImagePickerControllerDelegate protocol

MFMessageComposeViewController alloc returns nil

In my application, MFMailComposeViewController works fine but creating a new instance of MFMessageComposeViewController fails.
Here is the code for both:
-( IBAction)sendSMS: (id)sender
{
MFMessageComposeViewController *picker = [[[MFMessageComposeViewController alloc] init] autorelease];
picker.messageComposeDelegate = self;
NSArray *toRecipients = [NSArray arrayWithObject: cell.currentTitle ];
picker.recipients = toRecipients;
[self presentModalViewController:picker animated:YES];
}
-( IBAction)sendEmail: (id)sender
{
MFMailComposeViewController *picker = [[[MFMailComposeViewController alloc] init] autorelease];
picker.mailComposeDelegate = self;
NSArray *toRecipients = [NSArray arrayWithObject: email.currentTitle ];
[picker setToRecipients:toRecipients];
[self presentModalViewController:picker animated:YES];
}
Its seemingly obvious that everything is linking correctly because the email view controller works fine. Is there something I am missing maybe configuration wise?
Have you checked +[MFMessageComposeViewController canSendText]?
From the MFMessageComposeViewController Class Reference,
Before presenting a message composition view, call the canSendText class method to ensure that the user’s device is appropriately configured. Do not attempt to present a message composition view if the canSendText method returns NO. If SMS delivery isn’t available, you can notify the user or simply disable the SMS features in your application.
Starting in iOS 5, you can register to be notified of changes to the availability of text message sending by way of the MFMessageComposeViewControllerTextMessageAvailabilityDidChangeNotification notification.
Reasons it might be returning nil:
Device isn't running iOS 4.
Device is an iPod Touch/iPad without iMessage enabled.
No SIM card? (The view now shows in iOS 6; the app is not notified of the message send failure.)
"Device" is actually the simulator. (Perhaps this works in iOS 6 too.)
Similarly, [[MFMailComposeViewController alloc] init] returns nil when no mail accounts are enabled (you can quickly test this by disabling accounts in Settings), but also shows a "No mail accounts configured" alert for you. MFMessageComposeViewController does not do this.