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.


    SFSafariViewController as a ui.View

    Pythonista
    2
    9
    2606
    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.
    • coltonboyd
      coltonboyd last edited by

      I was wondering if any of you that are good with objc_util could help me out.

      I would like to present a SFSafariViewController as a ui.View from a ui.View that is already presented.

      Or be able to add SFSafariViewController as a subview for the already presented view.

      Thx in advance.

      cvp 2 Replies Last reply Reply Quote 0
      • cvp
        cvp @coltonboyd last edited by

        @coltonboyd see here

        1 Reply Last reply Reply Quote 0
        • cvp
          cvp @coltonboyd last edited by

          @coltonboyd or

          import ui
          from objc_util import *
          
          import os
          	
          #========= SFSafariViewController to allow Safari extension in WebView
          def safariViewControllerDidFinish_(_self, _cmd, _controller):
          	#print('SafariViewControllerDidFinish_')
          	SFSafariViewController = ObjCInstance(_controller)
          	#....... still close view problems
          	try:
          		SFSafariViewController.uiview.close()
          	except Exception as e:
          		SFSafariViewController.uiview().close()
          
          methods = [safariViewControllerDidFinish_,]
          protocols = ['SFSafariViewControllerDelegate']
          try:
          		MySFSafariViewControllerDelegate = ObjCClass('MySFSafariViewControllerDelegate')
          except:
          	MySFSafariViewControllerDelegate = create_objc_class('MySFSafariViewControllerDelegate', methods=methods, protocols=protocols)
          
          @on_main_thread	
          def MySFSafariViewController(url, w=None, h=None, local=None, popover_location=None, reader=False):
          	uiview = ui.View()
          	uiview.background_color = 'white'
          	if w and h:
          		uiview.frame = (0,0,w,h)
          		uiview.present('sheet',hide_title_bar=True)
          	else:
          		uiview.present('fullscreen',hide_title_bar=True)
          		
          	# SFSafariViewController only accepts http: or https:,  not file:
          	# thus, in this case, we need to create a local server
          	ns_url = nsurl(url)
          
          	SFSafariViewControllerConfiguration = ObjCClass('SFSafariViewControllerConfiguration').alloc().init()
          	#
          	SFSafariViewControllerConfiguration.setEntersReaderIfAvailable_(reader)
          	SFSafariViewController = ObjCClass('SFSafariViewController').alloc().initWithURL_configuration_(ns_url,SFSafariViewControllerConfiguration)
          	#print(dir(SFSafariViewController))
          		
          	# Use new delegate class:
          	delegate = MySFSafariViewControllerDelegate.alloc().init()
          	SFSafariViewController.delegate = delegate		
          	SFSafariViewController.setModalPresentationStyle_(3)
          	SFSafariViewController.uiview   = uiview		# used by delegate
          	
          	objc_uiview = ObjCInstance(uiview)
          	SUIViewController = ObjCClass('SUIViewController')
          	vc = SUIViewController.viewControllerForView_(objc_uiview)	
          
          	vc.presentViewController_animated_completion_(SFSafariViewController, True, None)
          
          if __name__ == '__main__':	
          	url = 'https://www.eqsl.cc/qslcard/Index.cfm'
          	url = 'http://www.kanisette.com/2018/06/roule-au-saumon-et-courgettes.html'
          	MySFSafariViewController(url,600,500)	
          
          
          1 Reply Last reply Reply Quote 0
          • coltonboyd
            coltonboyd last edited by

            @cvp to clarify I am attempting to run this is the most current Xcode Template. Your code is good starting place but when I tried it was just a blank white screen. The code I am trying to incorporate is:

            from objc_util import *
            
            SFSafariViewController = ObjCClass('SFSafariViewController')
            
            @on_main_thread
            def open_in_safari_vc(url, tint_color=None):
                vc = SFSafariViewController.alloc().initWithURL_(nsurl(url))
                if tint_color is not None:
                    vc.view().tintColor = tint_color
                app = UIApplication.sharedApplication()
                root_vc = app.keyWindow().rootViewController()
                root_vc.presentViewController_animated_completion_(vc, True, None)
                vc.release()
            
            open_in_safari_vc('http://apple.com', UIColor.orangeColor())
            

            Basically I have an ui.View called App that is presented from the point of where the iOS app starts. When clicking on a button view on this App class I want it to open a SFSafariViewController with the designated url given to the open_in_safari_vc function.

            When I attempt this in the Xcode Template I get this error:

            [Presentation] Attempt to present <SFSafariViewController: 0x7fbfa7a98000> on <PYAViewController: 0x7fbfa4408e80> (from <PYAViewController: 0x7fbfa4408e80>) whose view is not in the window hierarchy.

            I hope this clarifies what I'm trying to do.

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

              Also to add, I added the code above to a blank main.py file in a blank Xcode project and it worked just fine. I think it has something to do with the App ui.View that I am presenting.

              cvp 1 Reply Last reply Reply Quote 0
              • cvp
                cvp @coltonboyd last edited by

                @coltonboyd I can't help you with Xcode....

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

                  For anybody following this thread I figured how to present SFSafariViewController from an already presented view:

                  #######################################################################
                  
                  # Necessary imports. 
                  from objc_util import *
                  
                  #######################################################################
                  
                  class WebView():
                    @on_main_thread
                    def __init__(self,url,uiview,bar_tint_color,control_tint_color):
                      self.safari_view_controller = ObjCClass("SFSafariViewController").alloc().initWithURL_(nsurl(url))
                      self.safari_view_controller.preferredBarTintColor = bar_tint_color
                      self.safari_view_controller.preferredControlTintColor = control_tint_color
                      self.view_controller = ObjCClass('UIViewController').viewControllerForView_(ObjCInstance(uiview))
                  
                    @on_main_thread  
                    def present(self):
                      self.view_controller.presentViewController_animated_completion_(self.safari_view_controller,True,None)
                  cvp 1 Reply Last reply Reply Quote 0
                  • cvp
                    cvp @coltonboyd last edited by

                    @coltonboyd sorry, I didn't correctly understood your post πŸ™„

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

                      @cvp it’s all good. Your code gave me a good starting place and led me to the code I got so thank you!

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