iOS Question How to use WKWebview.createPdf

Status
Not open for further replies.

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub Process_Globals
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page
    Private xui As XUI
    Private WebView1 As WebView
End Sub

Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.RootPanel.LoadLayout("Page1")
    NavControl.ShowPage(Page1)
    WebView1.LoadUrl("https://www.google.com")
End Sub

Sub Button1_Click
    If App.OSVersion < 14 Then
        Log("not supported")
    Else
        Dim no As NativeObject = Me
        no.RunMethod("CreatePDF:", Array(WebView1))
    End If
End Sub

Sub PDF_Created (Data As Object, Success As Boolean)
    If Success Then
        Dim no As NativeObject = Data
        Dim arr() As Byte = no.NSDataToArray(Data)
        File.WriteBytes(File.DirLibrary, "1.pdf", arr)
        WebView1.LoadUrl(xui.FileUri(File.DirLibrary, "1.pdf"))
    End If
End Sub

Private Sub Page1_Resize(Width As Int, Height As Int)
    
End Sub

#if OBJC
#import <WebKit/WebKit.h>
- (void) CreatePDF:(WKWebView*)wv {
    if (@available(iOS 14, *)) {
        
    [wv createPDFWithConfiguration:nil completionHandler:^(NSData* data, NSError* error) {
            if (error != nil) {
                NSLog(@"Error %@", error);
            }
            [self.bi raiseUIEvent:nil event:@"pdf_created::" params:@[data, @(error == nil)]];
        }
    ];
    }
}
#End If
 
Upvote 0

b4x-de

Active Member
Licensed User
Longtime User
Thanks for this example. When trying to use it in my project it failed fails to compile on a local mac builder. Currently I'm using mac builder in Version 6.80 that does not contain any WebKit related files in its lib folder. Where can I find the referenced WebKit/WebKit.h and other files needed to compile this example on a local mac builder?

Thanks,
Thomas
 
Upvote 0
Status
Not open for further replies.
Top