【发布时间】:2017-09-02 17:28:21
【问题描述】:
我在我的 AppDelegate 中成功地从远程通知负载 userInfo 接收数据。现在我正在尝试从 Storyboard 推送到视图控制器,将数据传递给它,并使用 NSNotificationCenter 更新变量但收到错误:
Fatal error: unexpectedly found nil while unwrapping an Optional values
在viewDidLoad。谢谢参观!
(ObjC) AppDelegate.m
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)())completionHandler
{
NSDictionary *userInfo = response.notification.request.content.userInfo;
NSDictionary *roomKey = [[[userInfo objectForKey:@"custom"] objectForKey:@"a"] objectForKey:@"roomKey"];
NSLog(@"%@", roomKey);
// Successfully prints correct roomKey, now need to pass to view controller and update variable.
[[NSNotificationCenter defaultCenter] postNotificationName:@"pushToFollowers" object:nil userInfo:roomKey];
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"main" bundle:nil];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"audience"];
self.window.rootViewController = viewController;
// line below is involved in optional crash
[self.window makeKeyAndVisible];
}
(斯威夫特 3)VC:
import UIKit
import SocketIO
import IHKeyboardAvoiding
@objc class AudienceViewController: UIViewController {
@IBOutlet weak var previewView: UIView!
@IBOutlet weak var statusLabel: UILabel!
var room: Room!
var player: IJKFFMoviePlayerController!
let socket = SocketIOClient(socketURL: URL(string: Config.serverUrl)!, config: [.log(true), .forcePolling(true)])
var overlayController: LiveOverlayViewController!
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(self.notificationArrived(_:)), name: NSNotification.Name(rawValue: "pushToFollowers"), object: nil)
// Optional error occurs on next line
let urlString = Config.rtmpPlayUrl + room.key
player = IJKFFMoviePlayerController(contentURLString: urlString, with: IJKFFOptions.byDefault())
player.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
player.view.frame = previewView.bounds
previewView.addSubview(player.view)
player.prepareToPlay()
socket.on("connect") {[weak self] data, ack in
self?.joinRoom()
}
}
// function isn't firing
@objc func notificationArrived(_ notification: NSNotification) {
print(notification.userInfo!)
if let custom = notification.userInfo?["custom"] as? [String: Any] {
if let a = custom["a"] as? NSDictionary {
if let roomKey = a["roomKey"] as? String {
print(roomKey)
room.key = (roomKey) as String
}
}
}
}
}
【问题讨论】:
标签: ios objective-c swift