Metal Framework not recognized - swift

I'm currently following this tutorial on Metal/MetalKit. I have not developed for OSX before, so this is a bit new to me.
A pretty banal problem, that I'm having right from the start is, that Xcode (7.2) does not recognize, that Metal is imported. Or in a way it does, but doesn't allow me to use it's contents.
This is the content of my ViewController.swift:
import Cocoa
class ViewController: NSViewController {
#IBOutlet weak var label: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
if let device = MTLCreateSystemDefaultDevice() {
label.stringValue = "Your GPU name is:\n\(device.name!)"
} else {
label.stringValue = "Your GPU does not support Metal!"
}
}
}
I get the following error though:
When I try to import Metal explicitly, Xcode tells me that it's already implicitly imported with Cocoa.
My Deployment Target is OSX 10.11:
Why is this not working?

Related

Loading Entities from a file

I created a .reality file which i exported from Reality Composer and added to the project.
The code:
class ViewController: UIViewController {
#IBOutlet var arView: ARView!
override func viewDidLoad() {
super.viewDidLoad()
if let anchor = try? Entity.loadAnchor(named: "ARAnchorTestFile") {
arView.scene.addAnchor(anchor)
}
}
}
On devices with iOS version 13.5 or higher the app crash when the anchoring is triggered and the 3D model should be displayed.
The error:
Thread 1: EXC_BAD_ACCESS (code=1, address=0x40)
The entire project has been uploaded to this repo: https://github.com/evjand/ARAnchorTest
UPDATE: After filing a bug repport to Apple it seems like they have fixed it in the iOS 14 beta.
Seems there's a bug when reading .reality file. Use .rcproject format instead. It works.
if let anchor = try? Entity.loadAnchor(named: "AR") {
arView.scene.addAnchor(anchor)
print(anchor)
}

change to another screen after login is successful

The use case of the app I am developing is I will have login view at the initial. When user logs in with valid credentials, the user should see another view saying welcome user. I am totally beginner and I don't know much more about xcode. I see the screen navigation from storyboard but I have already done many things in xib.
Here is what I have done
SafariExtensionViewController.swift
import SafariServices
class SafariExtensionViewController: SFSafariExtensionViewController {
#IBOutlet weak var passwordMessage: NSTextField!
#IBOutlet weak var emailMessage: NSTextField!
#IBOutlet weak var message: NSTextField!
#IBOutlet weak var email: NSTextField!
#IBOutlet weak var password: NSSecureTextField!
static let shared = SafariExtensionViewController()
override func viewDidLoad() {
self.preferredContentSize = NSSize(width: 300, height: 250)
message.stringValue = ""
emailMessage.stringValue = ""
passwordMessage.stringValue = ""
}
#IBAction func userLogin(_ sender: Any) {
let providedEmailAddress = email.stringValue
let providedPassword = password.stringValue
let isEmailAddressValid = isValidEmailAddress(emailAddressString: providedEmailAddress)
self.message.stringValue = ""
emailMessage.stringValue = ""
passwordMessage.stringValue = ""
if isEmailAddressValid && providedPassword.count > 0 {
// api call is done here
// when success should show another screen
} else {
emailMessage.textColor = NSColor.red
emailMessage.stringValue = "Invalid Email"
}
}
}
here is the screenshot of xib.
Technology used
swift 4
xcode 9
not IOS its mainly for app extension
UPDATE
I am not using storyboard and also it's not IOS. I am using xib and from macos project I am trying to develop app extension which will be shown in browser as an extension.
A lot of time left from question posted, but it can be helpful for others as me (I'm porting safari legacy extension to safari app extension) and find the way with use of NSTabView with tabless style:
The use is:
Ensure to add NSObject in your storyboard from the Library:
And connect with the tabview from Outlet:
I'm rather newbie in Cocoa and Swift and if anyone knows more beautiful solution for routing, please share it with others!

CGDisplayForceToGray function replication

I am trying to programmatically switch the whole screen to grayscale mode, but the CGDisplayForceToGray function has disappeared in Swift 4(?). Is there another function or method to replicate the CGDisplayForceToGray effect or has Apple deprecated functions of this kind entirely? Thank you.
Edit: For example, the code for Desaturate has a function called CGDisplayForceToGray() which turns the whole screen into grayscale mode, in the same way, that the "Use Grayscale" checkbox in the Accessibility Preferences turns the screen into grayscale. See below (this code is from Desaturate):
import Cocoa
import Carbon
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
#IBOutlet weak var statusMenu: NSMenu!
var grayscaleStatus = false;
var preferencesWindow: PreferencesWindow!
// For MASShortcut
let callback = {}
let defaultsKey = "global_shortcut"
#IBOutlet weak var window: NSWindow!
#IBAction func quitAction(sender: AnyObject) {
NSApplication.sharedApplication().terminate(self)
}
#IBAction func toggleAction(sender: AnyObject) {
self.toggleGrayscale()
}
func toggleGrayscale() {
grayscaleStatus = !grayscaleStatus
CGDisplayForceToGray(grayscaleStatus)
}
However, when I tried to use the CGDisplayForceToGray() function myself, I get the Use of unresolved identifier error. My guess is that the CGDisplayForceToGray() function was deleted or isn't functional anymore, but I was wondering if there were other alternatives to this function that don't use AppleScript.
Edit: Resolved.

