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.















Friday, January 11, 2013

Compile OpenCore-amr for iOS Projects


I really had a hard time compiling OpenCore-amr for my iphone application meant for ios 6.0.

Special thanks to all those people / websites who guided me and i was able to finish up the task.

Follow the steps and may be it could help you :

Step - 1 : I downloaded from http://kakola.googlecode.com/files/opencore-amr-0.1.2.tar.gz

Step - 2 : Place this folder on your macbook
               (i placed in a folder named Compile-Opencore)

Step - 3 : Open the Terminal

Step - 4 : Change the directory to move to the downloaded opencore-amr-0.1.2 directory
               $ cd /Users/Swati/Compile-Opencore/opencore-amr-0.1.2 


Step - 5 : Execute configure
              $ /Users/Swati/Compile-Opencore/opencore-amr
             -0.1.2/configure

This command will start Checking like : 
             checking for a BSD-compatible install... /usr/bin/install -c
             checking for gcc... gcc

             checking if libtool supports shared libraries... yes
             checking if gcc supports -c -o file.o... yes
             checking if gcc supports -c -o file.o... (cached) yes
etc...

Step - 6 : Call make
               $ make

Step - 7 : Call make install
               $ sudo make install 
               Password:

Step - 8 : Remember to check path of xcode
               $ xcode-select -print-path
               /Applications/Xcode.app/Contents/Developer

Step - 9 : Execute shell script
               $ /Users/Swati/Compile-Opencore/opencore-amr
              -0.1.2/build_osx.sh 

Step - 10 : configure library for armv7s
                $ <path_idz_configure.sh> <architecture> <ios_version>
                   <path_configure_opencore-amr-0.1.2>
                $ /Users/swatisrivastava/Documents/Swati/Compile
                  -Opencore/opencore-amr-0.1.2/idz_configure.sh armv7s 
                  6.0 /Users/Swati/Compile-Opencore/opencore-amr
                 -0.1.2/configure

Above step may result in errors like : 
configure: error: source directory already configured; run "make distclean" there first
make: *** No rule to make target `clean'.  Stop.
make: *** No targets specified and no makefile found.  Stop.
make: *** No rule to make target `install'.  Stop.

Step - 11 : Do cleaning if error occurs in Step - 9
                 $ make distclean

Step - 12 : Again configure for armv7s
                 $ /Users/Swati/Compile-Opencore/opencore-amr
                    -0.1.2/idz_configure.sh  armv7s 6.0 /Users/Swati/
                    Compile-Opencore/opencore-amr-0.1.2/configure

Step - 13 : Configure for armv7 also
                 $ /Users/Swati/Compile-Opencore/opencore-amr-0.1.2/
                     idz_configure.sh  armv7 6.0 /Users/Swati/Compile-Opencore
                     /opencore-amr-0.1.2/configure

Step - 14 : Configure for i386 also
                 $ /Users/Swati/Compile-Opencore/opencore-amr-0.1.2/
                     idz_configure.sh i386  6.0 /Users/Swati/Compile-Opencore/
                     opencore-amr-0.1.2/configure

Step - 15 : You can view the libraries here:

Inside your opencore-amr-0.1.2 folder : you can see 6 new folders :
build-iPhoneOS-armv7 and install-iPhoneOS-armv7
build-iPhoneOS-armv7s and install-iPhoneOS-armv7s
build-iPhoneSimulator-i386 and install-iPhoneSimulator-i386

goto install-iPhoneOs-armv7 folder >> lib folder >> here u can see library files specific to armv7 architecture namely : 
   a) libopencore-amrnb.a 
   b) libopencore-amrwb.a






Similarly u can get libraries of each architecture.

Step - 16 :  Command to create a single "Universal" .a library that contains the features of all 3 libraries
                  $ lipo -create 
                  <path_of_armv7_libopencore-amrnb.a>
                  <path_of_armv7s_libopencore-amrnb.a> 
                  <path_of_i386_libopencore-amrnb.a>
                  -output <path_universal_libopencore-amrnb.a>

                                                   &

                  $ lipo -create 
                  <path_of_armv7_libopencore-amrwb.a>
                  <path_of_armv7s_libopencore-amrwb.a> 
                  <path_of_i386_libopencore-amrwb.a>
                  -output <path_universal_libopencore-amrwb.a>

Hope it helps.
Enjoy compiling...
:)

Wednesday, July 25, 2012

Working with Action Sheets


