Skip to content

Instantly share code, notes, and snippets.

@jjthrash
Created December 30, 2010 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jjthrash/ba85a3d564489e9e767b to your computer and use it in GitHub Desktop.
Save jjthrash/ba85a3d564489e9e767b to your computer and use it in GitHub Desktop.
Interact more seamlessly with the content in a UIWebView
#import "UIWebView+PlatformIntegration.h"
@implementation UIWebView (PlatformIntegration)
- (NSString*)pi:(NSString*)function {
return [self stringByEvaluatingJavaScriptFromString:
[NSString stringWithFormat:@"PlatformIntegration.%@", function]];
}
- (BOOL)platformIntegrationEnabled {
return ![@"undefined" isEqualToString:[self stringByEvaluatingJavaScriptFromString:@"typeof(PlatformIntegration)"]];
}
- (void)submitForm {
[self pi:@"submitForm();"];
}
- (void)submitFormWithSelector:(NSString*)selector {
[self pi:[NSString stringWithFormat:@"submitForm('%@');", selector]];
}
- (void)setValue:(NSString*)value forFormField:(NSString*)fieldID {
[self pi:[NSString stringWithFormat:@"setFormValue('%@', '%@');", fieldID, value]];
}
- (void)observe {
if ([self platformIntegrationEnabled]) {
[self pi:@"observe();"];
}
}
- (BOOL)handlePlatformIntegrationEventsForRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType delegate:(id<PlatformIntegrationDelegate>)delegate {
NSString *uri = request.URL.absoluteString;
NSArray *components = [uri componentsSeparatedByString:@":"];
if ([@"platformintegration" isEqualToString:[components objectAtIndex:0]]) {
if ([@"formchanged" isEqualToString:[components objectAtIndex:1]]) {
if ([delegate respondsToSelector:@selector(webViewFormChanged:)])
[delegate webViewFormChanged:self];
}
return NO;
}
else {
if ([delegate respondsToSelector:@selector(webView:loadedNewURL:)]) {
[delegate webView:self loadedNewURL:request.URL];
}
return YES;
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment