MBCalendarKit showing black screen in basic tabbed application - swift

I'm fairly new to swift & Xcode and I'm using a basic "tabbed" application template in XCode and I am trying to implement the MBCalendarKit to show a calendar on the first tab view. I am using Cocoapods for the MBCalendar kit. The Basic tabbed application starts with the following files:
The author of MBCalendarKit explains that :
"You can show an instance of CKCalendarView. Use this if you want to manually manage your view hierarchy or have a finer control over your calendar view." :
/*
Here's how you'd show a CKCalendarView from within a view controller.
It's just four easy steps.
*/
// 0. In either case, import CalendarKit:
#import "CalendarKit/CalendarKit.h"
// 1. Instantiate a CKCalendarView
CKCalendarView *calendar = [CKCalendarView new];
// 2. Optionally, set up the datasource and delegates
[calendar setDelegate:self];
[calendar setDataSource:self];
// 3. Present the calendar
[[self view] addSubview:calendar];
And I also followed some of the example code that was provided although most of it was in objective-c. Currently when running the program, On the first tab view, I'm just getting a black screen. How can I fix this to present the calendar itself? I have also looked at the other CalendarKit posts on stackoverflow but none seem to tackle something as specific as this.
My FirstViewController.swift (Which is basically identical to that provided by the author) :
import UIKit
import MBCalendarKit
class FirstViewController: CKCalendarViewController, CKCalendarViewDataSource {
var data : NSMutableDictionary
required init(coder aDecoder: NSCoder) {
data = NSMutableDictionary()
super.init(coder: aDecoder)
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
self.data = NSMutableDictionary()
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// Step 0 : Wire up the data source and delegate
self.delegate = self
self.dataSource = self
// Step 1 : Define some test events
let title : NSString = NSLocalizedString("Some random event", comment: "")
let date : NSDate = NSDate(day: 20, month: 12, year: 2015)
let event : CKCalendarEvent = CKCalendarEvent(title: title as String, andDate: date, andInfo: nil)
// Step 2 : Add the events to the cache array
self.data[date] = [event]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//
// MARK: - CKCalendarDataSource
//
func calendarView(calendarView: CKCalendarView!, eventsForDate date: NSDate!) -> [AnyObject]! {
return self.data.objectForKey(date) as! [AnyObject]!
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
and the AppDelegate.swift:
import UIKit
import MBCalendarKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
var viewController: CKCalendarViewController = FirstViewController(nibName: nil, bundle: nil)
self.window!.rootViewController = viewController
self.window!.makeKeyAndVisible()
return true
}
func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
Also when creating the FirstViewController() in the AppDelegate file I was a little unsure of the nibName and bundle arguments. If someone could please explain this as I tried to look it up on Apples reference documentation but it still wasn't clear, and for all I know the fact that I passe nil for both could be my issue? If anyone has experience with CalendarKit or can help me out that would be great,
Cheers

Related

How to pop up a view after application enter foreground?

I'm working on digital banking app. I need the user to be re prompted for PIN/Password after the app enter background for more than X seconds. I look up scene delegate's functions but I have no idea how can I check how long the user has been in foreground and how to popping out the view. I use AppDelegate and SceneDelegate for lifecycle
you can do this by using local notification what you have to do is following.
easy steps for new user
manage a global object for app state // if needed
add a local notification in your main view controller
post a notification from your SceneDelegate
here is the example
add observer in your main controller which always appears when app start or launch
class MainViewController: UIViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(showPopup(notification:)), name:
NSNotification.Name(rawValue: "showPinCodePopup"), object: nil)
}
// remove observer
override func viewWillDisappear(_ animated: Bool)
{
NotificationCenter.default.removeObserver(self)
}
#objc func showPopup(notification: NSNotification) {
//show your popup here
}
}
call the local notification from your SceneDelegate when app becomes active or enter in Foreground.
class SceneDelegate: UIResponder, UIWindowSceneDelegate
{
var window: UIWindow?
func sceneDidBecomeActive(_ scene: UIScene) {
NotificationCenter.default.post(name: Notification.Name(rawValue:"showPinCodePopup"), object: nil, userInfo:nil)
// Called when the scene has moved from an inactive state to an active state.
// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
}
}
Hope this solution may helps you 😊

Unity View is coming on top of current view

