Posting score to facebook - facebook

I created an app in SpriteKit with xcode where when the game is over, it shows you your score, and I want to add the feature to post your score to facebook. Pretty much all my code is in MyScene.m where it can't access presentViewController. Only my ViewController.m file can access that, so I tried calling a instance method in Viewcontroller from Myscene.m but I can't find a way to do that. The only way I have found calling methods from other files is using +(void) which is a class method I think.
Myscene.m:
if (location.x < 315 && location.x > 261 && location.y < 404 && location.y > 361) {
//if you click the post to facebook button (btw, location is a variable for where you tapped on the screen)
[ViewController facebook];
}
ViewController.m:
+(void)facebook{
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *facebook = [[SLComposeViewController alloc] init];
facebook = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[facebook setInitialText:#"initial text"];
}
}
So that worked, and it called the facebook class method correctly, but when I put [self presentViewController:facebook animated:YES] after the setInitialText brackets, it gives me this error: No known class method for selector 'presentViewController:animated:'
By the way it lets me use presentViewController in a instance method but I can't call that method from inside the class method or from my Myscene file. Is there any way to either call an instance method from another file, or access presentViewController from a class method? Thanks

You can either pass a reference for your View Controller to your SKScene or you can use NSNotificationCenter instead. I prefer to use the latter.
First make sure you have added the Social.framework to your project.
Import the social framework into your View Controller #import <Social/Social.h>
Then in your View Controller's viewDidLoad method add this code:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(createPost:)
name:#"CreatePost"
object:nil];
Next add this method to your View Controller:
-(void)createPost:(NSNotification *)notification
{
NSDictionary *postData = [notification userInfo];
NSString *postText = (NSString *)[postData objectForKey:#"postText"];
NSLog(#"%#",postText);
// build your tweet, facebook, etc...
SLComposeViewController *mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[self presentViewController:mySLComposerSheet animated:YES completion:nil];
}
At the appropriate location in your SKScene, (won game, lost game, etc...) add this code:
NSString *postText = #"I just beat the last level.";
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:postText forKey:#"postText"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"CreatePost" object:self userInfo:userInfo];
The above code sends a NSNotification, with text, which your View Controller will pick up and execute the specified method.

Related

Present another View Controller from SkScene

I'm trying to present another viewController from my "SkScene".
This is my main viewController(tuViewController)
Code:
-(void) openTweetSheet{
FacebookLikeViewDemoViewController *ctrl = [[FacebookLikeViewDemoViewController alloc] initWithNibName:#"FacebookLikeViewDemoViewController" bundle:nil];
[self presentViewController:ctrl animated:YES completion:nil];
}
This is my "SkScene":
tuViewController *viewController = [[tuViewController alloc]init];
[viewController openTweetSheet];
And the viewController which i want to present is FacebookLikeViewDemoViewController and i need to have way back to "SkScene".
And i got sigabrt error, i tried few ways to present viewController but always with failure, one time i got swap to viewController but it was entirely black. I read a lot how to perform that, but i personally can't figure out. I appreciate your help.
I tried too with Notification Center.
Main viewController
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(goToGameOverViewController:)
name:#"GoToGameOverViewController"
object:nil];
-(void)goToGameOverViewController:(NSNotification *) notification {
FacebookLikeViewDemoViewController *helpVC = [[FacebookLikeViewDemoViewController alloc]initWithNibName:#"HelpViewController" bundle:nil];
UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
[rootVC presentViewController:helpVC animated:YES completion:nil];
}
SkScene
[[NSNotificationCenter defaultCenter]
postNotificationName:#"GoToGameOverViewController" object:self];
But i got the same error. I prefer to figure out why the way with notification won't work.
I assume by your question that you are looking to do some social media posting.
You can either pass a reference for your View Controller to your SKScene or you can use NSNotificationCenter instead. I prefer to use the latter.
First make sure you have added the Social.framework to your project.
Import the social framework into your View Controller #import <Social/Social.h>
Then in your View Controller's viewDidLoad method add this code:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(createTweet:)
name:#"CreateTweet"
object:nil];
Next add this method to your View Controller:
-(void)createTweet:(NSNotification *)notification
{
NSDictionary *tweetData = [notification userInfo];
NSString *tweetText = (NSString *)[tweetData objectForKey:#"tweetText"];
NSLog(#"%#",tweetText);
// build your tweet, facebook, etc...
SLComposeViewController *mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[self presentViewController:mySLComposerSheet animated:YES completion:nil];
}
At the appropriate location in your SKScene, (won game, lost game, etc...) add this code:
NSString *tweetText = #"I just beat the last level.";
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:tweetText forKey:#"tweetText"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"CreateTweet" object:self userInfo:userInfo];
The above code sends a NSNotification, with text, which your View Controller will pick up and execute the specified method (which is createTweet in the above example).

Open View from didReceiveRemoteNotification

I have this question asked few times and I have tried a few different methods but have not been successful.
The latest method I have tried is as followed :
I included the ViewController that I wanted to show. I then put this code in the didReceiveRemoteNotification method.
CarFinderViewController *pvc = [[CarFinderViewController alloc] init];
// [self.window.rootViewController presentViewController:pvc animated:YES completion:nil];
[(UINavigationController *)self.window.rootViewController pushViewController:pvc animated:NO];
This did not work. I think the problem I may be having is that my initial view is not a navigation controller like a lot of the examples show.
This is a picture of my story board> The VC that I want to send the user to is the car finder (bottom right)
Can someone explain to me what I might be doing wrong?
You could use basically postNotification when you receive the Remote Notification
for exmaple in your didReceiveRemoteNotification post notification like this
[[NSNotificationCenter defaultCenter] postNotificationName:#"pushNotification" object:nil userInfo:userInfo];
now in your FirstViewController's you can register the FirstViewController for this notification like this
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(pushNotificationReceived) name:#"pushNotification" object:nil];
and in your method
-(void)pushNotificationReceived{
CarFinderViewController *pvc = [[CarFinderViewController alloc] init];
[self presentViewController:pvc animated:YES completion:nil];
}
don't forget to remove the observer from notification in your dealloc method
-(void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
I think the simplest solution would be to show the CarFinderViewController as a modal view instead of trying to push it to a navigation controller which may or may not be visible at the time.
Another important point to avoid further inconsistencies I'd recommend you instantiate your CarFinderViewController from the storyboard instead of directly through the class methods.
Something like:
UIViewController * vc = self.window.rootViewController;
// You need to set the identifier from the Interface
// Builder for the following line to work
CarFinderViewController *pvc = [vc.storyboard instantiateViewControllerWithIdentifier:#"CarFinderViewController"];
[vc presentViewController:pvc animated:YES completion:nil];

NSnotifications for multiple downloads

I have a parser class and some view controller classes. In the parser class i am sending a request and receiving an asynchronous response. I want multiple downloads, say one per viewcontroller. So i register an observer in each of these classes :
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(dataDownloadComplete:) name:OP_DataComplete object:nil];
and then post a notification in :
-(void)connectionDidFinishLoading:(NSURLConnection *)connection method of the parser class.
[[NSNotificationCenter defaultCenter] postNotificationName:OP_DataComplete object:nil];
but then on running the code the first viewcontroller works fine but for the second one onwards after download and parser class posting notification infinitely the code enters the first class's dataDownloadComplete: method although i have specified a different method name in the selector each time. I don't understand what the error might be. Please help. Thanks in advance.
Both view controllers are listening for the notification so both methods should be being called, one after another.
There's a few ways to solve this. The easiest would be for the notification to contain some sort of identifier that the view controller can look at to see if it should ignore it or not. NSNotifications have a userInfo property for this.
NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:#"viewController1", #"id", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:OP_DataComplete object:self userInfo:info];
and when you recieve the notification, check to see who it's for :
- (void)dataDownloadComplete:(NSNotification *)notification {
NSString *id = [[notification userInfo] objectForKey:#"id"];
if (NO == [id isEqualToString:#"viewController1"]) return;
// Deal with the notification here
...
}
There's a few other ways to deal with it but without knowing more about your code, I can't explain them well - basically you can specify the objects that you want to listen to notifications from (see how I have object:self but you sent object:nil) but sometimes your architecture won't allow that to happen.
it's better to create a protocol:
#protocol MONStuffParserRecipientProtocol
#required
- (void)parsedStuffIsReady:(NSDictionary *)parsedStuff;
#end
and to declare the view controller:
#class MONStuffDownloadAndParserOperation;
#interface MONViewController : UIViewController < MONStuffParserRecipientProtocol >
{
MONStuffDownloadAndParserOperation * operation; // add property
}
...
- (void)parsedStuffIsReady:(NSDictionary *)parsedStuff; // implement protocol
#end
and add some backend: to the view controller
- (void)displayDataAtURL:(NSURL *)url
{
MONStuffDownloadAndParserOperation * op = self.operation;
if (op) {
[op cancel];
}
[self putUpLoadingIndicator];
MONStuffDownloadAndParserOperation * next = [[MONStuffDownloadAndParserOperation alloc] initWithURL:url recipient:viewController];
self.operation = next;
[next release], next = 0;
}
and have the operation hold on to the view controller:
#interface MONStuffDownloadAndParserOperation : NSOperation
{
NSObject<MONStuffParserRecipientProtocol>* recipient; // << retained
}
- (id)initWithURL:(NSURL *)url Recipient:(NSObject<MONStuffParserRecipientProtocol>*)recipient;
#end
and have the operation message the recipient when the data is downloaded and parsed:
// you may want to message from the main thread, if there are ui updates
[recipient parsedStuffIsReady:parsedStuff];
there are a few more things to implement -- it's just a form. it's safer and involves direct messaging, ref counting, cancellation, and such.

How to push a View in a NavigationController which is containing in another tab in a TabBarController?

I have a TabBarController with 2 tabs, in one is a MapView and in the other one a simple TableView in a NavigationController. Both display Data from the same source. If any Data in the table is tapped, I add a DetailViewController to the NavigationController and show more details. Now on the MapView I also want to open this DetailViewController when the Data is tapped in the map. What's the best way to do this? I tried some with Notification but this doesn't work well because the TableViewController is finished loading (and registered as an observer) after the Notification is sent.
Here's my code:
MapViewController:
- (IBAction)goToNearestEvent:(id)sender {
if (currentNearestEvent) {
[[self tabBarController] setSelectedIndex:1];
NSDictionary *noteInfo = [[NSDictionary alloc] initWithObjectsAndKeys:currentNearestEvent, #"event", nil];
NSNotification *note = [NSNotification notificationWithName:#"loadDetailViewForEvent" object:self userInfo:noteInfo];
[[NSNotificationCenter defaultCenter] postNotification:note];
[noteInfo release];
}
}
TableViewController:
- (void)viewDidLoad {
[super viewDidLoad];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:#selector(loadDetailViewForEvent:)
name:#"loadDetailViewForEvent"
object:nil];
}
- (void)loadDetailViewForEvent:(NSNotification *)note {
Event *e = [[note userInfo] objectForKey:#"event"];
[self loadEventDetailViewWithEvent:e];
}
So I'm very new to iOS / Cocoa programming. Maybe my approach is the wrong choice. So I hope anybody could tell me how to solve such things the right way.
I forgot to declare my structure clearly:
- UITabBarController
- MapView (1)
- NavigationControllerContainer
- NavigationControllerView (2)
- TableView
I want to push a new View from the MapView (1) to the NavigationControllerView (2).
If you're going to use notifications, the fix is to force the second tab to be "created" before it's displayed.
Something like:
UIViewController *otherController = [[[self tabBarController] viewControllers] objectAtIndex:1];
otherController.view; // this is magic;
// it causes Apple to load the view,
// run viewDidLoad etc,
// for the other controller
[[self tabBarController] setSelectedIndex:1];
I don't have access to my code, but I did something similar to:
[[self.tabBarController.viewControllers objectAtIndex:1] pushViewController:detailView animated:YES];
Give this a try and let me know.
I think the observer/notification pattern is the right one. However, you normally want "controllers" to observe "model" objects.
I would create a Model object that contains the selected Event.
When each viewController is loaded, it looks at the "Model" object and directs itself to the selected event.
When any of the viewControllers changes the selected event, it does so in the Model, and then the notification propagates to the other(s) controllers.

Detecting current iPhone input language

Does anybody knows, can I get the current input language and/or keyboard layout in iPhone application? Can I also get a notification when input language was changed?
In iOS 4.2 and later, you can use the UITextInputMode class to determine the primary language currently being used for text input.
[UITextInputMode currentInputMode].primaryLanguage will give you an NSString representing the BCP 47 language code such as “es”, “en-US”, or “fr-CA”.
You can register for the UITextInputCurrentInputModeDidChangeNotification to be alerted when the current input mode changes.
(You might also be interested in the "Getting Your Apps Ready for China and other Hot New Markets" WWDC session, and Internationalization Programming Topics.)
You can ask current first responder (UITextField, UISearchBar, etc.) via UIResponder method textInputMode:
// assume you have instance variable pointing to search bar currently entering
UITextInputMode *inputMode = [self.searchBar textInputMode];
NSString *lang = inputMode.primaryLanguage;
You can add an observer to the default notification center:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(inputModeDidChange:)
name:#"UIKeyboardCurrentInputModeDidChangeNotification"
object:nil];
This method prints the currently selected input language (like "en_US" or "de_DE"):
- (void)inputModeDidChange:(NSNotification*)notification
{
id obj = [notification object];
if ([obj respondsToSelector:#selector(inputModeLastUsedPreference)]) {
id mode = [obj performSelector:#selector(inputModeLastUsedPreference)];
NSLog(#"mode: %#", mode);
}
}
BUT: All the above is not documented and you should not use it in shipping code!
From the Apple Reference Library - "Getting the Current Language and Locale":
NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
NSArray* languages = [defs objectForKey:#"AppleLanguages"];
NSString* preferredLang = [languages objectAtIndex:0];
In line with the top answers, the following is a generic solution to getting the keyboard language whenever it is changed. Register for the notification UITextInputCurrentInputModeDidChangeNotification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(inputModeDidChange:) name:UITextInputCurrentInputModeDidChangeNotification object:nil];
Then in inputModeDidChange
-(void)inputModeDidChange:(NSNotification *)notification {
UIView *firstResponder = [UIView currentFirstResponder];
UITextInputMode *currentInputMode = firstResponder.textInputMode;
NSString *keyboardLanguage = [currentInputMode primaryLanguage];
NSLog(#"%#", keyboardLanguage); // e.g. en-US
}
Where currentFirstResponder is from a category on UIView to get the first responder view, as suggested in this SO post:
// UIView+Additions.h
#import <UIKit/UIKit.h>
#interface UIView (Additions)
+ (id)currentFirstResponder;
#end
Implementation
// UIView+Additions.m
#import "UIView+Additions.h"
static __weak id currentFirstResponder;
#implementation UIView (Additions)
+ (id)currentFirstResponder {
currentFirstResponder = nil;
// This will invoke on first responder when target is nil
[[UIApplication sharedApplication] sendAction:#selector(findFirstResponder:)
to:nil
from:nil
forEvent:nil];
return currentFirstResponder;
}
- (void)findFirstResponder:(id)sender {
// First responder will set the static variable to itself
currentFirstResponder = self;
}
#end
The way I would do it is as follows:
Register your ViewController as a listener to UIApplicationDidBecomeActiveNotification
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(applicationDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
In applicationDidBecomeActive handler, check the current language using [NSLocale preferredLanguages] and act upon it accordingly.
This approach gives you what you want and is totally shippable without having to use private API.