MASShortcut no such module - swift

I've been try use MASShortcut and following the instructions there I've added it using cocoapods. I then added it to my <proj>-Bridging-Header.h file and imported it in my main swift file but I keep getting the error
No such module 'MASShortcut'
Here's my setup:
AppDelegate.swift:
import Cocoa
import Carbon
import MASShortcut
var kShortCut: MASShortcut!
#IBOutlet weak var shortcutView: MASShortcutView!
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
#IBOutlet weak var window: NSWindow!
override func awakeFromNib() {
... omitted ...
}
override func viewDidLoad() {
super.viewDidLoad()
shortcutView.shortcutValueChange = { (sender) in
let callback: (() -> Void)!
if self.shortcutView.shortcutValue.keyCodeStringForKeyEquivalent == "k" {
self.kShortCut = self.shortcutView.shortcutValue
callback = {
print("K shortcut handler")
}
} else {
callback = {
print("Default handler")
}
}
MASShortcutMonitor.sharedMonitor().registerShortcut(self.shortcutView.shortcutValue, withAction: callback)
and my Podfile:
target 'myapp' do
# Comment this line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
pod 'MASShortcut', '~> 2'
# Pods for myapp
target 'myappTests' do
inherit! :search_paths
# Pods for testing
end
end
and finally proj-Bridging-Header.h:
#import <Cocoa/Cocoa.h>
#import <MASShortcut/Shortcut.h>

Here's what the AppDelegate should sort of look like. Note the absense of any import MASShorcut.
import Cocoa
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var kShortCut: MASShortcut!
#IBOutlet weak var window: NSWindow!
#IBOutlet weak var shortcutView: MASShortcutView!
override func awakeFromNib() {
shortcutView.shortcutValueChange = { (sender) in
let callback: (() -> Void)!
if self.shortcutView.shortcutValue.keyCodeStringForKeyEquivalent == "k" {
self.kShortCut = self.shortcutView.shortcutValue
callback = {
print("K shortcut handler")
}
} else {
callback = {
print("Default handler")
}
}
MASShortcutMonitor.shared().register(self.shortcutView.shortcutValue, withAction: callback)
}
}
}
The bridging header should look like this:
#ifndef Testin_Bridging_Header_h
#define Testin_Bridging_Header_h
#import <MASShortcut/Shortcut.h>
#endif /* Testin_Bridging_Header_h */
Shortcut.h imports all the other header files. Testin was the name of my app (to test of course)
Some other tips:
Make sure you set the bridging header in your application's build settings.
Try building from the MASShortcut scheme if it doesn't build the framework initially.
The AppDelegate doesn't have lifecycle events, you need to use either NSViewController or NSWindowController subclasses to use lifecycle methods (ie viewDidLoad).

Related

Mac OSX app not loading URL in Webkit View

I am trying to load a URL inside a Webkit View in a Mac OX app using Swift.
This is how my code looks like right now:
import Cocoa
import AppKit
import Foundation
import CoreData
import WebKit
class ViewController: NSViewController, WKUIDelegate {
#IBOutlet weak var rightBox: NSView!
#IBOutlet weak var leftBox: NSView!
#IBOutlet var myWebView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
self.view.wantsLayer = true
// Do any additional setup after loading the view.
}
override func viewWillAppear() {
leftBox.layer?.backgroundColor = NSColor.blue.cgColor
rightBox.layer?.backgroundColor = NSColor.red.cgColor
let myURL = URL(string: "https://www.apple.com")
let myRequest = URLRequest(url: myURL!)
myWebView.load(myRequest)
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
}
When I run the code, I only see blank white box.
I have tried placing the url loading code both in the viewDidLoad as well as viewWillAppear. Same result.
I am trying to figure out what I might be doing wrong.
Any suggestions?
Thanks
Have you enabled outgoing connections for your application?
Had it been an http:// URL you'd also have to turn on NSAllowsArbitraryLoads in your Info.plist.

Parse PFUser not registering subclass

I am trying to use Parse PFUser in a software for OSX desktop. When I try to use it PFUser.query() it gives a message: Failed to set (contentViewController) user defined inspected property on (NSWindow): The class PFUser must be registered with registerSubclass before using Parse.
It is happening without registering the class.
I tried it registering the class this way: PFUser.registerSubclass() but it still doesn't work.
I will use the default PFUser without adding any fields to it, so I don't need to create a custom class to be my PFUser.
I tried to use PFUser.enableAutomaticUser() without success
Code below:
AppDelegate.swift
import Cocoa
import Parse
import Bolts
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
let APP_ID = "app_id"
let CLIENT_KEY = "client_key"
let SERVER = "https://parseserver.com/"
func applicationDidFinishLaunching(_ aNotification: Notification) {
PFUser.registerSubclass()
let configuracaoParse = ParseClientConfiguration {
$0.applicationId = self.APP_ID
$0.clientKey = self.CLIENT_KEY
$0.server = self.SERVER
}
Parse.initialize(with: configuracaoParse)
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}
ViewController.swift
import Cocoa
import Parse
class ViewController: NSViewController {
#IBOutlet weak var emailTextField: NSTextField!
#IBOutlet weak var senhaSecureTextField: NSSecureTextField!
override func viewDidLoad() {
super.viewDidLoad()
contaUsuarios()
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
#IBAction func entrarButtonClicked(_ sender: NSButton) {
}
func contaUsuarios() {
let query = PFUser.query()
query?.countObjectsInBackground(block: {
(count, error) -> Void in
let numeroUsers = Int(UInt32(count))
if numeroUsers > 0 {
}
print(numeroUsers)
})
}
}
Reading some content on the internet I discovered that in OSX the ViewController is launched before the AppDelegate finishes loading, so I initialized the Parse connection and subclassing in the ViewController's viewDidLoad instead of AppDelegate and it is working just fine.

private func: Missing argument for parameter #1 in call

Well, here's another 'Missing argument for parameter #1 in call' issue. (Seems Apple could do a better job naming its errors :-p )
In my class, I'm calling a private function libraryVisibility(), and on that line I get a Missing argument for parameter #1 in call error on compilation. Don't really understand why.
Anyhow, here's the code:
import Cocoa
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var libraryState: Bool = libraryVisibility()
func applicationDidFinishLaunching(aNotification: NSNotification) {
…
}
private func libraryVisibility() -> Bool {
…
// dostuff and return boolean
return true
}
}
You can't call instance functions in the default initialiser of a property
You can either make your libraryVisibility() function a class function:
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var libraryState : Bool = AppDelegate.libraryVisibility()
private class func libraryVisibility() -> Bool {
let homeUrl = NSURL(string: NSHomeDirectory())
let libraryUrl = NSURL(string: "Library", relativeToURL: homeUrl)
return libraryUrl!.hidden
}
}
Or you could make your libraryState property a lazy property:
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
lazy var libraryState : Bool = self.libraryVisibility()
private func libraryVisibility() -> Bool {
let homeUrl = NSURL(string: NSHomeDirectory())
let libraryUrl = NSURL(string: "Library", relativeToURL: homeUrl)
return libraryUrl!.hidden
}
}
The property will then get initialised the first time you use it.
Mike Buss has a nice usage guide on how to use lazy variables.
Ok, I have solved it, but not entirely sure why this is working:
Giving the libraryState variable a default value and setting it inside applicationDidFinishLaunching seems to do the trick:
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var libraryState: Bool = false
func applicationDidFinishLaunching(aNotification: NSNotification) {
libraryState = libraryVisibility()
}
}

