tvOS GameController GCController.controllers在启动后会在什么时候填充?

时间:2015-11-08 12:30:00

标签: objective-c swift tvos apple-tv gamecontroller

我的应用程序启动后,我正在尝试阅读GCController.controllers()数组,以了解在应用启动时哪些控制器已连接到AppleTV。但是GCController.controllers().count在我的初始UIViewController调用viewDidAppear之后的某个时刻才为0。有没有人知道你可以检查GCController.controllers()知道它已经填充了当前连接的控制器的权威点?

我知道需要使用;

注册控制器连接通知
NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleControllerDidConnectNotification:"   , name: GCControllerDidConnectNotification   , object: nil)

但这是在发布之后的。首先,我需要知道哪些已经连接。有人知道吗?

2 个答案:

答案 0 :(得分:1)

您可以在startWirelessControllerDiscoveryWithCompletionHandler上致电viewDidLoad,然后查看GCController.controllers()上的viewWillAppear,该 let zomatoKey = "<Your API Key>" let centerLatitude = 19.06558, centerLongitude = 72.86215 let urlString = "https://developers.zomato.com/api/v2.1/search?&lat=\(centerLatitude)&lon=\(centerLongitude)"; let url = NSURL(string: urlString) if url != nil { let request = NSMutableURLRequest(URL: url!) request.HTTPMethod = "GET" request.addValue("application/json", forHTTPHeaderField: "Accept") request.addValue(zomatoKey, forHTTPHeaderField: "user_key") let task = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: {(data, response, error) -> Void in if error == nil { let httpResponse = response as! NSHTTPURLResponse! if httpResponse.statusCode == 200 { do { if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary { if let restaurants = json["restaurants"] as? [NSDictionary] { for rest in restaurants { var searchResult = [String:AnyObject?]() let restaurant = rest["restaurant"] as! NSDictionary print(restaurant["id"] as? NSString) print(restaurant["average_cost_for_two"] as? NSNumber) print(restaurant["cuisines"] as? String) print(restaurant["url"] as? String) print(restaurant["thumb"] as? String) if let location = restaurant["location"] as? NSDictionary { print(location["address"] as? String) print(location["city"] as? String) print((location["latitude"] as? NSString).doubleValue) print((location["longitude"] as? NSString).doubleValue) } print(restaurant["menu_url"] as? String) print(restaurant["name"] as? String ) print(restaurant["phone_numbers"] as? String) if let user_rating = restaurant["user_rating"] as? NSDictionary { print(user_rating["aggregate_rating"] as? NSString) print(user_rating["rating_color"] as? String) print((user_rating["votes"] as? NSNumber)).doubleValue) } } } } } catch { print(error) } } } }) task.resume() } 似乎适用于我刚刚完成的游戏应用。

文档:

  

应用完成启动后,即操作系统   自动创建连接的控制器列表。打电话给   controllers类方法,用于检索GCController对象的数组   对于所有连接的控制器接下来,使用这些对象进行配置   控制器或读取控制器的输入。如果没有   连接控制器或您在应用程序时调用此方法   启动时,数组将为空

答案 1 :(得分:0)

GCController将为每个控制器生成GCControllerDidConnectNotification通知,包括在启动之前连接到设备的控制器。如果您未收到已连接控制器的通知,请确认以下内容:

  • 仔细检查它是否已配对并开启
  • 确保它是MFi控制器。