Configuring Interface Builder's new WKWebView object

As some of you may have noticed, Apple recently implemented the WKWebView (from WebKit) as an object in the Interface Builder; however, I am having difficulty with properly implementing it. I am still able to implement it by code, but implementing it via the Interface Builder has proven to be a bit of a pain.
The WKWebView appears blank on launch, and although the NSTextField appears to work when configured, the WKWebView continues to remain blank and doesn't even call upon didStartProvisionalNavigation, didCommit or didFinish when implemented (whereas, when done programatically, this continues to work).
Interface Builder Screenshot
Interface Builder Menu Options
Note: I did attempt to implement it using the WebConfiguration as well, but no luck either. Now I'm just trying to keep the code as simplistic as possible to get a better understanding as to why this is not working.
ViewController.swift
import Cocoa
import WebKit
class WindowController: NSWindowController {
override func windowDidLoad() {
super.windowDidLoad()
// Configure Window Appearance
window!.titlebarAppearsTransparent = true
window!.isMovableByWindowBackground = true
window!.title = ""
window!.backgroundColor = NSColor.white
}
}
class ViewController: NSViewController, WKUIDelegate, WKNavigationDelegate {
let myURL = "https://google.com"
#IBOutlet var webView: WKWebView!
#IBOutlet var pageTitle: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
// Load Homepage URL in WebView
webView.load(URLRequest(url: URL(string: myURL)!))
}
}
So as it turns out, the WKWebView refuses to become active while there is an entitlements file in your project. In my case, I had the App Sandbox enabled with the following settings:
com.apple.security.network.server – YES
com.apple.security.files.user-selected.read-write – YES
Once deleting the entitlements file and removing it entirely from the project, the WKWebView began to work again.

Handle browser's Notification in Swift webview application

I have very little knowledge about Swift, xCode or Objective-C but I have read/watch a lot of tutorials and my only intention is to create a webview with notification in webkit wrapper such as this Cocoa application provided by xCode.
Currently I have my code that can pretty much run when I build it.
import Cocoa
import WebKit
class ViewController: NSViewController {
#IBOutlet weak var webView: WebView!
override func viewDidLoad() {
super.viewDidLoad()
let urlString = "https://irccloud.com"
self.webView.mainFrame.loadRequest(NSURLRequest(URL: NSURL(string: urlString)!))
}
override func webView(sender: WebView!, runJavaScriptAlertPanelWithMessage message: String!, initiatedByFrame frame: WebFrame!) {
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
}
Now, if I access irccloud url directly I'm able to receive both sound and banner notification, but I'm wondering how can I make this notification able to display like native Mac app written in Swift?
This app able to do that, but it's in Objective-C https://github.com/jnordberg/irccloudapp