Python 123
Python 123

Reputation: 89

Python app framework create webview inside of app

I need to embed a webview inside my kivy app or with some other python app framework that supports this for both android and IOS. I am looking to also save cookies for log in so users do not have to log in more than once. Is this possible or should I look for another way to accomplish this? Thanks for any responses in advance!

Upvotes: 3

Views: 8340

Answers (1)

NameKhan72
NameKhan72

Reputation: 727

Unfortunately there is no "universal" method to do this. It should however still be possible. There unfortunately isn't a kivy "native" method either.

Android:

For android you can use webview-android:

from kivy.uix.widget import Widget
from kivymd.app import MDApp
from webview import WebView
from kivy.lang.builder import Builder
from kivymd.uix.button import MDFlatButton
from kivymd.uix.screen import MDScreen

Builder.load_string("""
<MyWebView>
    MDFlatButton:
        text: "Push"
        pos_hint: {"center_x": .5, "center_y": .4}
        on_press: root.Push()
""")

class MyWebView(MDScreen):
    def Push(self):
        WebView("https://www.google.com")


class MyWebApp(MDApp):
    def build(self):
        return MyWebView()


if __name__ == '__main__':
    MyWebApp().run()

Furthermore, you can use jnius to access the java classes, that would do this normally:

import kivy                                                                                     
from kivy.app import App                                                                        
from kivy.lang import Builder                                                                   
from kivy.utils import platform                                                                 
from kivy.uix.widget import Widget                                                              
from kivy.clock import Clock                                                                    
from jnius import autoclass                                                                     
from android.runnable import run_on_ui_thread                                                   
                                                                                                
WebView = autoclass('android.webkit.WebView')                                                   
WebViewClient = autoclass('android.webkit.WebViewClient')                                       
activity = autoclass('org.kivy.android.PythonActivity').mActivity                              
                                                                                                
class Wv(Widget):                                                                               
    def __init__(self, **kwargs):                                                               
        super(Wv, self).__init__(**kwargs)                                                      
        Clock.schedule_once(self.create_webview, 0)                                             
                                                                                                
    @run_on_ui_thread                                                                           
    def create_webview(self, *args):                                                            
        webview = WebView(activity)                                                             
        webview.getSettings().setJavaScriptEnabled(True)                                        
        wvc = WebViewClient();                                                                  
        webview.setWebViewClient(wvc);                                                          
        activity.setContentView(webview)                                                        
        webview.loadUrl('http://www.google.com')
                                                                                                
class ServiceApp(App):                                                                          
    def build(self):                                                                            
        return Wv()                                                                             
                                                                                                
if __name__ == '__main__':                                                                      
    ServiceApp().run()

iOS:

Unfortunately I don't own an iOS device, so I can not test any of this.

The kivy-ios module seems to contain methods for accomplishing this:

import ios
url = "http://www.google.com"
ios.IOSWebView().open(url, width, height)

Another solution would be to use pyobjus to access the Objective-C classes, that would normally implement the webview on iOS. I don't want to paste untested code, so I suggest you check out Michael Galaxy and Julez' answers on the bottom of this google group.

Upvotes: 5

Related Questions