I have an textfield which the user types whatever thing they want to Google. When the user clicks done in the Textfield it goes to a new view with an web view. The ISSUE Is when I type something in the textfield the web view is just white (Doesn’t load it to the web view).
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *query = [self buildGoogleSearchParameter:self.searchField];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.google.co/search?q=%@", query]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
}
- (NSString *)buildGoogleSearchParameter:(NSString *)searchField
{
NSArray *unencodedStrings = [searchField componentsSeparatedByString:@" "];
NSMutableArray *encodedStrings = [NSMutableArray array];
for (NSString *unencodedString in unencodedStrings)
[encodedStrings addObject:[self urlEncode:unencodedString]];
return [encodedStrings componentsJoinedByString:@"+"];
}
- (NSString *)urlEncode:(NSString *)unencodedString
{
return CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)unencodedString,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8));
}
-(IBAction)searchdone {
ViewController *web = [self.storyboard instantiateViewControllerWithIdentifier:@"WebView"];
web.searchField = searchbar.text;
[self presentModalViewController:web animated:YES];
}
@end
The problem is that you appear to be trying to issue
loadRequestbefore you even create the controller with a storyboard identifier of"WebView".If you’re trying to present a new modal view with the
UIWebViewon it, generally, you would add aNSStringproperty in your web viewViewControllerfor the string being searched, saysearchField, e.g.:and then the
viewDidLoadfor that view controller with the web view would then do theloadRequestbuilding the URL using thesearchFieldproperty:Thus,
ViewController.hfor the destination view controller might have an@interfacethat includes not only theUIWebView, but also our newsearchFieldproperty:And the
@implementationin the .m file might be:Note,
webViewis the name for myIBOutletfor myUIWebView. You should replace that with whatever is appropriate for your project.Also notice that I did some url encoding of the search fields. That way you can search for fields with ampersand characters, quotation marks, etc. The easiest way to do that is the
CFURLCreateStringByAddingPercentEscapesthat you see above.