I am trying to integrate the Unity build for iOS into a pre-existing native app. I am using two buttons to start and stop the unity, but when I click on start, Unity view comes on the top of the current view and both the buttons disappear behind it. I used Unity 2019.1.11f1 for building the unity project.
Below is my App Delegate Code:
//
// AppDelegate.swift
// blue
//
// Created by Vikas Roy on 01/07/19.
// Copyright © 2019 MedleyOne. All rights reserved.
//
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var application: UIApplication?
#objc var currentUnityController: UnityAppController!
var isUnityRunning = false
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.application = application
unity_init(CommandLine.argc, CommandLine.unsafeArgv)
currentUnityController = UnityAppController()
currentUnityController.application(application, didFinishLaunchingWithOptions: launchOptions)
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
if isUnityRunning {
currentUnityController.applicationWillResignActive(application)
}
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
if isUnityRunning {
currentUnityController.applicationDidEnterBackground(application)
}
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
if isUnityRunning {
currentUnityController.applicationWillEnterForeground(application)
}
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
if isUnityRunning {
currentUnityController.applicationDidBecomeActive(application)
}
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
func startUnity() {
if !isUnityRunning
{
isUnityRunning = true
currentUnityController.applicationDidBecomeActive(application!)
}
}
func stopUnity() {
if isUnityRunning {
currentUnityController.applicationWillResignActive(application!)
isUnityRunning = false
}
}
}
Below is the View Controller Code:
//
// Unity3DViewController.swift
// blue
//
// Created by Vikas Roy on 25/07/19.
// Copyright © 2019 MedleyOne. All rights reserved.
//
import UIKit
class Unity3DViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
func showUnitySubView() {
if let unityView = UnityGetGLView() {
view?.insertSubview(unityView, at: 0)
}
}
#IBAction func StartUnity(_ sender: Any) {
if let appDelegate = UIApplication.shared.delegate as? AppDelegate
{
appDelegate.startUnity()
showUnitySubView()
}
}
#IBAction func StopUnity(_ sender: Any) {
if let appDelegate = UIApplication.shared.delegate as? AppDelegate
{
appDelegate.stopUnity()
}
}
}
I want buttons to always stay on the top of the unity view. Any idea where the issue can be?
In ViewContoller, where Unity is loading,
create an outlet of your button
and call this function in viewDidload() this way:
let unityView = UnityGetGLView()
unityView.addsubView(yourbutton)

How to share properties between AppDelegate and ViewController, and save before App is terminated

I have a class with properties updated in viewController. I wanted to save the properties when the app goes into background or quit using AppDelegate. I used the following codes but it appears that the properties were not passed to the AppDelegate. Furthermore the applicationWillTerminate codes did not seem to get executed.
// testClass is defined and the properties are updated in viewController, e.g
testClass.status = true // default is false
// I want to save testClass.status when the app goes into background or being terminated using the following:
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var vc = ViewController()
func applicationDidEnterBackground(_ application: UIApplication) {
print(vc.testClass.status) // prints false
//codes to save
}
// save before App is terminated
func applicationWillTerminate(_ application: UIApplication) {
print(vc.testClass.status) // this code did not get executed?
//codes to save
}
}
applicationWillTerminate is called only when a user terminates the app without switching it to background mode.
When the app is active, double press on Home button and terminate the app.
But if you switch the app to the background, and then try to terminate the app, applicationWillTerminate will not be called.
And you are creating an instance of ViewController in AppDelegate
var vc = ViewController()
If you change the testClass property in another ViewController class instance, you won't get that value here. So create a singleton class like this
class TestClass: NSObject {
static let shared = TestClass()
private override init() {
super.init()
}
var status = false
}
Now update the value in any view controller from the singleton class
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
TestClass.shared.status = true
}
}
In AppDelegate save and retrieve the value from UserDefaults
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
TestClass.shared.status = UserDefaults.standard.bool(forKey: "Status")
return true
}
func applicationDidEnterBackground(_ application: UIApplication) {
UserDefaults.standard.set(TestClass.shared.status, forKey: "Status")
}
func applicationWillTerminate(_ application: UIApplication) {
UserDefaults.standard.set(TestClass.shared.status, forKey: "Status")
}
}
Or create a computed property to save the value in UserDefaults whenever it is changed.
class TestClass: NSObject {
static let shared = TestClass()
private override init() {
super.init()
}
var status: Bool {
get {
return UserDefaults.standard.bool(forKey: "Status")
}
set {
UserDefaults.standard.set(newValue, forKey: "Status")
}
}
}
As already mentioned by others you can ignore applicationWillTerminate.
To get notified when the app goes into the background just add an observer in the view controller.
However rather than didEnterBackground I'd recommend to observe willResignActive.
Add the observer in viewDidLoad once
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(forName: UIApplication.willResignActiveNotification, object: nil, queue: nil) { notification in
// save the properties
}
}
Or if you are using multiple view controllers you can add the observer in viewWillAppear and remove it in viewDidDisappear
Side note:
Never create a view controller with the default initializer ViewController() if you are using storyboard. You'll get a brand new instance which is not the storyboard instance.

Swift Admob issue's Thread 1: EXC_BAD_INSTRUCTION

