-
-
Save jjthrash/ba85a3d564489e9e767b to your computer and use it in GitHub Desktop.
Interact more seamlessly with the content in a UIWebView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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