Tuesday, December 17, 2013

To & Fro : UIWebView - HTML - JS - UIViewcontrollers


Greeting to @ll,

Finally got a chance to interact with web , views and scripts. Found the work interesting so thought of sharing a small sample with @ll. 

Hoping you find it useful. :)

Lets start

1. Create a new Single view based app with this structure : 



2. Now add a sample.html file as shown in app structure
    This HTML file contains an input text field and a click Me button


3. Also add a test.js as shown in app structure with following dummy functions

  • Here you can receive parameters in JS functions
  • Also you can returns values to the iOS native View Controllers, check the window.location statement in function areaOfCircle.




4.  Now open your storyboard : 

On your view controller add a UIWebView and 2 buttons with text viz : 
  • Pass arguments from VC to JS and alert displayed from JS
  • Pass arguments from JS to VC and alert displayed from VC




5. ViewControllers
  • Remember to create an IBOutlet for UIWebView and IBActions for the button Actions



  • stringByEvaluatingJavaScriptFromString : This method helps us to call JS methods directly from the View Controller
  • You can easily pass arguments too.






6. In in ViewController.m 
  • Load html file in View Did load



7. Way to receive data from JS and view on the iOS ViewController [VC]

  • This method gets invoked when from JS when window.location method is called.
  • It reloads the web view, here we can check the url and accordingly perform actions.


  • Private method of the ViewController is called to to show data received from JS in form of an alert





********* Some points to remember with Javascript code *********


  • Make sure your JS & HTML file is not shown in the Compile Sources Listing. 
  • If it exists there remove it.
  • Instead add the file in Copy Bundle Resources.



  • Place both the file in same location on the drive.
  • Try to run the HTML file on browser just to ensure methods get called there.
  • Also last important thing, whenever you try to add JS and HTML file make sure you put check on Create folder references for any added folders instead of Create groups for any added folders


Enjoy.
C u @ll again :)

Monday, December 16, 2013

Create Menu Items For UIWebView


Hi all,

I know this is one of the simplest task for many still i thought to write down a small sample on it.
Hoping it may help someone..

1. Create a new Single view Application



2.  Save the project



3.  This is the basic App structure :



4.  Open StoryBoard and add a UIWebView from the toolbar



5. Make WebView Outlet Connections




6. Add an IBOutLet for the UIWebView

7.  InViewController.m add following : 





8. Now run the code and see the work : 

9. Now try to select any part and you can see the menu items created by you.