Hi,

I got the chance to work on the action sheets.
I created a sample app before going ahead on my project.

Here is what i tried in my sample.
May be someone can be helped with this.

Here you go.....

1) Start with creating a Single View Based Application via 
    Xcode --> New --> Project --> Single View based App



2) Now Open the storyboard, and add a label and a button. clicking on this button the action sheet will be displayed.



3) Now Create an IBAction in .h file and connect the Button's "TouchUpInside"method with the IBAction.





4) Now open the .m file and inside the button's click method create an action sheet.



An important Edit for perfect display of action sheet in landscape mode
    [actionSheet showInView:[self.navigationController view]];
5) Once the action sheet is created we need to add its delegate method also so that we can perform some actions on the buttons added in the action sheet. All the buttons added in the action sheet are identified by their indices.



6) When you RUN the application

7) Click on the "Show Action Sheet" button, You can see the below output.When any button gets clicked in the action sheet then the corresponding delegate method gets fired. We have placed NSLogs inside the method to identify the particular clicks.



Enjoy.....
:)









Thursday, June 21, 2012

Horizontal Scrolling In Table View Cell


Hey guys,

I was very much eager to achieve this functionality.
I got the chance to implement this in iOS 5.0.

I decided to create a sample application before i could start on my project.
Here is the sample for you all.

Have a look...





Step -1  : Create a new Single View Based Application named Scrolling.
               
Step -2  : Open the storyboard and inside the view of your view controller add the following:
                 TableView
                 ScrollView
                 Button
                 Label


Connect the IBOutlets , Datasource , and Delegates of Tableview and the Scrollview.

Step - 3 : FileStructure : 


Step - 4 : Open ScrollingViewController.h and you can see..

a) We have created 3 arrays to store images for 3 rows. This can be done in any way depending upon
     the requirement.

#import <UIKit/UIKit.h>

@interface ScrollingViewController : UIViewController
                                     <UIScrollViewDelegate,
                                     UITableViewDelegate,
                                     UITableViewDataSource>
{
    IBOutlet UIScrollView *imageScrollView;
    IBOutlet UITableView  *tableView;
    IBOutlet UILabel      *textLabel;
    IBOutlet UIButton     *backButton;
    NSArray               *imageArray1, *imageArray2, *imageArray3;
}

@property(nonatomic,retain) UIScrollView *imageScrollView;
@property(nonatomic,retain) UITableView  *tableView;
@property(nonatomic,retain) UILabel      *textLabel;
@property(nonatomic,retain) UIButton     *backButton;

@property(nonatomic,retain) NSArray      *imageArray1;
@property(nonatomic,retain) NSArray      *imageArray2;
@property(nonatomic,retain) NSArray      *imageArray3;

@end

Step - 5 : Open ScrollingViewController.m and add following methods.

a) Inside the viewDidLoad method : We initially show the table view for list and keep the scroll view as 
    hidden for detail purpose.
b) Arrays are initialized here.
c) backButton is also Hidden which gets visible when user clicks on any image in table view.

- (void)viewDidLoad
{
    [super viewDidLoad];
   
    self.textLabel.text=@"Welcome to Table View  with horizontal Row Scroll";
    
imageArray1 = [NSArray arrayWithObjects:@"1.jpeg",@"2.jpeg",@"3.jpeg",@"4.jpeg",@"5.jpeg", nil];
imageArray2 = [NSArray arrayWithObjects:@"11.jpeg",@"12.jpeg",@"13.jpeg",@"14.jpeg",@"15.jpeg", nil];
imageArray3 = [NSArray arrayWithObjects:@"21.jpeg",@"22.jpeg",@"23.jpeg",@"24.jpeg",@"25.jpeg", nil];

    [self.imageScrollView setHidden:YES];
    [self.backButton setHidden:YES];
    [self.tableView setBackgroundColor:[UIColor clearColor]];
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
return 3;
}

d) Inside this method we create the table view cells


