UIWebView – embed web content

There is a possibility that we have to show web content in our application. The UIWebView class helps us to do exactly that. To do this, simply create a UIWebView object, attach it to a window, and send a request to load web content. We can also use this class to move back and forth in the history of web pages and you can even set some web content properties programmatically.

Now let's create a UIWebView class that displays www.rubymotion.com in our view.

Update about_controller.rb with the following code:

def submit_button
  @visitButton = UIButton.buttonWithType(UIButtonTypeRoundedRect)
  @visitButton.frame = [[80,10],[180,37]]
  @visitButton.setTitle("Visit", forState:UIControlStateNormal)
  @visitButton.setTitle("You have clicked me", forState:UIControlStateHighlighted)
  @visitButton.addTarget(self, action:"load_some_view", forControlEvents:UIControlEventTouchDown)
  view.addSubview(@visitButton)
end
def load_some_view
      @my_web_view = UIWebView.alloc.initWithFrame([[0,100],[320,220]])
      @my_web_view.delegate = self
      @my_web_view.scalesPageToFit = "YES"
      view.addSubview(@my_web_view)
      url = NSURL.URLWithString("http://www.rubymotion.com")
      request = NSURLRequest.requestWithURL(url)
      @my_web_view.loadRequest(request)
end

Now let's understand the code. Take the following line:

url = NSURL.URLWithString("http://www.rubymotion.com")

Here, NSURL.URLWithString tells our application that the text passed is a web address or a URL, which is now an NSURL object called url.

request = NSURLRequest.requestWithURL(url)

NSURLRequest.requestWithURL processes the url variable passed as a request. It is now a request object called request.

@my_web_view.loadRequest(request)

Finally, we load the request into the WebView class, which we have named @my_web_view.

Let's fire up the terminal and run our application as follows to see the results:

$rake

The following screenshot shows the output of the preceding command:

UIWebView – embed web content
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.14.118