Implement WatchConnectivity in an iOS App that supports iOS 8.0

My current iOS App supports iOS 8 and greater. I decided to implement a Watch App and I want to have watch and iOS app comunicate via WatchConnectivity. Let's say that I want to build a class to handle the connectivity and this class is a WCSessionDelegate. To be able to include the class in my project I've to wrap it into the #available(iOS 9.0,0) tag and that's ok...but, how can I keep track of an instance of this class in the AppDelegate (or in any other class)???
I cannot write something like
class AppDelegate: UIResponder, UIApplicationDelegate {
#available(iOS 9.0,0)
var connectivityHandler: ConnectivityHandler?
How can I use these class in a project that should support also iOS 8?!
Edit----
I've found this solution but it doesn't sound really "good". I create a dynamic property and dependently on the current system I return a instance or just nil... the property should obviously be of type AnyObject to be a valid iOS8 parameter...
var _connectivityHandler: AnyObject?
var connectivityHandler: AnyObject? {
get {
if _connectivityHandler != nil {
return _connectivityHandler
}else{
if #available(iOS 9.0, *) {
_connectivityHandler = ConnectivityHandler()
} else {
_connectivityHandler = nil
}
return _connectivityHandler
}
}
set(value) {
_connectivityHandler = value
}
}
You can move all the WatchConnectivity functions in your connectivityHandler to an extension of that class and mask that with the #availability API:
import UIKit
import WatchConnectivity
class ConnectivityHandler: NSObject {
}
#available(iOS 9.0, *)
extension ConnectivityHandler: WCSessionDelegate {
func initSession() {
if WCSession.isSupported() {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}
}
}
If you do that you can add this class to your AppDelegate and it also compiles in iOS 8:
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let connectivityHandler = ConnectivityHandler()
....
}

ios 8 and Swift: call a function in another class from view controller

I want to touch a button in ViewController and run a simple function in a custom class. No parameters needed. Code is basically:
class MyClass: UIView {
//init function is here
//other setup stuff
...
func SetFocus() {
//I want to set the camera focus to infinity.
var error: NSErrorPointer = nil
var pos: CFloat = 1.0
captureDevice!.lockForConfiguration(error)
captureDevice!.setFocusModeLockedWithLensPosition(pos, completionHandler: nil)
captureDevice!.unlockForConfiguration()
}
}
and in ViewController
import UIKit
class ViewController: UIViewController {
#IBOutlet var myClass: MyClass!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func myButton(sender: UIButton) {
myClass.SetFocus()
}
}
I've tried everything I can think of (new at Swift) and the app either crashes or the function I'm calling doesn't run. Everything else in MyClass works OK.
Try to delete the #IBOutlet from before MyClass.
var myClass : MyClass!
You can also try this:
var myClass : MyClass! = MyClass()
From the looks of it, however, everything you are doing in setFocus() in MyClass, can easily be recreated in the ViewController.
Try this
#IBAction func myButton(sender: UIButton) {
var ClassViewController = self.storyboard.instantiateViewControllerWithIdentifier("StoryboardID") as! UIViewController
ClassViewController.MyFunction()
}