如何从Game Center登录中隐藏“欢迎回来”横幅

时间:2011-09-07 15:29:00

标签: ios game-center

我一直在通过网络和Apple文档查找这些信息,但找不到答案...你知道当玩家验证时是否有办法隐藏Game Center中的“Welcome back,playerName”消息?

谢谢!

5 个答案:

答案 0 :(得分:1)

是的,可以从您的应用程序中以编程方式禁止Game Center Welcome横幅,至少在iOS 7下。我的方法基于一些观察:

  1. 横幅在UIApplication中作为额外的UIWindow呈现。
  2. 此窗口似乎总是出现在索引1处。(假设您的应用只使用一个窗口。)
  3. 横幅在iPad上为66像素高,在iPhone上为64像素。
  4. 横幅包含游戏中心图标的42x42像素子视图。
  5. 众所周知,横幅很可能会出现。 (即在启动时创建GKLocalPlayer对象以测试身份验证的几秒钟内。)
  6. 因此,您可以在几秒钟内重复轮询应用程序的窗口,等待额外的窗口出现。 (键值观察可能是执行此操作的“正确”方法,但我很懒。)当窗口显示时,测试它是否包含如上所述的子视图层次结构,这表明它可能是游戏中心横幅。如果是,则将窗口的alpha设置为0.就是这样。

    以下是我的应用中完成此操作的一些代码。我在尝试验证本地播放器后立即调用此方法,它会调用自己几秒钟,直到找到(并隐藏)横幅,否则超时:

    - (void)suppressGCBanner:(id)object {
        int osVersion = [[[UIDevice currentDevice] systemVersion] intValue];
        if (osVersion != 7) return;  // only tested against iOS 7
    
        static int iter = 0;    // try for 4 seconds, typically takes about one second for banner to appear
        static int origWindowCount = 0;
    
        NSArray* windows = [UIApplication sharedApplication].windows;
        if (origWindowCount == 0) origWindowCount = (int)[windows count];
    
        BOOL ipad = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
        float bannerHeight = ipad ? 66.0f : 64.0f;  // GC banner has height 66 on iPad, 64 on iPhone
    
        if ([windows count] > origWindowCount) {
            NSLog(@"suppressGCBanner: found extra window, testing");
    
            UIWindow* window = [windows objectAtIndex:1]; // in my testing, the GC banner is always at index 1
    
            for (UIView* view in [window subviews]) {
                CGRect frame = view.frame;
                NSLog(@"subview size: %f, %f", frame.size.width, frame.size.height);
    
                if (frame.size.height != bannerHeight) continue;
    
                for (UIView* subview in [view subviews]) {
                    CGRect frame = subview.frame;
                    NSLog(@"sub-subview size: %f, %f", frame.size.width, frame.size.height);
    
                    if (frame.size.width == 42.0f && frame.size.height == 42.0f) { // Game Center icon is 42x42
                        NSLog(@"found GameCenter banner: hiding. iter = %i", iter);
    
                        window.alpha = 0.0f; // make the window invisible!
    
                        return;
                    }
                }
            }
        }
    
        if (++iter > 200) {
            NSLog(@"suppressGCBanner: timeout, bailing");
            return;
        }
    
        // ____ otherwise recurse
        [self performSelector:@selector(suppressGCBanner:) withObject:nil afterDelay:0.02f];
    }
    

    偶尔你会在隐藏横幅之前在屏幕顶部看到单像素线闪烁,但一般来说这种方法似乎运行得很好。使用风险自负,享受!

答案 1 :(得分:1)

这是Swift的缩短版本:

//Call it right after create this object:
let localPlayer = GKLocalPlayer.localPlayer()
suppressGCBanner(0, originalWindowCount: UIApplication.sharedApplication().windows.count)

////////////////////////////////////////////////////////////////////////

static func suppressGCBanner(iteration: Int, originalWindowCount: Int) {
    let windows = UIApplication.sharedApplication().windows

    if windows.count > originalWindowCount {
        let window = windows[1]

        if window.respondsToSelector("currentBannerViewController") || window.respondsToSelector("bannerSemaphore") {
            print("Found banner, killing it \(iteration)")
            window.hidden = true
            return
        }
    }

    if iteration > 200 {
        print("suppressGCBanner: timeout, bailing")
        return
    }

    runThisAfterDelay(seconds: 0.02, after: {
        suppressGCBanner(iteration + 1, originalWindowCount: originalWindowCount)
    })
}

static func runThisAfterDelay(seconds seconds: Double, after: () -> ()) {
    let time = dispatch_time(DISPATCH_TIME_NOW, Int64(seconds * Double(NSEC_PER_SEC)))
    dispatch_after(time, dispatch_get_main_queue(), after)
}

答案 2 :(得分:0)

不,没有。如果不对设备进行越狱并重新布线,则无法更改iOS服务(GameCenter所属的服务)的行为。 出于好奇 - 你为什么需要那个?

答案 3 :(得分:0)

你可以退出游戏中心,如果这是你的选择。大多数游戏都是基于反应的,并且在3秒钟内屏蔽15%的屏幕通知显然不会被欣赏。

答案 4 :(得分:0)

这是适用于swift3的工作版本:

NameOfYourClass.suppressGCBanner(0, originalWindowCount: UIApplication.shared.windows.count)

static func suppressGCBanner(_ iteration: Int, originalWindowCount: Int) {

    let windows = UIApplication.shared.windows

    if windows.count > originalWindowCount {
        let window = windows[1]
        if window.responds(to: Selector("currentBannerViewController")) || window.responds(to: Selector("bannerSemaphore")) {
            print("Found banner, killing it \(iteration)")
            window.isHidden = true
            return
        }
    }

    if iteration > 200 {
        print("suppressGCBanner: timeout, bailing")
        return
    }

    runThisAfterDelay(seconds: 0.02, after: {
        suppressGCBanner(iteration + 1, originalWindowCount: originalWindowCount)
    })
}

static func runThisAfterDelay(seconds: Double, after: @escaping () -> ()) {
    DispatchQueue.main.asyncAfter(deadline: .now() + seconds) {
        after()
    }
}
相关问题