Is automated sharing possible for Twitter in the background on iOS? - iphone

I wanted to share a Twitter feed on the Twitter wall which contains an image and some text. I want support from iOS 4.3 to iOS 6.0.1. Is sharing possible in the background without a send/share button? How do I implement it?

The API call that you need to send is:
https://dev.twitter.com/docs/api/1.1/post/statuses/update_with_media
Before making that call, of course, you will need to authenticate with Twitter via xAuth/OAuth. Unless you get special permission from Twitter to do otherwise, it looks like you will need to use OAuth,
https://dev.twitter.com/docs/oauth/xauth
To background the request, it will likely make sense to use Grand Central Dispatch --that is unless you have a lot of different Twitter requests to send. In that case, I would instead opt for an NSOperationQueue where maxConcurrentOperationCount = 1. See the following:
http://www.fieryrobot.com/blog/2010/06/27/a-simple-job-queue-with-grand-central-dispatch/
http://www.raywenderlich.com/19788/how-to-use-nsoperations-and-nsoperationqueues
Nevertheless, because OAuth is such a pain, it will likely make sense to use a third party library. I've never used it before, but here is an example using MGTwitterEngine:
Twitter's statuses/update_with_media on iOS returns 500 error
If you were able to limit use to iOS 5+, then I would highly recommend using the SLRequest object. The advantage of this approach is that you integrate with the iOS users account directly, so they don't have to authenticate through a UIWebView or something cheesy.
To do so, you would simply plug in the appropriate Twitter API url in the following function requestForServiceType:requestMethod:URL:parameters: and obtain your SLRequest object. Then assign the appropriate Twitter ACAccount obtained from the ACAccountStore using requestAccessToAccountsWithType:options:completion:. Finally make your call to performRequestWithHandler, which would then perform your request asynchronously.

