Hi all.
I’ve been looking around the forum and I’m having trouble with this one as I’m new to Obj-C. I’ve basically been building everything through Unity and had little reason to dip in to Xcode.
Basically I need to implement a 3rd party library in to my game. The library is in Xcode and written in C. What I need to do is make a call to one of the library functions and pass it a UIViewController as an argument. This is so it can have an pre-written email pop-up which the user can then read and click send. After it’s done it will then come back to the Unity game. This is similar to a ‘tell a friend’ button which is in quite a lot of games.
Does anyone know how I can get a UIViewController argument sent from the AppController.mm file. I know how to make function calls in Xcode but I’m really stuck on how to actually get a UIViewController argument sent :s
Hope someone can give me some advice on this.
Thanks all
-M
The AppController does not even use a UIViewController so you won’t get one back.
what you need to do is create a new one, push it and add stuff you want to it.
Responding to your PM, basically you have to do what Dreamora has described.
I’d say the simplest way is to create a class which extends UIViewController and creates a full screen empty view programmatically.
i.e.
@implementation YourViewControler : UIViewController
...
- (void) loadView {
self.wantsFullScreenLayout = YES;
UIView view * = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame;
self.view = view;
[view release];
}
...
Hey Dreamora, Johnny.
Thanks for the reply and advice, really appreciate it. I’ve added a UIViewController Class and tried a few things. I think I’m getting closer to fixing this one but I come up with the following error when I try to call the ‘displayNewsletter()’ method:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[RootViewController superview]: unrecognized selector sent to class 0xdfe818'
In my new UIViewController class I call:
[NewsletterLibrary displayNewsletter:self];
The displayNewsletter function (from the library I’m trying to implement) looks like this:
static BOOL z_displayingAlready = NO;
static newsLetterMailController* z_controller = nil;
....
+(void) displayNewsletter:(UIViewController*) baseController
{
if (z_displayingAlready)
{
return;
}
z_displayingAlready = YES;
[self resources];
[[NSNotificationCenter defaultCenter] addObserver:[NewsletterLibrary class]
selector:@selector(notification_newsletter_completed:)
name:NOTIFICATION_COMPLETED
object:nil];
z_controller = [[MailController alloc] initWithMode:NEWSLETTERMODE_NEWSLETTER baseController:baseController];
z_baseController = baseController;
[z_controller sendMail];
}
I’m wondering if it’s because I’m not passing it the route view controller. ie. the main window that Unity is using. Is this correct?
Do I need to add my new UIViewController as a subview to the main window first or just pass in the main window to the displayNewsletter function?
Thanks again guys, really appreciate the help.
Best,
-M
You can not add the view controller as subview no. It would be an overlay (thats why making it fullscreen is critical, cause it will eat all touch events, unity won’t get any anymore, just in case that was meant to show during play).
but your code seems a bit fishy. You add the class of the NewsLetterLibrary as observer, yet it would require a newsletterlibrary instance to my understanding as an observer is an instance listening for an event. Did you cut a few steps there creating a singleton or alike?
Hey Dreamora. I’m not sure on how the newsletter code works as it’s 3rd party library, a lot of the set up is done through other parts of the library I believe. I had some success with the displayNewsletter() functionality though.
Basically I created a new instance of a UIViewController and passed that to displayNewsletter() as an argument. This was done in AppController.mm:
RootViewController *rootViewController = [RootViewController sharedManagerInitWithNibName: @"RootViewController" bundle: nil];
//Add the RootViewController view to the main window.
[_window addSubview: rootViewController.view];
[NewsletterLibrary displayNewsletter:rootViewController];
In this case the newsletter pre-written email pops-up, great. The only problem is that because I’ve created the new UIViewController, when I cancel the email to come back to the game it just shows the blank UIViewcontroller view as opposed to going back to the view which Unity is running in :s
I tried another way which I hoped would work better. In the AppController.mm file I’ve wrapped the EAGLView that is created for Unity, with a UIViewController (found on this post here: http://forum.unity3d.com/threads/56755-Unity-plugin-to-handle-loading-native-Cocoa-UI-s-with-ease?highlight=EAGLView+UIviewController)
That allowed me to pass the Unity _window UIViewController to the displayNewsletter() function as an argument:
[NewsletterLibrary displayNewsletter: _window.rootViewController];
The only problem is that now the pre-written email window doesn’t pop-up. I’m assuming it’s behind the main Unity window as the game continues to run no problem.
Do you know if I need to call some kind of ShowWindow() command? or is it because it needs to have a xib file associated to the main Unity window?
Thanks in advance,
All the best.
Hi @macfinch ,
Did you get it working well?, as I’m facing the same scenario.
Thanks.