objective c - Call Javascript Function From iOS Webview in Angularjs App - Stack Overflow

My problem is that when I try to call a javascript function that exists in my AngularJS app from within

My problem is that when I try to call a javascript function that exists in my AngularJS app from within a UIWebView that function is not recognized. When I call the function in a typical html structure the function is recognized as expected. Example provided below:

OBJECTIVE-C:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // CODE GOES HERE
    _webView.delegate = self;

    // LOAD THE CHABACORP HOMEPAGE WITH INITIAL UIWEBVIEW
    NSString *fullURL = @"";
    NSURL *url = [NSURL URLWithString:fullURL];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:requestObj];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{

    [_webView stringByEvaluatingJavaScriptFromString:@"testing()"];

}

ANGULARJS CONTROLLER:

'use strict';

angular.module('testing.controllers', [])

.controller('Testing', function($scope) {

    function testing() {
        alert('testing');
    }

});

BASIC HTML (WORKS WITH CURRENT OBJECTIVE-C):

<!DOCTYPE HTML>
<html>

<body>

    <SCRIPT>
    function testing() {
        alert('testing');
    }
    </SCRIPT>

</body>

</html> 

My problem is that when I try to call a javascript function that exists in my AngularJS app from within a UIWebView that function is not recognized. When I call the function in a typical html structure the function is recognized as expected. Example provided below:

OBJECTIVE-C:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // CODE GOES HERE
    _webView.delegate = self;

    // LOAD THE CHABACORP HOMEPAGE WITH INITIAL UIWEBVIEW
    NSString *fullURL = @"http://testing.";
    NSURL *url = [NSURL URLWithString:fullURL];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:requestObj];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{

    [_webView stringByEvaluatingJavaScriptFromString:@"testing()"];

}

ANGULARJS CONTROLLER:

'use strict';

angular.module('testing.controllers', [])

.controller('Testing', function($scope) {

    function testing() {
        alert('testing');
    }

});

BASIC HTML (WORKS WITH CURRENT OBJECTIVE-C):

<!DOCTYPE HTML>
<html>

<body>

    <SCRIPT>
    function testing() {
        alert('testing');
    }
    </SCRIPT>

</body>

</html> 
Share Improve this question asked Mar 14, 2014 at 13:56 rjm226rjm226 1,2031 gold badge11 silver badges21 bronze badges 1
  • 1 You need to read up on and understand scope in javascript – JoseM Commented Mar 14, 2014 at 14:21
Add a ment  | 

1 Answer 1

Reset to default 6

It's because your testing function isn't globally available. It's only available inside your Testing controller function. To make it globally available, you need to modify the controller to be something like this:

'use strict';

angular.module('testing.controllers', [])

.controller('Testing', function($scope) {

    window.testing = function() {
        alert('testing');
    }

});

Can't say that I remend you to do this though, but it's hard to judge since we don't know more about why you want to do this.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745419711a4626911.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信