Native iOS Twitter post from Unity - ios

I'm trying to implement the native iOS twitter account to unity. I found a code which uses the native social accounts via Xcode, and so I'm trying to make it communicate to unity but I get this error: "Use of undeclared identifier 'self'"
Here's my code:
extern "C"
{
void _PushTwitter(const char * messageTweet)
{
//Check Twitter accessibility and at least one account is setup.
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
SLComposeViewController *tweetSheet =[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
//This is setting the initial text for our share card.
[tweetSheet setInitialText:[NSString stringWithUTF8String:messageTweet]];
//Brings up the little share card with the test we have pre defind.
[self presentViewController:tweetSheet 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 Twitter account setup and your device is running the latest iOS version" delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil];
[alertView show];
}
}
}
I get the error on the [self presentViewController...] part.
This was the original code:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
//Each button title we gave to our action sheet is given a tag starting with 0.
if (buttonIndex == 0) {
//Check Twitter accessibility and at least one account is setup.
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
SLComposeViewController *tweetSheet =[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
//This is setting the initial text for our share card.
[tweetSheet setInitialText:#"veasoftware.com made it easy to intergate Twitter with iOS 6! :D "];
//Brings up the little share card with the test we have pre defind.
[self presentViewController:tweetSheet 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 Twitter account setup and your device is using iOS6." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
}
}
Any help will be greatly appreciated.
EDIT/SOLUTION: For anybody interested, I've found a much easier integration on this article (probably the only one out there): http://www.theappguruz.com/unity/share-image-and-text-on-twitter-in-ios-using-native-code/ Works like a charm. I'm running iOS 8, Unity 5, and the latest version of Xcode.

The code that you found is probably part of an objective-c class, where self is defined and it points to the current object.
The code that you wrote doesn't seem to be a method of a class, but a global function, which means it doesn't have an object attached.
You should add the code you found to one of the classes Unity defines for you, maybe inside UnityAppController.mm

Related

Twitter addImage returns False

Here's my code:
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
UIViewController* rootVC = [self getRootViewController];
SLComposeViewController *tweetSheet = [SLComposeViewController
composeViewControllerForServiceType:SLServiceTypeTwitter];
NSLog(#"Image: %f",image.size.height);
[tweetSheet addImage:image];
NSLog([tweetSheet addImage:image] ? #"True" : #"False");
[tweetSheet setInitialText: //text
[rootVC presentViewController:tweetSheet animated:YES completion:nil];
}else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"No Twitter Account"
message: //message
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:nil];
[alertView show];
}
The first log returns my image's size, so it isn't nil. The second log returns False. On an iOS 6 device, I'm unable to tap the send button. On an iOS 9 device, not only can I send it, but the image is attached successfully to the tweet. Am I not correctly logging addImage? How could it tweet the image if it wasn't added? I'm trying to fix the disabled send button on iOS 6, but now I'm worried about its functionality on iOS 9 despite tweeting successfully.
Please help me resolve this, it's my last problem before I can ship my game.
This method returns NO if image does not fit in the currently available space or if the view controller has already been presented to the user (and therefore cannot be changed). For the accepted UIImage formats, see Figure 2. Image size limits are dependent on the target service and are documented by the service provider. For links to documentation for the supported services, see Table 1.
i remember a similar problem, the solution was to reformat the images (or change the dpi, dont remember now)

Adding iOS app to Twitter accounts in settings??

I am adding some sharing in my app to allow the use to post to FaceBook and to Twitter. I decided to use the Social Sharing framework as it does exactly what I need and works well.
However I noticed that my app doesn't ask the user for permission it assumes it has permission and does a check if there is an account setup.
The problem I see here is due to this - my app doesn't appear in the Settings - > Twitter under "Allow these apps to use your account" section.
Her is the code I use when I want to share an item - user taps on a UIButton which presents a UIActionSheet with various options"
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]){
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
SLComposeViewControllerCompletionHandler completionBlock = ^(SLComposeViewControllerResult result){
if (result == SLComposeViewControllerResultCancelled){
NSLog (#"Cancelled");
}else {
NSLog(#"Done");
}
[controller dismissViewControllerAnimated:YES completion:nil];
};
controller.completionHandler = completionBlock;
NSURL *url = [NSURL URLWithString:self.item.url];
/* Adding the text to the Tweet */
[controller setInitialText:#"Online now!"];
[controller addURL:url];
[controller addImage:self.imageView.image];
[self presentViewController:controller animated:YES completion:nil];
}
else {
[self showUserAlert:#"" message:#"Please make sure your FaceBook account has been setup on this device"
delegate:self
cancelButtonTitle:#"Okay"];
}
How do I ask for access first? And add the app to the settings part of iOS so the user can turn it on or off?
I think you don't need a permission because your app can't post without user action. User have all control.
You can check if sharing possible with canSendTweet method in TWTweetComposeViewController. Look up Xcode documentation. I'm sure Apple has something similar for Facebook.
Apple documentation

iOS 8 Today Widget - SLComposeViewController not displaying properly

I've created an extension, in fact a Today Widget, using Xcode 6 and run it on my iOS 8 beta device (I am a registered iOS developer).
However, being new to development, I've encountered this issue with the SLComposeViewController being 'stuck' inside its view (shown below), deeming the user to be unable to interact with it and not be able to consequently post a tweet.
Is there any way to fix this and bring the SLComposeViewController to the front, in front of the Notification Centre pane? Any help would be appreciated.
Edit: (yay, fixed that "Hello World" text)
2nd Edit: Here's my SLComposeViewController code:
- (IBAction)TwitterShare:(id)sender; {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController
composeViewControllerForServiceType:SLServiceTypeTwitter];
[self presentViewController:tweetSheet animated:YES completion:nil];
}
else
{
UIAlertView *twitterAlert = [[UIAlertView alloc] initWithTitle:#"Uh oh!" message:#"Ensure you have setup a valid Twitter account and/or you have allowed access for Twitter in this application." delegate:nil cancelButtonTitle:#"Dismiss" otherButtonTitles:nil, nil];
[twitterAlert show];
twitterAlert = nil;
}
}
According to Apple's documentation SLComposeViewController is not allowed to be shown in NC. They want anything that requires keyboard entry to happen in-app.

iOS UIalertView displaying and processing order

I have this code
- (IBAction)save:(id)sender {
self.imageView.image = [UIImage imageNamed:#"loading.gif"];
[self.view setUserInteractionEnabled:NO];
MBProgressHUD *hudSave = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
hudSave.labelText = #"Saving...";
NSLog(#"save");
UIAlertView *deco;
if (!isVideo) {
UIImageWriteToSavedPhotosAlbum (imageView.image, nil, nil , nil);
deco = [[UIAlertView alloc]initWithTitle:#"Save" message:#"Your photo has been saved." delegate: nil cancelButtonTitle:#"oK" otherButtonTitles:nil];
}else{
//UIAlertView *explain;
//explain = [[UIAlertView alloc]initWithTitle:#"Wait during processing" message:#"Your video is being filtered, this process may be long, depending of both video and device. Please do not close this app until task is finished." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
UIAlertView *explain;
explain = [[UIAlertView alloc]initWithTitle:#"Wait during processing" message:#"Your video is being filtered, this process may be long, depending of both video and device. Please do not close this app until task is finished." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[explain show];
[self InitialisationRefiltrage:urlVideo];
//[self InitialisationRefiltrage:urlVideo];
deco = [[UIAlertView alloc]initWithTitle:#"Save" message:#"Your video has been saved." delegate: nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
}
[MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
[deco show];
[self.view setUserInteractionEnabled:YES];
}
The problem part is if my file is a video, it goes directly too "initialisationRefiltrage" (who is working fine) but without displaying the MBProgressHUD and the alert view explain, and after my video traitement, it display everything (explain, deco, and the MBProgressHUD) at the same time.
I try something with dispatch, thread, etc... But i think a don't do it correctly, so can you please give me a clue too how to do that.
Have a nice day.
Put the code [self InitialisationRefiltrage:urlVideo] in the delegate method of your UIAlertView so that it is executed only when the alert has been displayed and user has tapped on a button of the alert.
You may also use instead some third-party UIAlertView subclasses that uses completion blocks to make your code only execute when the alert is dismissed. See my class that does this for example.
Besides, you should respect coding conventions and use a method name beginning with a lowercase letter to make your code more readable.
The UI is updated in the "run loop".
The calls you're making tell iOS to display some views (alert, MUD...) and it'll do that on the next run through the loop.
What you need to do is wait for the user to respond to the alert before continuing. You do this by setting yourself as the UIAlert's delegate, then responding to the event:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
...
}
There are also libraries available that allow you to pass a block to the alert view, thus simplifying the whole thing. (https://github.com/jivadevoe/UIAlertView-Blocks, for example)
P.S. I see that you're new to Stack Overflow - please tick my answer if you're happy that it has responded to your question...

Deployment target vs. base SDK with TWTweetComposeViewController

I have set my base SDK to 6.0 and my Deployment Target to 6.0.
When I build, I get the following error.
'TWTweetComposeViewController' is deprecated: first deprecated in iOS 6.0
If I drop my Deployment Target down to 5.1 it compiles without error. Since TWTweetComposeViewController is "Available in iOS 5.0 and later.", this seems opposite to what I was expecting.
I would just leave it like that but I am also using Social/Social.h which is only available in 6.0 and up and will be error prone for the 5.1 users.
This is the problematic line of code:
if ([TWTweetComposeViewController canSendTweet]){
Racking my brain on this. I'm hoping it is just some obscure setting I have missed.
Thanks in advance.
TWTweetComposeViewController is deprecated, because for iOS 6.0 there is a new framework to handle all social interactions called Social.framework. Inside there you can find similar functionality to post tweets via SLComposeViewController.
to solve your problem with iOS6 you have to options depending on what you support:
Support iOS6 and above: Just use SLComposeViewController. Link framework Social.framework and done with it.
Support iOS 5.x and above: Needs more work. First need to check if the SLComposeViewController exists (so you are in iOS 6.0), and then use it and present it, otherwise use the TWTweetComposeViewController for older iOS. Also you would need to link framework Social.framework as optional.
As a side note, if you support iOS 6 only, you can use same controller to post to facebook for free, so consider using facebook integration too.
Instead of TWTweetComposeViewController use SLComposeViewController which is include in Social Framework
- (IBAction)SendTweet:(id)sender {
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[mySLComposerSheet setInitialText:[NSString stringWithFormat:#" THIS IS AN EXAMPLE",mySLComposerSheet.serviceType]];
[mySLComposerSheet addImage:[UIImage imageNamed:#"image.png"]];
[mySLComposerSheet addURL:[NSURL URLWithString:#"http://www.StackOverflow.com/"]];
[self presentViewController:mySLComposerSheet animated:YES completion:nil];
}
[mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
NSLog(#"dfsdf");
NSString *output;
switch (result) {
case SLComposeViewControllerResultCancelled:
output = #"ACtionCancelled";
break;
case SLComposeViewControllerResultDone:
output = #"Post Successfull";
[self dismissViewControllerAnimated:YES completion:nil];
break;
default:
break;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Twitter Message" message:output delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
}];
}

Resources