amirrajan / rubymotion-applied

RubyMotion documentation provided by the community. Submit a pull request to the docs for a free one year indie subscription.
Apache License 2.0
49 stars 9 forks source link

WebKit usage #16

Open amirrajan opened 7 years ago

amirrajan commented 7 years ago

documentation on how to use WebKit: https://developer.apple.com/documentation/webkit/wknavigationdelegate/1455638-webview?language=objc

This doesn't seem to work:

class AppDelegate
    def application(application, didFinishLaunchingWithOptions:launchOptions)
        rootViewController = TestVC.alloc.init
        rootViewController.title = 'auth-test'

        navigationController = UINavigationController.alloc.initWithRootViewController(rootViewController)

        @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
        @window.rootViewController = navigationController
        @window.makeKeyAndVisible

        true
    end

    class TestVC < UIViewController
        def loadView
            self.view = UIView.alloc.initWithFrame(UIScreen.mainScreen.bounds)
            view.backgroundColor = UIColor.whiteColor

            config = WKWebViewConfiguration.alloc.init
            @web_view = WKWebView.alloc.initWithFrame(UIScreen.mainScreen.bounds, configuration:config)
            @web_view.navigationDelegate = self
            url = NSURL.URLWithString("https://www.paypal.com")
            @request = NSMutableURLRequest.requestWithURL(url)

            view.addSubview(@web_view)

            @web_view.loadRequest(@request)
        end

        def webView(webView, decidePolicyForNavigationAction:action, decisionHandler:handler)
            handler.call(WKNavigationActionPolicyAllow)
        end

        def webView(webView, didReceiveAuthenticationChallenge:challenge, completionHandler:handler)
            NSLog("challenge: #{challenge.class}")
            handler.call(NSURLSessionAuthChallengePerformDefaultHandling, nil)
        end
    end
end
amirrajan commented 7 years ago

checking the sender does indeed prevent the crash, but a bit more info: I check the challenge class as a workaround for https://forums.developer.apple.com/thread/72469 , in which the challenge type may or may not be a NSURLAuthenticationChallenge and correctly expose any methods, namely protectionSpace. Even trying challenge.respond_to?(:protectionSpace) instead produces the error: Objective-C stub for message `respond_to?:' type `@@:@@' not precompiled. Make sure you properly link with the framework or library that defines this message.