iOS web browser Google search - ios

The assignment was to take anything with a space in the search query, assume it is a search criteria, and run it through a Google programmatically. Here is the code:
NSString *URLString = textField.text;
if ([URLString rangeOfString:#" "].location > 0) {
NSString *searchString = [URLString stringByReplacingOccurrencesOfString:#" " withString:#"+"];
NSURL *searchURL =[NSURL URLWithString:[NSString stringWithFormat:#"http://google.com/search?q=%#",searchString]];
NSURLRequest *searchRequest = [NSURLRequest requestWithURL:searchURL];
[self.webview loadRequest:searchRequest];
}
I'm sure it's sloppy, but I was just curious as to why it's not functional. I took it through the debugger, and everything up to "searchString" seems to go well. I'm at a loss.
Let me know if I need to add more code.

I created a project using Xcode 6.3, storyboards, and ran in the iPhone 6 simulator and it works just fine. Here are two screenshots from the simulator:
And here's the code I used. It's slightly different from yours and I also included my whole VC:
#import "ViewController.h"
#interface ViewController ()
#property (strong, nonatomic) IBOutlet UITextField *searchText;
#property (strong, nonatomic) IBOutlet UIWebView *webView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)googleItButtonPress:(id)sender {
NSString *searchString = self.searchText.text;
if ([searchString rangeOfString:#" "].location > 0) {
NSString *searchStringModified = [searchString stringByReplacingOccurrencesOfString:#" " withString:#"+"];
NSURL *searchURL =[NSURL URLWithString:[NSString stringWithFormat:#"http://google.com/search?q=%#",searchStringModified]];
NSURLRequest *searchRequest = [NSURLRequest requestWithURL:searchURL];
[self.webView loadRequest:searchRequest];
}
}
#end
I'm also getting this when I print the value of my modified search string if I set a breakpoint for the debugger:
(lldb) po searchStringModified
google+stuff
Looks like you're good to go. In this case just use storyboards and make sure your UIWebView is connected to your VC code.

Not sure what happened, but I suffered a case of spontaneous resolution. Thanks, everyone, for your time and advice.

Check this thing this work fine for me
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *URLString = #"HEllo World";
if ([URLString rangeOfString:#" "].location > 0) {
NSString *searchString = [URLString stringByReplacingOccurrencesOfString:#" " withString:#"+"];
NSURL *searchURL =[NSURL URLWithString:[NSString stringWithFormat:#"http://google.com/search?q=%#",searchString]];
NSURLRequest *searchRequest = [NSURLRequest requestWithURL:searchURL];
[webview loadRequest:searchRequest];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
NSLog(#"Started Loading");
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
}

Related

How to load two urls in webview Together in iOS?

i am loading url in webview.
i have two types of data to display
1)text(html string)
2)url
here is my code
To display text
[self.webView loadHTMLString:[self.arr objectAtIndex:indexPath.row][#"text"] baseURL:nil];
To display url
NSString *str = [NSString stringWithFormat:#"%#%#",URL,strpdf];
NSURL *websiteUrl = [NSURL URLWithString:str];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:websiteUrl];
[self.webView loadRequest:urlRequest];
issue is when i loading both data,data is overwrite.
i want to display both data in webview
but right now only text or image is displaying.
what is solution for this.
My demo is below:
#import "ViewController.h"
#interface ViewController ()<UIWebViewDelegate>
#property(nonatomic, strong) UIWebView *webView;
#property (nonatomic, copy) NSString *html_str;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initUI ];
}
- (void)initUI {
_html_str = #"I love google..."; // the html text you want
CGRect frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
_webView = [[UIWebView alloc] initWithFrame:frame];
_webView.delegate = self;
NSString *str = [NSString stringWithFormat:#"http://chsezhsan195030.e-fae.cn/"];
NSURL *websiteUrl = [NSURL URLWithString:str];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:websiteUrl];
[self.webView loadRequest:urlRequest];
[self.view addSubview:self.webView];
}
#pragma mark - webview delegate
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *script_str = [NSString stringWithFormat:#"var body = document.getElementsByTagName('body')[0];\
var script = document.createElement('div');\
script.innerText = '12345678'%#;\
script.style.width = window.innerWidth + 'px';\
script.style.height = '300px';\
script.style.backgroundColor = '#eeeeee';\
body.appendChild(script); \
body.insertBefore(script, body.childNodes[0]);",_html_str];
NSString *function = [NSString stringWithFormat: #"var script = document.createElement('script');\
script.type = 'text/javascript';\
script.text = \'function myFunction() { \
%# };'\
document.getElementsByTagName('head')[0].appendChild(script);", script_str];
[self.webView stringByEvaluatingJavaScriptFromString:function];
[self.webView stringByEvaluatingJavaScriptFromString:#"myFunction()"];
}
#end
But you should attention most of the url is prevented for inject js code,especially the https url, so you can not insert the html text above the google's index page.

cannot set a property

I am making a blog reader app. Everything works fine until I try to access the detailView from a cell. I receive this error,
[UINavigationController setPFeedURL:]: unrecognised selector sent to instance 0x7f96d5802800
below code is in the PRTableViewController.m
#import "PRTableViewController.h"
#import "PRFeedPost.h"
#import "PRDetailViewController.h"
This is where the code is where the crash happens
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"showDetail"]){
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
PRFeedPost *feed_Post = [self.polisenFeed objectAtIndex:indexPath.row];
[segue.destinationViewController setPFeedURL:feed_Post.url];
}
}
This is to help you understand what I am trying to setPFeedURL to. Also in PRViewTableController.m
self.polisenFeed = [NSMutableArray array];
NSArray *polisenFeedArray = [dataDictionary objectForKey:#"posts"];
for (NSDictionary *postDictionary in polisenFeedArray) {
PRFeedPost *fp = [PRFeedPost feedWithTitle:[postDictionary objectForKey:#"title"]];
fp.author = [postDictionary objectForKey:#"author"];
fp.thumbnail = [postDictionary objectForKey:#"thumbnail"];
fp.date = [postDictionary objectForKey:#"date"];
fp.url = [NSURL URLWithString:[postDictionary objectForKey:#"url" ]];
[self.polisenFeed addObject:fp];
}
Now the PRDetailViewController.h,
This is where the property is I am trying to set is,
#property (strong, nonatomic) NSURL *pFeedURL;
#property (strong, nonatomic) id detailItem;
#property (strong, nonatomic) IBOutlet UIWebView *webView;
Here is the code in PRDetailViewController.m,
- (void)viewDidLoad {
[super viewDidLoad];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:self.pFeedURL];
[self.webView loadRequest:urlRequest];
I cannot seem to figure out why this is crashing. I know it has to do with this line ,
[segue.destinationViewController setPFeedURL:feed_Post.url];
(at leaset I think it is that line. I can't figure out why I cannot set it.
Thanks for your help guys!
Change This
PRDetailViewController *objPRDetailViewController = (PRDetailViewController *)[segue.destinationViewController topViewController];
objPRDetailViewController.pFeedURL=feed_Post.url;
And Also put bellow code in viewDidAppear
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:self.pFeedURL];
[self.webView loadRequest:urlRequest];
try this
[[((id)segue.destinationViewController) topViewController] setPFeedURL:feed_Post.url];
let me know if it works

xcode osx webview new window

I have a problem with webview.
I made a simple webbrowser for Osx, within I have to hide nvigation bar , menu amd right click and the user can go only in one specific url..
all this is ok but I need that allow _blank target.. i mean .. I have some link woth target _blank, so to open in a new window, but it does no work and i don't know hot to allow this.
this is my code for DataOAppDelegate.h:
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#interface DataOAppDelegate : NSObject <NSApplicationDelegate,NSWindowDelegate>
{WebView *WebView;
//other instance variable
}
#property (assign) IBOutlet NSWindow *window;
#property (retain, nonatomic) IBOutlet WebView *myWebView;
#end
and code for DataOAppDelegate.m
#import "DataOAppDelegate.h"
//#import <WebKit/WebKit.h>
#implementation DataOAppDelegate
#synthesize window;
#synthesize myWebView;
//your function etc
-(void)awakeFromNib{
NSString *urlText = #"http://website.com";
[[myWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]];
[myWebView setDrawsBackground:NO];
[window setDelegate:self];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[myWebView setUIDelegate:self];
NSString *urlText = #"http://website.com";
[[myWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]];
}
- (WebView *)myWebView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request
{
NSLog(#"sss%#",sender);
NSUInteger windowStyleMask = NSClosableWindowMask |
NSMiniaturizableWindowMask |
NSResizableWindowMask |
NSTitledWindowMask;
NSWindow* webWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600) styleMask:windowStyleMask backing:NSBackingStoreBuffered defer:NO];
WebView* newWebView = [[WebView alloc] initWithFrame:[webWindow contentRectForFrameRect:webWindow.frame]];
[newWebView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
[webWindow setContentView:newWebView];
[webWindow center];
[webWindow makeKeyAndOrderFront:self];
[[newWebView mainFrame] loadRequest:request];
return newWebView;
}
- (void)launchSoftWithBundleID:(NSString *)softPath
{
NSBundle *softBundle = [NSBundle bundleWithPath:softPath];
NSString *bundleID = [softBundle bundleIdentifier];
//
NSTask *softTask = [[NSTask alloc] init];
[softTask setLaunchPath:softPath];
[softTask launch];
//
NSArray *array = [NSRunningApplication runningApplicationsWithBundleIdentifier:bundleID];
if ([array count] > 0)
{
NSRunningApplication *runningApp = [array objectAtIndex:0];
[runningApp activateWithOptions:NSApplicationActivateIgnoringOtherApps];
}
}
WebViews have delegate methods to do so: decidePolicyForNavigationAction and decidePolicyForNewWindowAction (documentation).
- (void)webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id)listener {
if ([sender isEqual:self.YourVebView]) {
[listener use];
}
else {
[[NSWorkspace sharedWorkspace] openURL:[actionInformation objectForKey:WebActionOriginalURLKey]];
[listener ignore];
}
}
- (void)webView:(WebView *)sender decidePolicyForNewWindowAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request newFrameName:(NSString *)frameName decisionListener:(id<WebPolicyDecisionListener>)listener {
[[NSWorkspace sharedWorkspace] openURL:[actionInformation objectForKey:WebActionOriginalURLKey]];
[listener ignore];
}
Note: don't forget to set the policy delegate for your WebView.

Load Different HTML in iOS UIWebView for iPhone and iPad

I can get both iPhone and iPad to load the same HTML file, however I would like to load a more optimized page based on which device the program is loading on.
ViewController.h:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIWebView *webView;
#end
ViewController.m:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
NSURL *url = [[NSBundle mainBundle] URLForResource:#"index" withExtension:#"html"];
NSString *html = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[_webView loadHTMLString:html baseURL:baseURL];
// disable scrolling
_webView.scrollView.scrollEnabled = NO;
_webView.scrollView.bounces = NO;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I have tried to create a new outlet, but I seem to be running into overlapping code.
How can I load different HTML files for iPad and iPhone with a universal build?
Just use one web view. All you need is two different HTML files.
Then you code can be something like:
NSURL *url;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
url = [[NSBundle mainBundle] URLForResource:#"index-iphone" withExtension:#"html"];
} else {
url = [[NSBundle mainBundle] URLForResource:#"index-ipad" withExtension:#"html"];
}
This assumes you have both an index-iphone.html and a index-ipad.html file in your app bundle.

Screen Orientation Xcode 4.3 iOS5.1

I am trying to have this view rotate, so that when a youtube video is played, it can be viewed full screen.
The view is embedded inside both a navigation view controller, and a tabBar view controller.
Using Xcode 4.3 target deployment is iOS 5.1, testing on both a tethered iPhone 4, and the simulator on iPhone.
I have added:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
To my view controller, which should allow the screen to rotate based on the device orientation.
Also note: I have landscape enabled in my info settings, and verified in my plist.
This is my first time posting to stackoverflow. I hope the above is clear.
Here is the full .m for reference:
// Created by Jacob Topping on 12-03-20.
// Copyright (c) 2012 The Melon Inc. All rights reserved.
//
#import "WebOneViewController.h"
#interface WebOneViewController ()
#end
#implementation WebOneViewController
#synthesize webOne;
#synthesize rotateWeb;
#synthesize follow;
#synthesize earnestWeb;
#synthesize ronWeb;
#synthesize richardWeb;
#synthesize davidWeb;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
NSString *david = #"http://www.youtube.com/embed/YSbQ1SDwtgM";
NSURL *url7 = [NSURL URLWithString:david];
NSURLRequest *davidUrl = [NSURLRequest requestWithURL:url7];
[davidWeb loadRequest:davidUrl];
NSString *richard = #"http://www.youtube.com/embed/6jL_1kJ6pno";
NSURL *url6 = [NSURL URLWithString:richard];
NSURLRequest *richardUrl = [NSURLRequest requestWithURL:url6];
[richardWeb loadRequest:richardUrl];
NSString *ron = #"http://www.youtube.com/embed/E0EmDSg3YXY";
NSURL *url5 = [NSURL URLWithString:ron];
NSURLRequest *ronUrl = [NSURLRequest requestWithURL:url5];
[ronWeb loadRequest:ronUrl];
NSString *earnest = #"http://www.youtube.com/embed/oTG7JtdSpT8";
NSURL *url4 = [NSURL URLWithString:earnest];
NSURLRequest *earnestUrl = [NSURLRequest requestWithURL:url4];
[earnestWeb loadRequest:earnestUrl];
NSString *website3 = #"http://twitter.com/search/?src=hash&q=%23creb13";
NSURL *url3 = [NSURL URLWithString:website3];
NSURLRequest *requestUrl3 = [NSURLRequest requestWithURL:url3];
[follow loadRequest:requestUrl3];
NSString *website2 = #"http://themeloninc.com/The_Melon_Inc./app/Slider/demo/rotate.php";
NSURL *url2 = [NSURL URLWithString:website2];
NSURLRequest *requestUrl2 = [NSURLRequest requestWithURL:url2];
[rotateWeb loadRequest:requestUrl2];
NSString *website = #"http://www.youtube.com/embed/J2EWRG-epes";
NSURL *url = [NSURL URLWithString:website];
NSURLRequest *requestUrl = [NSURLRequest requestWithURL:url];
[webOne loadRequest:requestUrl];
//<iframe width="560" height="315" src="http://www.youtube.com/embed/J2EWRG-epes" frameborder="0" allowfullscreen></iframe>
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(320,1150)];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)viewDidUnload
{
[self setRichardWeb:nil];
[self setRonWeb:nil];
[self setEarnestWeb:nil];
[self setFollow:nil];
[self setRotateWeb:nil];
[self setWebOne:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
#end

Resources