ARkit in Webview

You don’t see it where?

In your screenshot.

The file needs to be copied to the Xcode project as I say above…
have a re read above.

don’t follow, sorry.

Where should it be in the code?

In your sample Mtest is the rcproject right? So I replaced Mtest with Owl

I Built, as you said, and that fixed the eror

Screenshot 2020-06-15 at 23.26.12

Yes, I had that, it was a matter of building. Now, on Hype,

code is

window.webkit.messageHandlers.openStore.postMessage(‘Owl.rcproject’);

Is that correct?

Oh boy..

That will do, but you don't need to put ‘Owl.rcproject’ it is not doing anything in this case.


So are you saying you are not getting the error now in Xcode.

Correct, after I built, the error was gone

1 Like

So as a test, I have a simple Hype file with a button that calls upon the postMessage function, and I have it

window.webkit.messageHandlers.openStore.postMessage();

But nothing happens

Can you paste the postMessage code in the xcode that you are using ( in full )

//
//  WKWebViewController.swift
//  AlphaPod
//
//  Created by JULIAN DAMY on 3/1/20.
//  Copyright © 2020 JULIAN DAMY. All rights reserved.
//

import UIKit
import WebKit
import SafariServices
import RealityKit

class ViewController: UIViewController {
    
    @IBOutlet var arView: ARView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Load the "Real" scene from the "owl" Reality File
        let boxAnchor = try! Owl.loadReal()
        
        
        // Add the box anchor to the scene
        arView.scene.anchors.append(boxAnchor)
    }
}



class FullScreenWKWebView: WKWebView {
    override var safeAreaInsets: UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    }
}
class WKWebViewController: UIViewController, WKNavigationDelegate ,WKScriptMessageHandler  {
    
    var webView: WKWebView!
    
    //-- declare a message name to use later
       let openStore = "openStore"
       let printPage = "print"
       
       //-- declare a your html file  name to use later ( do not include the .html )
       let htmlName = "index"
    
    override var prefersStatusBarHidden: Bool {
        return true
    }
    
    // MARK: - WKUserContentController:  -> webkit Posted messages handler delegate //-- handles messages from hype page
    
    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
       
          if message.name ==  "openStore" {
          print("\(message.body )")
          //-- Get the message passed to app
        let mssg    =  message.body as! String
         
             // anURL  = URL.init(string: message.body as! String)!
            if let url =  URL(string: "\(mssg)")  {
            //URL(string: "\(message.body)") else {
                let config = SFSafariViewController.Configuration()
                                   config.entersReaderIfAvailable = true
                
                let vc = SFSafariViewController(url: url, configuration: config)
                present(vc, animated: true)
                
                  return //be safe
              }
              
              
            
          }
    //MARK: - PRINT
        if message.name == printPage{
            
            let webviewPrint = webView.viewPrintFormatter()
            let printInfo = UIPrintInfo(dictionary: nil)
            printInfo.jobName = "page"
            printInfo.outputType = .general
            printInfo.orientation = .landscape
            let printController = UIPrintInteractionController.shared
            printController.printInfo = printInfo
            printController.showsNumberOfCopies = false
            printController.printFormatter = webviewPrint
            printController.present(animated: true, completionHandler: nil)
            
        }
        
    //MARK: - ARKIT
        
        if message.name ==  "openStore" {
        print("\(message.body )")
        //-- Get the message passed to app
          let storyBoard: UIStoryboard = UIStoryboard(name: "realv", bundle: nil)
          let balanceViewController = storyBoard.instantiateViewController(withIdentifier: "realv") as! ViewController
        self.present(balanceViewController, animated: true, completion: nil)
        }
        
        
    }

    
    override func viewDidLoad() {
        super.viewDidLoad()

        let wconfiguration = WKWebViewConfiguration()
        
        /* false = Play Media playback with native device player ; true =  inline */
          wconfiguration.allowsInlineMediaPlayback = false
         
         let wcontroller = WKUserContentController()
        
//MARK: WKUserContentController Init :  We must add the webkit scripts Posted messages we expect to get from the Hype Page to the controller
          wcontroller.add(self, name:  openStore )
          wcontroller.add(self, name:  printPage )
         
         //  attach controller, config to WKWebview
         wconfiguration.userContentController = wcontroller;

        
        /* MARK: Does not require user interaction for .ie sound auto playback  */
        
        wconfiguration.mediaTypesRequiringUserActionForPlayback = []
        wconfiguration.setValue(true, forKey: "_allowUniversalAccessFromFileURLs")
        
        webView = FullScreenWKWebView(frame: .zero, configuration: wconfiguration)

        
        let url = Bundle.main.url(forResource: "index", withExtension: "html", subdirectory: "website")!
        webView.loadFileURL(url, allowingReadAccessTo: url)
        var request = URLRequest(url: url)
        request.addValue("*", forHTTPHeaderField: "Access-Control-Allow-Origin")
        webView.load(request)
        
        webView.navigationDelegate = self
        webView.scrollView.delegate = self
        
        webView.scrollView.bounces = false
        webView.scrollView.isScrollEnabled = false
        webView.isOpaque = false
        
        ///Start in hidden mode
        webView.isHidden = true
        
        ///Add as subView, so we have original views bacground in blue
        view.addSubview(webView)
        
        ///Anchor webKit to view parrent view's edges
        webView.translatesAutoresizingMaskIntoConstraints = false
        webView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        webView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        webView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        webView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    }
}

