iProgrammed
iProgrammed

Reputation: 980

How to load Web view in different View in iOS 6

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

Upvotes: 1

Views: 2266

Answers (1)

Rob
Rob

Reputation: 438232

The problem is that you appear to be trying to issue loadRequest before you even create the controller with a storyboard identifier of "WebView".

If you're trying to present a new modal view with the UIWebView on it, generally, you would add a NSString property in your web view ViewController for the string being searched, say searchField, e.g.:

-(IBAction)searchdone {

    ViewController *web = [self.storyboard instantiateViewControllerWithIdentifier:@"WebView"];
    web.searchField = searchbar.text;
    [self presentModalViewController:web animated:YES];
}

and then the viewDidLoad for that view controller with the web view would then do the loadRequest building the URL using the searchField property:

Thus, ViewController.h for the destination view controller might have an @interface that includes not only the UIWebView, but also our new searchField property:

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (nonatomic, strong) NSString *searchField;

@end

And the @implementation in the .m file might be:

- (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));
}

Note, webView is the name for my IBOutlet for my UIWebView. 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 CFURLCreateStringByAddingPercentEscapes that you see above.

Upvotes: 2

Related Questions