I have a UiViewController1 that loads an Admob interstitial, displays it when the user clicks a button, and loads another UiViewController2 when the user clicks out of the ad. However, I don't want the previous viewController to show, and the Admob interstitials are presented animated (I don't know of a way to stop this):
-(void)viewDidLoad {
[super viewDidLoad];
interstitial = [[GADInterstitial alloc] init];
interstitial.adUnitID = #"ca-app-pub-7102519969826764/7522738211";
interstitial.delegate = self;
GADRequest *request = [GADRequest request];
request.testDevices = [NSArray arrayWithObjects:GAD_SIMULATOR_ID,#"TheIDAppearingInLogs",nil];
[interstitial loadRequest:request];
}
-(void)onButtonPush {
if (interstitial.isReady) {
[interstitial presentFromRootViewController:self];
} else {
UIViewController2 *viewController = [[UIViewController2 alloc] init];
[self presentViewController:viewController animated:NO completion:nil];
}
}
-(void)interstitialDidDismissScreen:(GADInterstitial *)ad {
UIViewController2 *viewController = [[UIViewController2 alloc] init];
[self presentViewController:viewController animated:NO completion:nil];
}
I tried to load UIViewController2 before the ad is dismissed but, this happens:
Attempt to present <UIViewController1: 0x1355816a0>
on <UIViewController2: 0x135506490> whose view is not in the window hierarchy!
I guess you CANNOT modify AdMob SDK's viewController presenting behaviour,
actually what you are doing is not really common.
I would suggest to do some workaround f.e adding your viewController2 as a child instead of presenting modally in interstitialdidDismiss delegate
Related
I have a test ViewController which is embedded in a NavigationController. In test ViewController I have a WebView button. When I click on that button it navigates to SafariViewController and it's loading given URL. In that WebView when I click on "Done" button it's calling delegate method called safariViewControllerDidFinish. But that SafariViewController is not being dismissed.
Here is my code.
- (IBAction)bannerWebviewAction:(id)sender {
NSURL *urls = [NSURL URLWithString:#"http://www.google.com"];
SFSafariViewController safariVC = [[SFSafariViewController alloc] initWithURL:urls];
[self showViewController:safariVC sender:nil];
safariVC.delegate = self;
}
#pragma SFSafariViewControllerDelegate
-(void)safariViewControllerDidFinish:(SFSafariViewController *)controller {
// Done button pressed
[controller dismissViewControllerAnimated:YES completion:nil];
}
Done button is calling, view is not dismissing. I checked log for the self and controller, both are correct.
You should not say
[self showViewController:safariVC sender:nil];
You should say
[self presentViewController:safariVC animated:YES completion:nil];
SFSafariViewControllers are meant to be presented modally and will dismiss itself when done is pressed.
We're adding react native to an existing app and are having trouble pushing a native view controller on top of another native view controller housing a react native view.
The main view controller's viewDidLoad looks like this:
-(void)viewDidLoad {
...
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation moduleName: #"ListingsView" launchOptions:nil];
self.view = rootView;
}
The react view is a ListView where the TouchableHighlight's onPress is exported to this method on the same view controller:
RCT_EXPORT_METHOD(productClicked:(NSDictionary *)productDict)
{
dispatch_async(dispatch_get_main_queue(),
^{
SNKProduct *product = [[SNKProduct alloc] initWithDictionary:productDict];
SNKProductViewController *vc = [[SNKProductViewController alloc] initWithProduct:product];
[self.navigationController pushViewController:vc animated:YES];
});
}
The method is definitely called, but the SNKProductViewController is never pushed onto the screen (no log messages). I also tried modally presenting the view controller, and am getting this console message:
Warning: Attempt to present <SNKProductViewController: 0x7feadf247d10> on <SNKProductsViewController: 0x7feada5e2a20> whose view is not in the window hierarchy!
Any help would be appreciated, thanks much!
After call RCT_EXPORT_MODULE macros by Reac-Native this created new instance of this module. And after call RCT_EXPORT_METHOD method this method called from another instance of module. To solve this problem, I found only one solution yet. in called method place from js search my ViewController in Hierarchy controllers:
RCT_EXPORT_MODULE(AuthController);
- (instancetype)init {
self = [super init];
if (self) {
}
return self;
}
- (void)loadView {
self.view = [[RCTRootView alloc] initWithBundleURL:[[BCEConfigs sharedInstance] getBundleOfReactResources]
moduleName:#"AuthorizationView"
launchOptions:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
}
RCT_EXPORT_METHOD(a_registrationButton) {
[self presentRegistrationViewController];
}
- (void)presentRegistrationViewController {
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
UIViewController *mainViewController = keyWindow.rootViewController.childViewControllers[0];
BCERegistrationViewController *bceRegistrationViewController = [BCERegistrationViewController new];
dispatch_async(dispatch_get_main_queue(), ^{
[mainViewController presentViewController:bceRegistrationViewController animated:YES completion:nil];
});
}
In this example my controller, which i needed, has a place this window>rootViewController>authViewController
UPD
this i found some solution how we can get current ViewController
I think you should change the -viewDidLoad method in you main view controller like this:
-(void)viewDidLoad{
[super viewDidLoad];
......
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation moduleName: #"ListingsView" launchOptions:nil];
[self.view addSubview:rootView];
}
That is , don't set the RCTRootView as 'self.view'; make it a subview.
I've set up the Facebook iOS SDK with my app, and everything works fine. I can login using Facebook, but for some reason when I try and click my "Logout" button (top right, see image below), it doesn't do anything. I'm using a Storyboard to create my app (the login xib opens on top of my existing storyboard), and everything in my AppDelegate seems correct. What am I missing? See snippets of code below.
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.mainViewController = [[SSViewController alloc]
initWithNibName:#"SSViewController" bundle:nil];
self.navController = [[UINavigationController alloc]
initWithRootViewController:self.mainViewController];
self.window.rootViewController = self.navController;
[self.window makeKeyAndVisible];
if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
// Yes, so just open the session (this won't display any UX).
[self openSession];
} else {
// No, display the login page.
[self showLoginView];
}
return YES;
}
- (void)showLoginView
{
UIViewController *topViewController = [self.navController topViewController];
UIViewController *modalViewController = [topViewController modalViewController];
// If the login screen is not already displayed, display it. If the login screen is
// displayed, then getting back here means the login in progress did not successfully
// complete. In that case, notify the login view so it can update its UI appropriately.
if (![modalViewController isKindOfClass:[SSLoginViewController class]]) {
SSLoginViewController* loginViewController = [[SSLoginViewController alloc]
initWithNibName:#"SSLoginViewController"
bundle:nil];
[topViewController presentViewController:loginViewController animated:NO completion:nil];
} else {
SSLoginViewController* loginViewController =
(SSLoginViewController*)modalViewController;
[loginViewController loginFailed];
}
}
SSViewController.m
-(void)logoutButtonWasPressed:(id)sender {
[FBSession.activeSession closeAndClearTokenInformation];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:#"Logout"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(logoutButtonWasPressed:)];
I'm using Apple's code to show a GKGameCenterViewController:
GKGameCenterViewController *gameCenterController = [[GKGameCenterViewController alloc] init];
if (gameCenterController != nil) {
gameCenterController.gameCenterDelegate = self;
[self presentViewController:gameCenterController animated:YES completion:nil];
}
This is the text describing the code above:
Game Center UI is Displayed by Your View Controller (iOS)
The convention used by Game Kit is for one of your view controllers to present the Game Kit view controller. Your view controller acts as a delegate to the view controller it presents so that it can be informed when the player is finished looking at the presented screen. Listing 2-1 shows most common use of this pattern, which is to show the Game Center user interface. The Game Center view controller displays many different pieces of Game Center content, so most games should offer a button that brings the player to this screen, even if the game also shows Game Center content using a custom user interface.
When I use the recommended code I get to this screen (GameCenter Challenges), which is not what I want:
I have also tried this code:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"gamecenter:"]];
By using that code, I get to the screen I expected to display:
Do I misunderstand something or am I doing something wrong? Shouldn't the first piece of code bring me to the main menu? Why won't it show the leaderboards?
UPDATE
I implemented viewState as suggested by phix23:
GKGameCenterViewController *gameCenterController = [[GKGameCenterViewController alloc] init];
if (gameCenterController != nil) {
gameCenterController.gameCenterDelegate = self;
gameCenterController.viewState = GKGameCenterViewControllerStateLeaderboards;
[self presentViewController:gameCenterController animated:YES completion:nil];
}
But, it still displays the same Challenges screen, despite the fact that I want /try to display the Leaderboards screen.
The GKGameCenterViewController which is available since iOS 6 can show the leaderboards, achievements and challenges of your game center enabled application.
You can change the initial view by setting the viewState of the GKGameCenterViewController. If you don't set this property it will show the default view, which is the challenges view in your case. I guess you don't have setup any leaderboards or achievements so there is nothing to be shown.
Try using this code:
-(void)showGameCentersDefaultPage {
GKGameCenterViewController *gameCenterController = [[GKGameCenterViewController alloc] init];
if (gameCenterController != nil) {
gameCenterController.gameCenterDelegate = self;
gameCenterController.viewState = GKGameCenterViewControllerStateDefault;
[self presentViewController:gameCenterController animated:YES completion:nil];
}
}
If you want to begin with a specific type of GameCenter Leaderboard you can call the following method with your leaderboardID
- (void)showLeaderboard:(NSString*)leaderboardID {
GKGameCenterViewController *gameCenterController = [[GKGameCenterViewController alloc] init];
if (gameCenterController != nil) {
gameCenterController.gameCenterDelegate = self;
//The next three lines are the lines of interest...
gameCenterController.viewState = GKGameCenterViewControllerStateDefault;
gameCenterController.leaderboardTimeScope = GKLeaderboardTimeScopeToday;
gameCenterController.leaderboardCategory = leaderboardID;
[self presentViewController:gameCenterController animated:YES completion:nil];
}
}
For iOS 7.0, I use the following:
Display Leaderboard:
- (void)displayLeaderboard:(UIViewController *)viewController
{
GKGameCenterViewController *gameCenterController = [[GKGameCenterViewController alloc] init];
if (gameCenterController != nil) {
gameCenterController.gameCenterDelegate = self;
gameCenterController.viewState = GKGameCenterViewControllerStateLeaderboards;
[viewController presentViewController:gameCenterController animated:YES completion:nil];
}
}
Display Achievements:
- (void)displayAchievements:(UIViewController *)viewController
{
GKGameCenterViewController *gameCenterController = [[GKGameCenterViewController alloc] init];
if (gameCenterController != nil) {
gameCenterController.gameCenterDelegate = self;
gameCenterController.viewState = GKGameCenterViewControllerStateAchievements;
[viewController presentViewController:gameCenterController animated:YES completion:nil];
}
}
Note that the view controller trying to use these functions will need to pass itself (i.e. viewController param must be set to some active view controller).
Hope this helps.
Use GKGameCenterViewController and set the view state:
//Create a leaderboard view controller
GKGameCenterViewController *leaderboardViewController = [[GKLeaderboardViewController alloc] init];
leaderboardViewController.viewState = GKGameCenterViewControllerStateLeaderboards;
//Set the time scope (ex. All Time, This Week, Today) and the leaderboard ID
leaderboardViewController.timeScope = GKLeaderboardTimeScopeAllTime;
leaderboardViewController.leaderboardCategory = leaderboardID;
//Set the delegate so we can handle various actions including dismissal
leaderboardViewController.leaderboardDelegate = self;
//Present the view controller
[self presentViewController:leaderboardViewController animated:YES completion:nil];
This code will present a view controller that displays all of your games leaderboards (or the rankings if there is only one). You can also set properties such as which leaderboard to show, the time scope, delegate, etc. Also note that you can do a similar thing with achievements using the GKAchievementViewController.
The code that you provided in your question,
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"gamecenter:"]];
Launches the GameCenter app. This means that iOS will exit your app and switch to GameCenter. This could be confusing to the user. You should also avoid making the user leave your app. Instead, use the GKViewControllers which are presented modally inside of your app.
I have a View Controller which displays a table view. The VC calls another VC to display the send SMS view to the user, the code for this SMS VC is:
- (void) sendSMSWithBody: (NSString*) body andRecipients: (NSArray*) recipients
{
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
if ([MFMessageComposeViewController canSendText])
{
controller.messageComposeDelegate = self;
controller.body = body;
controller.recipients = recipients;
[[UIApplication sharedApplication].delegate.window.rootViewController addChildViewController:self];
[self presentModalViewController:controller animated:YES];
}
}
- (void) messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[self dismissModalViewControllerAnimated:YES];
[[UIApplication sharedApplication].delegate.window.rootViewController removeFromParentViewController];
}
(I know the call to sharedApplication is a bit hacky, but it will suffice for now. the rootViewController is a UINavigationController which has its root controller set to the table view controller)
I am invoking the SMS VC from the table VC like so:
- (void ) viewDidAppear:(BOOL)animated
{
static BOOL presentedSMSVC = NO;
if (!presentedSMSVC)
{
SendSMSController *sendSMS = [[SendSMSController alloc] init];
[sendSMS sendSMSWithBody:#"body"
andRecipients:[NSArray arrayWithObject:#"123456789"]];
presentedRegisterVC = YES;
}
}
The problem is that after the user sends the SMS the table view cells are not displaying.
I thought maybe I need to refresh the view/table so I added a protocol callback from the 2nd VC to the first which gets invoked when the user sends the SMS, and then within the callback call [self.tableView reloadData] But it made no difference.
So I got rid of the intermediary class and edited the table view to display the SMS view directly like this:
- (void ) viewDidAppear:(BOOL)animated
{
static BOOL presentedRegisterVC = NO;
if (!presentedRegisterVC)
{
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
if ([MFMessageComposeViewController canSendText])
{
controller.messageComposeDelegate = self;
controller.body = #"body";
controller.recipients = [NSArray arrayWithObject:#"12345678"];
[self presentModalViewController:controller animated:NO];
}
}
}
- (void) messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[self dismissModalViewControllerAnimated:NO];
}
But now the MFMessageComposeViewController doesn't dismiss (although messageComposeViewController:didFinishWithResult: does get called)
What is the problem with both approaches? Thanks
For the second variant, I changed to:
[self presentViewController: controller animated: YES completion:nil];
[self dismissViewControllerAnimated:YES completion:nil];
And that worked, haven't tried applying to the first method.
I faced a similar UI issue.
My case was: The controller, let's say controller A, in which I had written the code to present and dismiss the MFMessageComposeController, was not being used directly as the active controller rather I was using A.view as a subview over another controller, say controller B. So writing the following was distorting the view:
[self presentViewController:composeVC animated:YES completion:nil];
But then writing the following solved my issue:
[self.customTabbarNavigation presentViewController:composeVC animated:YES completion:nil];
The customTabbarNavigation, was the controller B, ans was actually the navigation controller which was active when controller A's view was visible.
Same change had to be made for dismissing the composeVC.