omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Questions about Webview

    Pythonista
    webview ui.webview
    3
    16
    8085
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Neso
      Neso last edited by Neso

      Hello,
      I am building a webview to present a specific website. There are some questions:

      1. Is it possible to have persistent web cache between sessions? The webview is for just one site so keeping cache could save traffic and time downloading resources.

      2. I make webview a subview of a UI View and present the UI view in landscape fullscreen. Since I hide title bar, I can only double-finger swipe down to close. However after I close the view, the webview seems to be still running in the background. Any way to completely close it except for closing Pythonista?

      Any help will be welcomed.
      Thanks.

      mikael 1 Reply Last reply Reply Quote 0
      • mikael
        mikael @Neso last edited by

        @Neso, here is an earlier thread about trying to specifically clear the cache - which did not seem to be possible, so you are in luck there. Actually closing down the WebView was also brought up, with no results.

        1 Reply Last reply Reply Quote 0
        • Neso
          Neso last edited by

          @mikael
          Thanks for your input. According to your case, cache is removed when quitting Pythonista. I would like to know an approach to keep cache (to a specific size for best) even after closing Pythonista.

          1 Reply Last reply Reply Quote 0
          • JonB
            JonB last edited by

            Looks like you can instantiate a NSURLCache , with a disk path pointed it a folder, and set sharedCache=your new cache.

            That would let you reuse the cache across app starts, though files may get deleted when they expire, or iOS decides disk space is low.

            1 Reply Last reply Reply Quote 0
            • Neso
              Neso last edited by

              @JonB

              Thank you.
              I have made some searches and constructed a piece of code.

              NSURLCache = ObjCClass('NSURLCache')
              shared_cache = NSURLCache.alloc().initWithMemoryCapacity_diskCapacity_diskPath_(100*1024*1024,500*1024*1024,"kc")
              NSURLCache.setSharedURLCache_(shared_cache)
              

              Tested. Look like it does not preserve and reuse caches. Is there any mistake?

              1 Reply Last reply Reply Quote 0
              • JonB
                JonB last edited by

                You might try os.path.abspath('kc'). And of be course make sure kc actually exists.

                It is possible you have to point to the caches folder.

                1 Reply Last reply Reply Quote 0
                • Neso
                  Neso last edited by

                  @JonB

                  No luck. I tried

                  os.path.abspath(os.path.dirname(__file__))+'/kc’
                  
                  

                  No file was written to the directory.

                  mikael 1 Reply Last reply Reply Quote 0
                  • mikael
                    mikael @Neso last edited by

                    @Neso, from the docs:

                    ”In iOS, path is the name of a subdirectory of the application’s default cache directory in which to store the on-disk cache (the subdirectory is created if it does not exist).”

                    To me this looks like your first attempt should have been successful. Maybe if you try a full path to the cache directory? (Need to find out what that directory is, first.)

                    1 Reply Last reply Reply Quote 0
                    • Neso
                      Neso last edited by Neso

                      @mikael

                      yep, I tried both ways. The strange thing is, I cannot find the cache directory in either cases. Answers from stackoverflow tell that the cache directory is located in “Library/Cache” but no directory or file was there. I did some searches using stash however cannot find anything related. So it is possible that cache is not created from the very beginning.

                      My code structure is like:

                      NSURLCache = ObjCClass('NSURLCache')
                      shared_cache = NSURLCache.alloc().initWithMemoryCapacity_diskCapacity_diskPath_(100*1024*1024,1200*1024*1024,'kc')
                      NSURLCache.setSharedURLCache_(shared_cache)
                      
                      w, h = ui.get_screen_size()
                      
                      v = ui.View(background_color='black')
                      wv =ui.WebView()
                      
                      ......
                      
                      

                      Missing anything?

                      JonB 1 Reply Last reply Reply Quote 0
                      • JonB
                        JonB @Neso last edited by

                        @Neso
                        UIApplication.sharedApplication().userCachesDirectory()
                        shows the caches folder. there, under com.omz-software.Pythonista3 you will find your kc folder.

                        how you force your webview to use the cache is another story maybe. what website are you trying to use?

                        1 Reply Last reply Reply Quote 0
                        • JonB
                          JonB last edited by

                          NSURLRequest = ObjCClass('NSURLRequest')
                          v=ui.WebView()
                          v.present()
                          r=NSURLRequest.requestWithURL_cachePolicy_timeoutInterval_(nsurl('https://example.com.'),2,10)
                          v.objc_instance.webView().loadRequest_(r)
                          

                          seems to do the trick. cache policy 2 is to try the cache first, then only load if the cache misses. i was able to turn off networking and run the script again, after quitting pythonista

                          one note, i think this only caches http, and images, but not javascript.

                          1 Reply Last reply Reply Quote 0
                          • Neso
                            Neso last edited by

                            @JonB

                            Thank you very much. Now I can finally locate the cache folder. There are three files: cache.db, cache.db-wal and cache.db-shm. So the cache is initialized successfully. I ran the code you provided. The size of these files are not changed. I guess the cache has not been used?

                            The website is a browser gaming site. It will download resources (mainly image) during start-up. I am trying to figure out a way to avoid repeated downloading.

                            1 Reply Last reply Reply Quote 0
                            • JonB
                              JonB last edited by

                              did you look at share_cache.currentDiskUsage() and share_cache.currentMemoryUsage()?

                              one problem is that depending on how the site handles resources, they might appear as different resources every time to the cache. i.e if there is some unique identifier included in the get requests. i think the ios cache caches things based on the request url, not resulting file -- i.e two requests that look different but return the same file might not result in a cache hit.

                              there are ways to "pre fill" the cache, if you know what those requests look like in advance. or basically you have to override some of the urlrequest methods to be smart enough to strip off the auth info, etc when lookin in the cache. i think, at least

                              1 Reply Last reply Reply Quote 0
                              • Neso
                                Neso last edited by Neso

                                @JonB

                                I would like to force writing cache to the disk so I set memorycapacity as 0 (don’t know whether this is right). The disk usage of cache is about 110k. Almost no change after several times of reloading the site.

                                Do cached requests have to be in the same domain with “NSURL”? There is a redirect during site loading. Wondering whether this is relevant.

                                Requests have the same domain. The format is like:

                                http://[fixed domain]/xxx.png?version=x.x

                                The modification date of the cache files changes so cache is actually affected. However don't know why large images do not get cached.

                                1 Reply Last reply Reply Quote 0
                                • JonB
                                  JonB last edited by

                                  Yeah, not sure if webview only caches the specific request, or all the subsequent requests. The cache policy is what is important, and not sure if we can control that otherwise.

                                  I suspect there are some delegate methods in uiwebview that involve requests that you can intercept and set the cache policy... If you can find examples on stack overflow (Uiwebview cache policy), we can implement it in Python. I didn't do a thorough search

                                  1 Reply Last reply Reply Quote 0
                                  • Neso
                                    Neso last edited by Neso

                                    @JonB

                                    I did some searches. Do you think this and this are relevant? Also maybe cachedresponseforrquest?

                                    I think the key is to find a way making nsrequest cache images. I ran share_cache.currentMemoryUsage() during the site loading. The memory usage is very small.

                                    1 Reply Last reply Reply Quote 0
                                    • First post
                                      Last post
                                    Powered by NodeBB Forums | Contributors