- (UITableViewCell *)tableView:(UITableView *)tableView 
                     cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *Identifier=@"Identifier";
    UITableViewCell *cell;
    int arraySize = 0;
    NSArray *arr;
    int offset = 0;

    cell = [self.tableView dequeueReusableCellWithIdentifier:Identifier];

    if(cell == nil)
{
cell = [self reuseTableViewCellWithIdentifier:
                                          Identifier withIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone; 
}
    switch (indexPath.row)
    {
        case 0:
            arraySize = [imageArray1 count];
            arr = [NSArray arrayWithArray:imageArray1];
            break;
        case 1:
            arraySize = [imageArray2 count];
            arr = [NSArray arrayWithArray:imageArray2];
            break;   
        case 2:
            arraySize = [imageArray3 count];  
            arr = [NSArray arrayWithArray:imageArray3];
            break;
    }

    UIScrollView *scrollview = (UIScrollView*)[cell.contentView viewWithTag:1];

    for (int i=0; i<arraySize; i++)
    {
        UIButton *btn = [[UIButton alloc] init];
        [btn setImage:[UIImage imageNamed:
                        [arr objectAtIndex:i]] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:
                        [arr objectAtIndex:i]] forState:UIControlStateHighlighted];

        btn.frame = CGRectMake(2+offset, 5, 100, 100);
        btn.tag=indexPath.row*100+i;

        [btn addTarget:nil action:@selector(buttonClicked:) 
                 forControlEvents:UIControlEventTouchUpInside];

        [scrollview addSubview:btn];
        offset = offset+110;
    }
    scrollview.contentSize = CGSizeMake(offset+10, 50);
return cell;
}

e) Here we set the frames.



-(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier 
                                       withIndexPath:(NSIndexPath *)indexPath 
{
CGRect cellRectangle;
cellRectangle = CGRectMake(0.0, 0.0, 340, 180);
UITableViewCell *cell = [[UITableViewCell alloc] 
                              initWithFrame:cellRectangle reuseIdentifier:identifier];
UIScrollView *scrollview;
cellRectangle = CGRectMake(30.0, 0.0, 280, 95);
scrollview = [[UIScrollView alloc] initWithFrame:cellRectangle];
scrollview.tag = 1;
        scrollview.scrollEnabled=TRUE;
        scrollview.autoresizingMask = TRUE;
    
[cell.contentView addSubview:scrollview];
return cell;
}


f) When user clicks on any image in the table view cell then this method gets called.
    Inside the method we hide the table view and show the scroll view and also change the label text.
    When imageScrollView meant for Detail purpose is shown then we also show the cross button on      
    top right corner to move back to the table view.
    We have used a method  : scrollRectToVisible :  This is used so that when user clicks on 2nd image
    in table view then in detail view by default the 2nd image is shown i.e the scroll view is set to scroll
    to the new position of second image.


-(void)buttonClicked:(UIButton*)btn
{
    [self.tableView setHidden:YES];
    [self.imageScrollView setHidden:NO];
    [self.backButton setHidden:NO];
    self.textLabel.text = @"Detail Scroll View";
    self.imageScrollView.scrollEnabled=TRUE;
    self.imageScrollView.autoresizingMask = TRUE; 
    
    NSArray *tempArray;
    if(btn.tag>=0 && btn.tag<=4)
        tempArray = [NSArray arrayWithArray:imageArray1];
    else if(btn.tag>=100 && btn.tag<=104)
        tempArray = [NSArray arrayWithArray:imageArray2];
    else if(btn.tag>=200 && btn.tag<=204)
        tempArray = [NSArray arrayWithArray:imageArray3];

    for (int i=0; i<[tempArray count]; i++)
    {
        UIImageView *imageView = [[UIImageView alloc] 
                      initWithImage:[UIImage imageNamed:[tempArray objectAtIndex:i]]];

        [imageView setFrame:CGRectMake(i*320+10, 5, 300, 300)];

        [self.imageScrollView addSubview:imageView];

        if(btn.tag==i || btn.tag-100==i || btn.tag-200==i)
            [self.imageScrollView scrollRectToVisible:
                                  CGRectMake(i*320+20, 5, 300, 300) animated:YES];
    }
   
    self.imageScrollView.contentSize = CGSizeMake([tempArray count]*320, 310);
    [backButton addTarget:self action:@selector(backbuttonClicked:) 
                     forControlEvents:UIControlEventTouchUpInside];
}

g) When user clicks on the cross button on the top right corner then table view is again visible and 
     scrollview is again set to hidden.


-(void)backbuttonClicked:(UIButton*)backBtn
{
    [self.tableView setHidden:NO];
    [self.imageScrollView setHidden:YES];
    [self.backButton setHidden:YES];
    self.textLabel.text=@"Welcome to Table View  with horizontal Row Scroll";
    [backBtn setHidden:YES];
}





Hope it helps.....