Mac OSX app not loading URL in Webkit View - swift

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.

Related

Can't see anything in Web.load(URLRequest(url: URL(string: "https://google.com")!))

I am trying to make a macOS app using Xcode 12.4 , that opens webpages. When I try to use Web.load(URLRequest(url: URL(string: "https://google.com")!)) , I see an empty window.
I use a WKWebView in Main.storyboard
Here is the ViewController.swift code:
import Cocoa
import WebKit
let webView = WKWebView()
class ViewController: NSViewController {
#IBOutlet weak var Search: NSSearchField!
#IBOutlet weak var Web: WKWebView!
var web: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
Web.load(URLRequest(url: URL(string: "https://google.com")!))
// Do any additional setup after loading the view.
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
}
I just found the simplest solution to this problem: remove the App Sandbox! More information about this here: https://developer.apple.com/forums/thread/92265

Particles Speed Up When Recording A Video Using ARVideoKit

We have an ARKit & SceneKit app that records videos using ARVideoKit pod (link). Inside our scene we have a fire particle. Fire is playing at a slow speed. However, when you start recording a video the fire particle speeds up. I can't figure out why the fire particle is speeding up. Please see the video (here) to see the issue.
Here's a sample project which you can use to test the issue: Project
I would appreciate if anyone can explain why this is happening.
Edit 1: Below is my ViewController Code
import UIKit
import ARKit
import SceneKit
import ARVideoKit
class ViewController: UIViewController, ARSCNViewDelegate {
#IBOutlet var sceneView: ARSCNView!
#IBOutlet weak var photoButton: UIButton!
#IBOutlet weak var videoButton: UIButton!
#IBOutlet weak var takeVideoButtonPressedLabel: UILabel!
// ARVideoKit Variables
var recorder : RecordAR?
var takenImage: UIImage?
var takenVideoAtURL : URL?
let recordingQueue = DispatchQueue(label: "recordingThread", attributes: .concurrent)
var arTrackingConfig = ARWorldTrackingConfiguration()
override func viewDidLoad() {
super.viewDidLoad()
sceneView.delegate = self
sceneView.scene = SCNScene(named: "art.scnassets/fire.scn")!
self.resetTracking()
}
func resetTracking() {
arTrackingConfig = ARWorldTrackingConfiguration()
arTrackingConfig.isLightEstimationEnabled = true
sceneView.session.run(arTrackingConfig, options: [.resetTracking,
.removeExistingAnchors])
}
}
// MARK: - ARVideoKit Implementation
extension ViewController {
#IBAction func takePhoto(_ sender: UIButton) {
// Do Nothing
}
#IBAction func takeVideo(_ sender: UIButton) {
setupCamera()
startRecording()
}
func setupCamera() {
recorder = RecordAR(ARSceneKit: sceneView)
recorder?.prepare(arTrackingConfig)
}
func startRecording() {
takeVideoButtonPressedLabel.isHidden = false
recordingQueue.async {
self.recorder?.record(forDuration: 5) { path in
self.takenVideoAtURL = path
DispatchQueue.main.async {
self.takeVideoButtonPressedLabel.isHidden = true
}
}
}
}
}
Edit 2: Added sample project for testing.
While getting the image from SCNRenderer using snapshot method pass 0 as time , this way force rendering will not happen and animations will be smoother. In your case particles will not speed up

Q: How to change language PDF page indicator?

I want to change the language of the page indicator, it should be german and not english. If you scroll inside of a pdf file it shows you "1 of 6" that's what i want to change.
Any ideas?
import UIKit
import WebKit
class PDFViewController: UIViewController {
var pdfName: String?
#IBOutlet weak var wkWebView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let pdfFilePath = Bundle.main.url(forResource: pdfName!, withExtension: "")
let urlRequest = URLRequest.init(url: pdfFilePath!)
wkWebView.load(urlRequest)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Xcode Project -> Info -> Localizations add German
was the answer!
Thanks #Retterdesdialogs

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.

Why isn't my WKWebView resizing properly inside my subview?

I am creating a minimal WebKit browser for a Mac OS App using the following Swift code to load the new WKWebView into an Custom View Object.
When I run the App the web page loads correctly and fills the window, however if the window is resized the subview does not resize with the window.
Any Help would be most appreciated.
import Cocoa
import WebKit
import AppKit
#NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate {
#IBOutlet weak var window: NSWindow!
#IBOutlet weak var customView: NSView!
func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application
var url = NSURL(string:"http://www.google.com/")
var request = NSURLRequest(URL:url!)
var theWebView:WKWebView = WKWebView(frame: customView.bounds)
self.customView.autoresizingMask = NSAutoresizingMaskOptions.ViewWidthSizable | NSAutoresizingMaskOptions.ViewHeightSizable
self.customView.addSubview(theWebView)
theWebView.loadRequest(request)
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
}