iOS app crashes when displaying SLComposeViewController - ios

Okay, so I have a single view iOS app. Inside the view controller, I have a method attached to a button in the storyboard. Here is the method for when then button is pressed:
- (IBAction)tweetButton:(id)sender {
if ([SLComposeViewController isAvailableForServiceType:#"SLServiceTypeTwitter"]) {
SLComposeViewController *tweetSheet = [[SLComposeViewController alloc] init];
tweetSheet = [SLComposeViewController composeViewControllerForServiceType:#"SLServiceTypeTwitter"];
[tweetSheet setInitialText:#"This is a test."];
[self presentViewController:tweetSheet animated:YES completion:nil];
}
else {
NSLog(#"Twitter not configured.");
}
}
Whenever I press the button in the app, I get a crash with the following error:
2015-07-17 15:57:24.110 Now Playing[425:19583]
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target <ViewController: 0x157e4c620>.'
My code follows pretty much every example that I have seen online, so I'm not sure what's up.

You mustn't hard type the service type even if it's taking a NSString, you must use one of the constant instead: SLServiceTypeTwitter, SLServiceTypeFacebook...
Also, composeViewControllerForServiceType: is already doing the alloc/init steps for you, so no need to allocate it before calling this method.
Code fixed:
- (IBAction)tweetButton:(id)sender {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:#"This is a test."];
[self presentViewController:tweetSheet animated:YES completion:nil];
}
else {
NSLog(#"Twitter not configured.");
}
}

Related

SLComposeViewController initWithServiceType failed to get extension error

I am using Facebook and Twitter share in one of my application. Suddenly while testing, I started getting below following error. I checked my code and it looks okay to me.
2017-04-29 14:41:19.727248 iCamTranslator[1140:322770] [core] SLComposeViewController initWithServiceType failed to get extension for identifier com.apple.share.Facebook.post
2017-04-29 14:41:19.727642 iCamTranslator[1140:322770] [core] SLComposeViewController dealloc <SLComposeViewController: 0x102992050>
2017-04-29 14:41:19.750499 iCamTranslator[1140:322770] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target <AboutViewController: 0x102986900>.'
Here is my code. Note that error occurs for both Facebook and twitter share but I am writing code for only Facebook share as both errors are similar.
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){
if (result == SLComposeViewControllerResultCancelled)
{
NSLog(#"Cancelled");
}
else
{
NSLog(#"Done");
}
[controller dismissViewControllerAnimated:YES completion:Nil];
};
controller.completionHandler =myBlock;
[controller setInitialText:#"iCamTranslator is a great translation app."];
[controller addURL:[NSURL URLWithString:#"https://itunes.apple.com/us/app/icamtranslator/id955853183?mt=8"]];
[controller addImage:[UIImage imageNamed:#"logo-2.png"]];
[self presentViewController:controller animated:YES completion:Nil];
}
Any help is highly appreciated.
This stupid error was caused because I have not updated my Facebook app on my device. After updating, everything start's working fine. Sorry for bothering you guys.

Detecting if Twitter account is set up on iOS 7

I've been following some tutorials on integrating Twitter into my application. Here is what I have so far:
- (IBAction)postToTweeter:(id)sender {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
SLComposeViewController *tweetSheet = [SLComposeViewController
composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:#"this is a test"];
[self presentViewController:tweetSheet animated:YES completion:nil];
}
}
My storyboard has a button, and when a user taps that button, this is the code that gets called. However, this only works whenever they have a Twitter account setup on their iPhone first. How can I handle the case where a user has not set up a twitter account yet, and show them an alert instructing them to add a Twitter account?
SLComposeViewController has a very handy built-in mechanism, whereby it will offer to send the user to Settings if you instantiate a ComposeViewController when the user hasn't set up / logged into the social-media service in question. To test this, all you need to do is remove the conditional so your code looks like this:
- (IBAction)postToTweeter:(id)sender {
SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:#"this is a test"];
[self presentViewController:tweetSheet animated:YES completion:nil];
}
...and you'll find that iOS will automatically pop up an alert which will take the user through to Settings.
Note that I've found that this doesn't tend to work in the simulator, so it's best to test on a device.

Warning: Attempt to present * on * whose view is not in the window hierarchy

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.");
}]; }
}

How to share local var in different view in Objective-C

I am making casual game now.
I'd like to share the score with twitter.
My this work image is that below.
1.After game player plays the game and there is the score.
2.When the game goes to gameoverscene, they can push twitter button (using social.framework)
3. There is a text like "You got xx score !!" in the twitterdisplay(modal).
*I would like to change xx to the shared score.
Could you please give me some advices to do that ?
I am a beginner of Objective - C. So, easy way is better for me.
Little by little, I'd like to concern about security and scalability.
[Information]
*MainScene and GameOverScene are made from SKScene
MainScene.m
#implementation MainScene {
//The score
NSInteger _score;
}
- (void)incrementScore
{
_score++;
}
GameOverScene.m
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if([node.name isEqualToString:#"twitterbutton"]){
NSLog(#"self.delegate = %#",self.delegate);
[self.delegate showShareScreen];
//delegate to ViewController
if (nil == self.delegate) NSLog(#"delegate is nil");
}
}
ViewController.m
-(void)showShareScreen
{
NSLog(#"showShareScreen");
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController
composeViewControllerForServiceType:SLServiceTypeTwitter];
tweetSheet.modalPresentationStyle = UIModalPresentationFormSheet;
tweetSheet.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[tweetSheet setInitialText:#"You got xx score"];
NSLog(#"self = %#",self);
[self presentViewController:tweetSheet animated:YES completion:nil];
}
else {
NSLog(#"not sls type twitter");
}
}
Change your line in delegate method in ViewController
[tweetSheet setInitialText:#"You got xx score"];
to as shown below; to include the score with a NSString's stringWithFormat: method
-(void)showShareScreenWithScore:(NSInteger) score {
NSLog(#"showShareScreen");
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController
composeViewControllerForServiceType:SLServiceTypeTwitter];
tweetSheet.modalPresentationStyle = UIModalPresentationFormSheet;
tweetSheet.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[tweetSheet setInitialText:[NSString stringWithFormat:#"You got %d score",score]];
NSLog(#"self = %#",self);
[self presentViewController:tweetSheet animated:YES completion:nil];
}
else {
NSLog(#"not sls type twitter");
}
}
And from GameOverScene call this delegate method.
Note: When you are creating Game over scene at that time after object instantiation you can set the property of GameOverScene. So just declare a property
#property (assign , nonatomic) NSInteger score;
and set its value Using NSURLSession that the game is getting over. And score value you can get from MainScene score property.
NSURLSession
The NSURLSession class and related classes provide an API for
downloading content via HTTP. This API provides a rich set of delegate
methods for supporting authentication and gives your app the ability
to perform background downloads when your app is not running or, in
iOS, while your app is suspended.

SLComposeViewController - SLServiceTypeTwitter, addImage issue

Here is what I am facing using SLComposeViewController to generate a tweet in an iOS app.
First of all this is my code:
- (IBAction)twitButtonHandler:(id)sender
{
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
SLComposeViewController *tweetController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
tweetController.completionHandler=nil;
[tweetController setInitialText:#"Hello this is a tweet."];
if (![tweetController addImage:[UIImage imageNamed:#"MyNicePicture.png"]]) {
NSLog(#"Unable to add the image!");
} else NSLog(#"Add the image OK!");
[self presentViewController:tweetController animated:YES completion:Nil];
} else {
// The device cannot send tweets.
NSLog(#"No Tweet possible!");
return;
}
}
The above code works fine except for the fact that I never see the image in the actual tweet though it always displays the message "Add the image OK!" in the debugger. Can anyone see something wrong in the code? Or tell me where could be a potential problem?
Digging more into the issue on my own made me discover that :
[UIImage imageNamed:#"MyNicePicture.png"]
was returning nil.
From that point everything is explained and the problem has to do with the way I get the UIImage object, not with SLComposeViewController and SLServiceTypeTwitter as I first thought.

Resources