Html form in UIwebview send back to ios? - objective-c

i am trying to pass information between an HTML form and ios i tryed javascrip but it isn't working?
// Init Web View
UIWebView *webView;
webView = [[UIWebView alloc] initWithFrame:boundsRect];
webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
webView.scalesPageToFit = YES;
webView.contentMode = UIViewContentModeCenter;
webView.multipleTouchEnabled = NO;
webView.userInteractionEnabled = YES;
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
[self addSubview:webView];
NSString *testArrayString = [webView stringByEvaluatingJavaScriptFromString:#"testString"];
NSArray *testArray = [testArrayString componentsSeparatedByString:#"#"];
NSLog(#"%#", testArray);
[webView release];
return self;
any ideas?
----EDIT----
Ok guys if u only want to make data from a form pass to a ios app you only do this to your webview
[webView setDelegate:self];
Then you can implement this methods in your program marks
like:
-(void)webViewDidFinishLoad:(UIWebView *) webView {
//implement code to do here
}
if you want to capture a for just do a webview shouldStartWithRequest like this:
-(BOOL)webView:(UIwebView *) webView shouStartWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigation{
//implement code to do afeter the user click the form don't forget to return YES or NO because it is a BOOL
}
that is how i solve it any question please do tell me i try my best to help

To pass data from UIWebView and back to your Objective-C runtime, you need to implement UIWebViewDelegates webView:shouldStartLoadingWithRequest: and pass your form-data as parameters in the URL. If you craft all your URL's with a scheme like f.ex. my-app://?parameter1=value1&parameter2=value2, you can easily filter out the URLs the web view should not load, and return NO.
EDIT:
Added example-code.
- (void)initWebView
{
UIWebView *webView;
webView = [[UIWebView alloc] initWithFrame:boundsRect];
webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
webView.scalesPageToFit = YES;
webView.contentMode = UIViewContentModeCenter;
webView.multipleTouchEnabled = NO;
webView.userInteractionEnabled = YES;
webView.delegate = self;
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = request.URL;
if ([url.scheme isEqualToString:#"http://"]) {
return YES;
}
else if ([url.scheme isEqualToString:#"my-app://"]){
// Process data from form
return NO;
}
return YES;
}
EDIT2: You are releasing the web view in your initialisation-code. This prevents the delegate-method from being called when you are loading the form-url
EDIT3: See this question for more code-examples: Invoke method in objective c code from HTML code using UIWebView

Related

Objective C - When tapping the tab bar item again, how to reload web view again?

I really need help please. Initially, by tapping on a tab bar item, I will load a webview on Gym VC user will then scroll down the web view
What I am trying to accomplish is when user tap on the same tab bar item again, the webView will reload again. I managed to call the method handleRefresh from MyTabBarController But the method doesn't do anything at all. Any guidance is much appreciated.
Currently I have the following code
MyTabBarController.m
#import "GymNavigationController.h"
#import "Gym.h"
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if([viewController isKindOfClass:[GymNavigationController class]]){
Gym *myGym = [[Gym alloc] init];
[myGym handleRefresh:nil];
}
}
Gym.m
#import "Gym.h"
#import "AppDelegate.h"
#import "GymDet.h"
#import <QuartzCore/QuartzCore.h>
#interface Gym () {
AppDelegate *appDelegate;
NSString *sURL, *sRefresh;
}
#end
#implementation Gym
#synthesize webView;
- (void)viewDidLoad {
[super viewDidLoad];
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
sURL = appDelegate.gURL;
sURL = [sURL stringByAppendingString:#"/apps/gym.asp?"];
NSLog(#" The sURL to be load for the current page : %# ", sURL);
sRefresh = sURL;
[defaults setObject:sRefresh forKey:#"txtRefresh"];
[defaults synchronize];
NSURL *url = [NSURL URLWithString:sURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView setDelegate:(id<UIWebViewDelegate>)self];
[webView loadRequest:urlRequest];
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:#selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
[webView.scrollView addSubview:refreshControl];
}
- (void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//[self handleRefresh:nil];
[[self navigationController] setNavigationBarHidden:YES animated:YES];
}
-(void)viewDidDisappear:(BOOL)animated{
[[self navigationController] setNavigationBarHidden:NO animated:YES];
}
- (void)handleRefresh:(UIRefreshControl *)refresh {
//--- Call the stored txtMemCode, something like session ---
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
sRefresh = [defaults objectForKey:#"txtRefresh"];
NSURL *url = [NSURL URLWithString:sRefresh];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
[refresh endRefreshing];
}
First i would like to suggest why you're initiating a new Gym object every time tabbar is tapped you should take a reference of the Gym object and initialise it if its nil then process ahead with the reference, but it maybe your requirement and if it is i would like to suggest you to create a BOOL variable lets say isUpdateRequire and when you're instantiating your viewcontroller assign isUpdateRequire = true. Then in the end of view did load or view will appear (according to your need) check
if isUpdateRequire {
[self handleRefresh:nil]
}
Alternatively you can create protocols in your webviewcontroller and assign its delegate to your tabbarcontroller and fire the delegate method when ever required.
and if you don't want the method to call when you come back to vc simply put this condition in viewWillAppear
if self.isMovingToParentViewController {
[self handleRefresh:nil]
}

UIWebView stringByEvaluatingJavaScriptFromString returns empty string

I know there have been couple of discussion already on this topic, but I am trying a different approach here.
I am trying to first inject a variable into the Webview, then try to get the output by calling a method on that variable. Here's how it goes:
webview = [[UIWebView alloc] initWithFrame:window.bounds];
[window addSubview:webview];
[webview loadHTMLString:#"<html><head><script>(function() {var myVariable = window.myVariable = {};"
#"myVariable.getVersion = function() {return 1;}})();"
#"</script></head></html>"
baseURL:nil];
webview.delegate = self;
- (void)webViewDidFinishLoad:(UIWebView *)webView {
if ([[webView stringByEvaluatingJavaScriptFromString:#"typeof(myVariable);"] isEqualToString:#"object"]) {
NSLog(#"version=%#",[webView stringByEvaluatingJavaScriptFromString:#"myVariable.getVersion());"]);
} else {
NSLog(#"failed the test.");
}
}
The output:
version=

UIWebView & NavigationController crashes while loading

I have very weird problem
I build a simple app with UIWebView and NavigationController but the problem is when the seconds page is loading and I go back to the first page and visited a link the APP crashes and the console says nothing
Here is my code on finish loading
-(void)webViewDidFinishLoad:(UIWebView *)aWebView {
NSString *str = [aWebView stringByEvaluatingJavaScriptFromString:#"document.title"];
self.navigationItem.title = str;
[navigationActivity stopAnimating];
}
and this for the web view should start loading
if(navigationType == UIWebViewNavigationTypeOther)
{
NSURL *url2 = [request URL];
NSString *URLStr = [url2 absoluteString];
RootViewController* viewController = [[RootViewController alloc] init];
NSString *holder = [self getQueryStringInner:URLStr];
[self getQueryString:URLStr];
if([holder length] != 0 && [flag length] != 0 && !facebook )
{
appDelegate.title =#"Title";
appDelegate.query = queryString;
appDelegate.url = holder;
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
return NO;
}
}
I am really new to Objective-c and iOS development so any help will be appreciated
Any Help
Seems that the necessary step is to call [webView stopLoading] in your viewWillDisappear. Apparently the web view delegate was sending the webViewDidFinishLoading method message to a deallocated instance. Glad you got it worked out.

UIView with rotation issues

I've a sample project, where I'm creating an custom UIViewController.
#import "ViewController.h"
#implementation ViewController
#synthesize webViews = _webViews;
#synthesize webView = _webView;
- (void)setWebView:(UIWebView *)webView {
if (webView!=_webView) {
[self.webView removeFromSuperview];
_webView = nil;
_webView = webView;
[self.view addSubview:self.webView];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com/"]]];
}
}
- (IBAction)newWebView:(id)sender {
self.webView = [self.webViews objectAtIndex:1];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
UIWebView *webView1 = [[UIWebView alloc] initWithFrame:self.view.bounds];
webView1.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
UIWebView *webView2 = [[UIWebView alloc] initWithFrame:self.view.bounds];
webView2.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.webViews = [NSArray arrayWithObjects: webView1, webView2, nil];
self.webView = [self.webViews objectAtIndex:0];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
#end
Two UIWebView's are created inside the viewWillAppear:animated and stored in an array -- and than the first UIWebView is added to the subview of self.view. When a button in the navigation bar is pressed, the first UIWebView will be removed from the subview and the next one will be added instead.
The problem is, that if I'm running the application in landscape (both iPhone and iPad) after the second UIWebView is added, the WebView is not filling the entire screen. Why?
You can download the small sample project here: http://uploads.demaweb.dk/WebView.zip.
I dont know why but this inside your action method will do the trick...
UIWebView *newWebView=[[UIWebView alloc] init];
newWebView=[self.webViews objectAtIndex:1] ;
newWebView.frame=self.view.frame;
self.webView = newWebView;
hope this helps.. :/

NetworkActivity indicator in webview

ive got a view based application containg a button clicking the button takes you to a web view..wat i want is the netwokActivityindicator to swirl..after finish loading of the web page i want the networkActivity indicator to disappear ...below is the code
-(IBAction)Button4
{
WebViewGive *newEnterNameController4 = [[WebViewGive alloc] initWithNibName:#"WebViewGive"
bundle:[NSBundle mainBundle]];
[[self navigationController] pushViewController:newEnterNameController4 animated:YES];
[newEnterNameController4 release];
}
- (void)viewDidLoad {
[super viewDidLoad];
[UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
self.urlAddress1 = #"http://www.youtube.com/user/stevenandkeirabanks";
self.url1 = [NSURL URLWithString:urlAddress1];
self.requestObj1 = [NSURLRequest requestWithURL:url1];
[webViewAnnouncements loadRequest:requestObj1];
}
if i m to use the network activity indicator below the
[webViewAnnouncements loadRequest:requestObj1];
I m jst getting a flickr and not the swrling of the activity indicator
The simplest way to do this is to add this line after you instantiate your UIWebView.
[webView setDelegate:self];
Now you will call webViewDidStartLoad: and the entire method should look like this.
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}
You can stop the network activity indicator as follows -
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}