Using a url scheme internally with a UIWebView to change a UILabel
I've got a UIWebView which shows up a couple of screens into a UINavigation开发者_开发知识库Controller:
First View > Second View > View with UIWebView + UILabel
Now, I display a certain page in that web view, which has a link back to my app....
myapp://foofoo
I know you can set up a custom URL with (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
and some info.plist poking, but how would change the UILabel, which is one the same screen as UIWebView, by simply clicking the myapp://foofoo link?
The best way to do this would be through a custom link, and then use the UIWebView delegate method -webView:shouldStartLoadWithRequest:navigationType: to trap requests. When you see a request come through with your link in it, you know your action has been triggered.
UIWebView Expose Objective C to JavaScript
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(@"scheme = %@",[[request URL] scheme]);
if([[[request URL] scheme]isEqualToString:@"myscheme"])
{
[messageLabel setText:@"HELLO!"];
return NO;
}
else return YES;
}
Make sure your view controller conforms to the UIWebViewDelegate protocol for this to work.
精彩评论