The following code will not post in background but it can post across ios versions...
You can use condition for ios versions like below code.This is working code I have implemented and it is working on both ios 5 and 6. Please check in ios 4 to confirm.I think it should work.
#import "Twitter/Twitter.h"
#import "Social/Social.h"
-(IBAction)tweetPost:(id)sender
{
if ([self isSocialAvailable])
{
SLComposeViewController *tweetComposer=[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
SLComposeViewControllerCompletionHandler __block completionHandler=
^(SLComposeViewControllerResult result){
[tweetComposer 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;
}};
NSString*message = #"posting to twitter test ios 6";
[tweetComposer setInitialText:message];
[tweetComposer addImage:[UIImage imageNamed:#"2.jpg"]];
[tweetComposer addURL:[NSURL URLWithString:#"http://www.youtube.com/watch?v=GoZ2Be2zLq8"]];
[tweetComposer setCompletionHandler:completionHandler];
[self presentViewController:tweetComposer animated:YES completion:nil];
}
}
else
{
TWTweetComposeViewController *twitter= [[TWTweetComposeViewController alloc] init];
[twitter addImage:[UIImage imageNamed:#"2.jpg"]];
[twitter addURL:[NSURL URLWithString:#"http://www.youtube.com/watch?v=GoZ2Be2zLq8"]];
[twitter setInitialText:#"Tweet from iOS 5 app using the Twitter framework."];
[self presentModalViewController:twitter animated:YES];
twitter.completionHandler = ^(TWTweetComposeViewControllerResult result)
{
NSString *title = #"Tweet Status";
NSString *msg;
if (result == TWTweetComposeViewControllerResultCancelled)
msg = #"Tweet compostion was canceled.";
else if (result == TWTweetComposeViewControllerResultDone)
msg = #"Tweet composition completed.";
// Show alert to see how things went...
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil];
[alertView show];
};
}
}
-(BOOL)isSocialAvailable {
return NSClassFromString(#"SLComposeViewController") != nil;
}
You need to include three frameworks named social,adSupport and Accounts.Check which one not needed with twitter feed post.
Hope ,this will help you.

Yes, but you'll need find some 1.1 API wrapper (thing which generates API requests, singns them etc) for you and authoriser (MGTWitter engine works fine). I have a working solution for sharing (text only) and getting user info for iOS 4+.
And about background part - that depends on how you implement that (i.e. notifications or continious background execution or gps callbacs etc...).

Related

I want this button to show an error message if you are on iOS 5.1

I have a button that shares twitter message. The problem is social network does not work on iOS 5.1 so my question is how do I send an error message if the user is using iOS 5.1?
-(IBAction)Twitter:(id)sender{
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){
if (result == SLComposeViewControllerResultCancelled) {
NSLog(#"Cancelled");
} else
{
NSLog(#"Done");
}
[controller dismissViewControllerAnimated:YES completion:Nil];
};
controller.completionHandler =myBlock;
[controller setInitialText:#"#VOX"];
[controller addURL:[NSURL URLWithString:#""]];
[controller addImage:[UIImage imageNamed:#""]];
[self presentViewController:controller animated:YES completion:Nil];
}
else{
alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Please check your Twitter settings." delegate:self cancelButtonTitle:#"cancel" otherButtonTitles:nil ,nil];
[alert show];
}
}
This is my code.
If you are supporting iOS 5.1 as your deployment target, not allowing the user to post their tweet is a terrible user experience. Instead, your method should look something like this:
- (IBAction)sendTweetTapped:(id)sender {
if ([SLComposeViewController class]) {
// Execute your code as you have it
}
else {
// Use TWTweetComposeViewController and the Twitter framework
}
}
You'll need to weakly link the Social framework. In doing so, if the user's iOS version doesn't support the Social framework (i.e. is less than 6.0), you're basically just sending a message to nil, which is allowed. In such a case, you'd fall back to using the Twitter framework and everyone gets to happily tweet!
** NOTE: I changed the name of your method because it's terrible and doesn't describe what-so-ever what the method is supposed to do.
To solely get the system version, you can find a good answer already here: How can we programmatically detect which iOS version is device running on?
To sum it up, however, you can call:
[[[UIDevice currentDevice] systemVersion] floatValue];
Which returns the iOS version as a float value.
This, however, is a bad practice for what you need it for. It is better to check for a feature as well as checking for the current OS.To fully successfully integrate Twitter you should consider including built in Twitter functionality for iOS 5.0 as well (You will need to weakly include and #import both Twitter.framework and Social.framework):
float osv = [[[UIDevice currentDevice] systemVersion] floatValue];
if (osv >= 6.0 && [SLComposeViewController class]) { //Supports SLComposeViewController, this is preferable.
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
//Success, you can tweet! (using the class SLComposeViewController)
} else {
if ([TWTweetComposeViewController canSendTweet]) { //Perhaps redundant, but worth a try maybe?
//Success, you can tweet! (using the class TWTweetComposeViewController)
} else {
//Error Message
}
}
} else if (osv < 6.0 && osv >= 5.0 && [TWTweetComposeViewController class]) {
if ([TWTweetComposeViewController canSendTweet]) {
//Success, you can tweet! (using the class TWTweetComposeViewController)
} else {
//Error Message
}
} else {
//No internal solution exists. You will have to go with 3rd party or write your own.
}

iOS Facebook API don't bring up the iPhone settings like Tweeter

I'm using DEFacebookComposeViewController for post a message in Facebook. When using iOS5 the user is redirected to the facebook page for login and then go back to the app, that works nice. But, in iOS6 the Service API show a UIAlertView that show the options "Settings" or "Cancel"
The problem is that bot buttons have the same effect. Both cancel the dialog (it's also happend in the 'FacebookSample' here).
In the other hand, the Tweeter framework woks fine in both iOS 5 and 6. The alerts appears if there is no tweeter account configured and the "Settings" button brings you to the "Add Account" screen.
All ideas are welcome.
Thanks
PD: all test where in the simulator.
Edit:
This is the code that I'm using:
DEFacebookComposeViewController *facebookComposer = [[DEFacebookComposeViewController alloc] init];
[facebookComposer setInitialText:#"Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test"];
__block id weakSelf = self;
[facebookComposer setCompletionHandler:^(DEFacebookComposeViewControllerResult result) {
[weakSelf dismissModalViewControllerAnimated:YES];
}];
[self presentModalViewController:facebookComposer animated:YES];
[facebookComposer release];
And this is the important part in the DEFacebookComposeViewController class;
- (id)init
{
if ([[UIDevice currentDevice].systemVersion floatValue] >= 6) {
self = [(DEFacebookComposeViewController*)[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook] retain];
return self;
}
self = [super init];
if (self) {
[self facebookComposeViewControllerInit];
}
return self;
}
There. If the iOS version is 6 the init method return [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook] wich is the vanilla iOS6 way of interact with facebook.
How about using Social framework that is officially provided by Apple on iOS6, especially on iOS6 ?
Ex:
SLComposeViewController
 *viewController
= [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[self presentViewController:viewController animated:YES completion:nil];

What will happen to users running a lower version of IOS if new code is called?

I am fairly new to iOS Development and I've always wondered if a user running my application on iOS 4 were to try and run this code:
//POST TWEET//
- (void)showTweetSheet
{
TWTweetComposeViewController *tweetSheet =
[[TWTweetComposeViewController alloc] init];
tweetSheet.completionHandler = ^(TWTweetComposeViewControllerResult result) {
switch(result) {
case TWTweetComposeViewControllerResultCancelled:
break;
case TWTweetComposeViewControllerResultDone:
break;
}
dispatch_async(dispatch_get_main_queue(), ^{
[self dismissViewControllerAnimated:YES completion:^{
NSLog(#"Tweet Sheet has been dismissed.");
}];
});
};
[tweetSheet setInitialText:#"Check out this cool picture I found on #Pickr_"];
// Add an URL to the Tweet. You can add multiple URLs.
if (![tweetSheet addURL:[NSURL URLWithString:ImageHost]]){
NSLog(#"Unable to add the URL!");
}
[self presentViewController:tweetSheet animated:YES completion:^{
NSLog(#"Tweet sheet has been presented.");
}];
}
What would happen? Would the application just terminate with an error or will the code just not run? And how do I properly implement features that are OS specific? Would I just use something like this:
NSString *DeviceVersion = [[UIDevice currentDevice] systemVersion];
int DeviceVersionInt = [DeviceVersion intValue];
if (DeviceVersionInt > 5)
{
//do something.
}
else
{
//don't do a thing.
}
It will crash on iOS 4 if you write iOS5 features without checking if they are available or not. Try to implement Twitter like this
Class twClass = NSClassFromString(#"TWTweetComposeViewController");
if (!twClass) // Framework not available, older iOS
{
//use iOS4 SDK to implement Twitter framework
}
else {
//use Apple provided default Twitter framework
}
Make sure you have added Twitter Framework with weak link.
Id imagine that it would work the same as with any other api. If you link against a function which is not in a previous version, the program will crash on an attempt to call the function. Therefore, version switches are used, as you demonstrated, to avoid crashes.
The app would crash. If you want to implement features based on iOS, you can use a variety of methods. See this question.

Error! Twitter not Conecting from my IPhone

i m using AddThis SDK for facebook,twitter and Email share, facebook and Email is nicely working while Twitter is giving error while conecting from my Iphone. it nice work from my simulator but from iphone twitter not connecting and just display a message in console"Error!" code: [AddThisSDK setTwitterViaText:#"QuotesApp"];
[AddThisSDK shareURL:#"http://HazratAliQuotes.com"
withService:#"twitter" title:string description:#"QuotesApp"];
Try this link to implement Twitter in your application for previous version of iOS
The Previos API of twitter is known as MGTTwitter
For AddThis:
To use Twitter OAuth-enabled sharing, you'll need to tell the library to do so, and then configure your Twitter consumer key, consumer secret, and callback URL.
[AddThisSDK setTwitterAuthenticationMode:ATTwitterAuthenticationTypeOAuth];
[AddThisSDK setTwitterConsumerKey:#"yourconsumerkey"];
[AddThisSDK setTwitterConsumerSecret:#"yourconsumersecret"];
[AddThisSDK setTwitterCallBackURL:#"yourcallbackurl"];
Below is the new answer added.
Remove the line [AddThisSDK setTwitterViaText:#"QuotesApp"];
Add the line [AddThisSDK setTwitterAuthenticationMode:ATTwitterAuthenticationTypeOAuth];
and on twitter button pressed create a selector and add the following code as
[AddThisSDK shareURL:#"http://www.addthis.com"
withService:#"twitter"
title:#"AddThis - The #1 Bookmarking & Sharing Service"
description:#"AddThis is a free way to boost traffic back to your site by making it easier for visitors to share your content."];
This is optional answer for you. In IOS 5 there is framework Tweeter.Framework. add this Framework and import, i use this for uploading a image(as a link) and message. may be it's helpful for you.:)
import "Twitter/Twitter.h"
and on Button Click..
- (void)postImageOnTweeterWall
{
if (imageView.image)
{
NSLog(#"tweetbutton pressed called");
// Create the view controller
TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];
// Optional: set an image, url and initial text
[twitter addImage:[UIImage imageNamed:#"img1.jpeg"]];
// [twitter addURL:[NSURL URLWithString:[NSString stringWithString:#"http://MobileDeveloperTips.com/"]]];
[twitter setInitialText:#"Tweet from iOS 5 app using the Twitter framework."];
// Show the controller
[self presentModalViewController:twitter animated:YES];
// Called when the tweet dialog has been closed
twitter.completionHandler = ^(TWTweetComposeViewControllerResult result)
{
NSString *title = #"Tweet Status";
NSString *msg;
if (result == TWTweetComposeViewControllerResultCancelled)
msg = #"Tweet compostion was canceled.";
else if (result == TWTweetComposeViewControllerResultDone)
msg = #"Tweet composition completed.";
// Show alert to see how things went...
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:#"Okay" otherButtonTitles:nil];
[alertView show];
// Dismiss the controller
[self dismissModalViewControllerAnimated:YES];
};
}
else
{ UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:#"Tweeter" message:#"Select photo to Upload"
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil] autorelease];
[alertView show];
}
}

twitter ios4 ..Accounts Framework

https://github.com/doubleencore/DETweetComposeViewController
I followed twitter integration here for supporting twitter for both ios5 and ios4.
if ([DETweetComposeViewController canSendTweet]) {
DETweetComposeViewControllerCompletionHandler completionHandler = ^(DETweetComposeViewControllerResult result) {
switch (result) {
case DETweetComposeViewControllerResultCancelled:
NSLog(#"Twitter Result: Cancelled");
break;
case DETweetComposeViewControllerResultDone:
NSLog(#"Twitter Result: Sent");
break;
}
[self dismissModalViewControllerAnimated:YES];
};
DETweetComposeViewController *tcvc = [[[DETweetComposeViewController alloc] init] autorelease];
[tcvc addImage:[UIImage imageNamed:#"YawkeyBusinessDog.jpg"]];
[tcvc addURL:[NSURL URLWithString:#"http://www.DoubleEncore.com/"]];
[tcvc addURL:[NSURL URLWithString:#"http://www.apple.com/ios/features.html#twitter"]];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentModalViewController:tcvc animated:YES];
}
else {
self.oAuth = [[[OAuth alloc] initWithConsumerKey:kDEConsumerKey andConsumerSecret:kDEConsumerSecret] autorelease];
TwitterDialog *td = [[[TwitterDialog alloc] init] autorelease];
td.twitterOAuth = self.oAuth;
td.delegate = self;
td.logindelegate = self;
[td show];
}
The tutorial didn't specify what is self.oAuth so i imported oAuth in my interface file and declared oAuth as a property
I have the following problems now :
1) the code above worked when i didn't put the completion handler block, after adding the handler the build is failing with error in DETweetAccountSelectorViewControllerDelegate in line
#import <Accounts/Accounts.h>
saying no such file or directory exist... which it didn't when there was no completion handler. And yes i have now linked and unlinked to accounts framework more than 5 times now..so it was working before not now.
2) when i run the project without completion handler the twitter dialog pops up. but can't post(authorization request comes up for my twitter app but after authorizing it gets stuck) ..the log in console is
discarded an uncaught exception in the webView:decidePolicyForNavigationAction:request:frame:decisionListener: delegate:
also if i close the dialog ..the program crashes because there is no code to dimiss the controller which i tried to solve using completion handler.
3) if any one have a simpler tutorial to integrate twitter for both ios4 and ios5 ..can you share..
Thanks
I have recently received the:
discarded an uncaught exception in the webView:decidePolicyForNavigationAction:request:frame:decisionListener: delegate:
Error in a different twitter framework that I am using.
The solution to the problem was that I had an error in my Delegate Function twitterDidLogin.
I think that if you solve for the error which you have not included on this post, that the 'discarded an uncaught exception' error will go away. Maybe you could paste that error?
I found the answer..there tutorial takes you in the opposite direction..waste my 5 hours on it..
just have to initialize DETweetComposeViewController ..and the rest is taken care of.. in their tutorial.. they show twitter dialog .and can tweet function which will just confuse