//MARK: - Disable zooming

extension WKWebViewController: UIScrollViewDelegate {
    func viewForZooming(in: UIScrollView) -> UIView? {
        return nil
    }
    
    func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
        scrollView.pinchGestureRecognizer?.isEnabled = false
    }
}

//URL


//MARK: - Delete double tap

extension WKWebViewController {
    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        //deleteDoubleTap(web: webView)
    }
    
    func deleteDoubleTap(web: WKWebView) {
        for subview in web.scrollView.subviews {
            for recognizer in subview.gestureRecognizers ?? [] {
                if recognizer.isKind(of: UITapGestureRecognizer.self) {
                    let tapRecognizer = recognizer as! UITapGestureRecognizer
                    if tapRecognizer.numberOfTapsRequired == 2 && tapRecognizer.numberOfTouchesRequired == 1 {
                        subview.removeGestureRecognizer(recognizer)
                    }
                }
            }
        }
    }
}

//MARK: - WebKit loaded

extension WKWebViewController {
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        ///On webKit finished loading, unhide it
        webView.isHidden = false
    }
}

And the button works fine if I have a URL link included, opens a Browser, just doesn’t work without an external link

So… you remember why I got you to make two message names.
openStore and printPage

Well you are doing what you did before. Please go back and read why I said do that…
You are nearly there…

I do remember. I use printPage function to print and openStore to open external URLs in Safari. And both work.

Should I create another message different from those two?

IN any case, I did, created a message “ARView”,

//MARK: - ARKIT

    if message.name ==  "ARView" {
    print("\(message.body )")
    //-- Get the message passed to app
      let storyBoard: UIStoryboard = UIStoryboard(name: "realv", bundle: nil)
      let balanceViewController = storyBoard.instantiateViewController(withIdentifier: "realv") as! ViewController
    self.present(balanceViewController, animated: true, completion: nil)
    }

and in Hype I have

window.webkit.messageHandlers.ARView.postMessage();

But still no go

correct.

Did you add it to the wcontroller

Yes, I have this under messages to declare later:

let externalLink = “externalLink” (used to be openStore)
let printPage = “print”
let internalAR = “internalAR”

then this under userContentController:

if message.name == “internalAR” {
print("(message.body )")
//-- Get the message passed to app
let storyBoard: UIStoryboard = UIStoryboard(name: “realv”, bundle: nil)
let balanceViewController = storyBoard.instantiateViewController(withIdentifier: “realv”) as! ViewController
self.present(balanceViewController, animated: true, completion: nil)
}

then this underWKUserContentController Init:

wcontroller.add(self, name: externalLink )
wcontroller.add(self, name: internalAR )
wcontroller.add(self, name: printPage )

then in Hype the button’s function is:

window.webkit.messageHandlers.internalAR.postMessage();

Ah, it had to have some url (anything) at the end, so now it’s

window.webkit.messageHandlers.internalAR.postMessage(‘http://www.alphapodapp.com’);
Then it works!!

Thank you so much for all your help and infinite patience. Now, there’s one tiny little thing. It looks like this on my iPad:

However, on my iPhone 7 plus it uses the entire screen. The problem with that is that I can’t swipe down to dismiss it, like I can on the iPad.

This is on the Phone

1 Like

It looks like that is to do with orientation.
Seems the modal anchor is not right.
You would need to research how to deal with it

Remember I am not giving you the exact coding to use. Just showing it can be done and a starting point

You know. I should have also suggested this before but my initial thought was side tracked by you not wanting it to open in Safari.

You can also use the method I showed you using the SFSafariViewController code.
And send it the external URL of the owl.reality file via hype psotMessage…

This will open it within the app and give you all the AR controls on board.
It will work in any orientation.

Doing that may suffice for you and lower the learning curve you may need to do it with the ar internal file.