I've been working on the following problem for a couple of days now and I'm getting a little upset with it. So I have a project file https://github.com/PCmex/lift-side-memu-in-swift-3 and I successfully initiated Cocoapod with it. I followed the whole admob tutorial and did all the required steps. When I try to test the app the build is OK, but after the app launches it crashes and gives me the following information:
Screenshot for error message: Thread 1: EXC_BAD_INSTRUCTION
The log gives me the following information:
Google Mobile Ads SDK version:(GADRequest.sdkVersion())
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
Here is the app delegate.swift
import UIKit
import GoogleMobileAds
import Firebase
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
//Use Firebase library to configure APIs
FirebaseApp.configure()
GADMobileAds.configure(withApplicationID: "ca-app-pub-***")
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
And this is the ViewController.swift
import UIKit
import GoogleMobileAds
import Firebase
class ViewController: UIViewController {
#IBOutlet weak var btnMenuButton: UIBarButtonItem!
#IBOutlet weak var bannerView: GADBannerView!
override func viewDidLoad() {
super.viewDidLoad()
// Aanroepen print functie voor de Google AdMob banner
print("Google Mobile Ads SDK version:(GADRequest.sdkVersion())")
bannerView.adUnitID = "ca-app-pub-***"
bannerView.rootViewController = self
bannerView.load(GADRequest())
// Do any additional setup after loading the view, typically from a nib.
if revealViewController() != nil {
// revealViewController().rearViewRevealWidth = 62
btnMenuButton.target = revealViewController()
btnMenuButton.action = #selector(SWRevealViewController.revealToggle(_:))
// revealViewController().rightViewRevealWidth = 150
// extraButton.target = revealViewController()
// extraButton.action = "rightRevealToggle:"
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I'm pretty sure I've been installing cocoa-pod / AdMob and all prerequisites correctly. When I do all steps in a new project everything works fine. But I'm trying to understand why it doesn't work in my current project. Hope someone could point me in the right direction, thanks in advance!
The variable bannerView is an implicitly unwrapped optional. That means that it is an optional variable type. Remember that unwrapping optionals will crash if its value is nil, so normally you would do some optional chaining like if let to test before unwrapping to prevent crashing. In your case, bannerView is nil, so your application crashed. An implicitly unwrapped optional is declared by placing a ! after its type (in your case, GADBannerView!).
I suggest you to go to the storyboard (or XIB) of your controller, select your GADBannerView and go to the connections inspector
And check if there is anything in the "Referencing Outlets" section (except for "New Referencing Outlet). If there is, break the connection by clicking the X button.
Then delete the #IBOutlet weak var bannerView line in the Controller and reconnect the GADBannerView to ViewController. If there is nothing in the section, simply delete #IBOutlet weak var bannerView and reconnect the GADBannerView to ViewController

setObjectForKey method with custom objects doesn't work NSUserDefaults swift

I use the setObjectForKey method when I try to save a custom object, but when I try to retrieve it for the same key, it returns nil. I have been researching and I know that there is some issue with set custom objects to NSUserDefaults so I tried using NSKeyedArchiver class, but it still returns nil.
//
// VirtualRewardsClient.swift
// VirtualRewardsNew
//
// Created by Dhruv Mangtani on 3/14/15.
// Copyright (c) 2015 dhruv.mangtani. All rights reserved.
//
import UIKit
class VirtualRewardsClient{
class var sharedInstance: VirtualRewardsClient{
struct Static{
static var instance = VirtualRewardsClient()
}
return Static.instance
}
func getClass() -> Class{
var defaults = NSUserDefaults.standardUserDefaults()
println(defaults.objectForKey(classKey))
if let data = defaults.objectForKey(classKey) as? NSData{
//let unarc = NSKeyedUnarchiver(forReadingWithData: data)
//unarc.setClass(Class.self, forClassName: "Class")
let currentClass = NSKeyedUnarchiver.unarchiveObjectWithData(data) as Class
println("entering")
Class.sharedInstance.students = currentClass.students
Class.sharedInstance.teacher = currentClass.teacher
return currentClass
} else {
var newClass = Class()
newClass.students = [Student]()
var encodedObject: NSData = NSKeyedArchiver.archivedDataWithRootObject(newClass)
defaults.setObject(encodedObject, forKey: classKey)
defaults.synchronize()
return newClass
}
}
}
Class.swift
import Foundation
import UIKit
let classKey = "CLASS_KEY"
class Class: NSObject{
let defaults = NSUserDefaults.standardUserDefaults()
class var sharedInstance: Class{
struct Static{
static var instance: Class = VirtualRewardsClient.sharedInstance.getClass()
}
return Static.instance
}
var students:[Student] = [Student]()
var teacher = Teacher(currentClass: sharedInstance)
func addStudent(name: String, value: Int){
students.append(Student(name: name, startingPoints: value))
defaults.setObject(Class.sharedInstance, forKey: classKey)
VirtualRewardsClient.sharedInstance.getClass()
}
func addStudent(name: String){
students.append(Student(name: name))
defaults.setObject(Class.sharedInstance, forKey: classKey)
VirtualRewardsClient.sharedInstance.getClass()
}
func printClass(){
for i in students{
println("Student: \(i.name), Points: \(i.points)")
}
}
}
App Delegate:
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
/*var storyboard = UIStoryboard(name: "Main", bundle: nil)
var vc = storyboard.instantiateViewControllerWithIdentifier("StudentsNavigationController") as UIViewController
window?.rootViewController = vc
println("didFinishLaunchingWithOptions\(window?.rootViewController)")
*/
return true
}
func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
println("applicationDidEnterBackground")
}
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
println("applicationWillEnterForeground")
}
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
println("applicationDidBecomeActive")
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
change Class to VirtualRewardsClient