I am very new to iPad and was trying "Loading a web page using the Web View" - opening the google page!
I followed all the steps given in the box/tutorial but I am not able to view the page.
Step 1 : Added web view in the view window
Step 2 : decalre outlet for web view by adding following code
#interface WebiPadViewController : UIViewController {
IBOutlet UIWebView *webDisplayiPad;
}
#property(nonatomic,retain) UIWebView *webDisplayiPad;
Step 3 : Connect webview outlet to Web view using file manager
Step 4 : Create URl and request object using followng code:
- (void)viewDidLoad {
NSString *urlAddress = #"http://www.google.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webDisplayiPad loadRequest:requestObj];
[super viewDidLoad];
}
Step 5 : Save and run the app to see google page.
But the Simulator shows only a white background!
Let me start by saying it is possible you simply haven't setup your ViewController class and View outlet property properly, as sergio describes, since that can also lead to a blank white background.
But I'm going to assume that it is your webDisplayiPad property that is not connected. I think this most likely since your "Step 3 : Connect webview outlet to Web view using file manager" seems a sketchy description at best.
To check your connection go to your WebiPadViewController.xib and right-click (cntrl-click) on the File's Owner icon. Under outlets there should be your webDisplayiPad listed with a circle to the right of it. If the circle is empty, and I suspect it is, drag from the circle to the UIWebView. The UIWebView will do a little flash to confirm. And your done.
Also the modern way of connecting IBOutlets is to put that keyword in the property definition and forget the ivar completely. For example:
#interface WebiPadViewController : UIViewController {
}
#property(nonatomic,retain) IBOutlet UIWebView *webDisplayiPad;
and in the .m file to do this:
#implementation WebiPadViewController
#synthesize webDisplayiPad = _webDisplayiPad;
The synthesize will create the backing variable for you and the _ character will help you keep them straight.
If you were to change to this way your loadRequest call would look like one of the following two:
[_webDisplayiPad loadRequest:requestObj]; // Calls through ivar
[self.webDisplayiPad loadRequest:requestObj]; // Calls through accessor method
Don't forget in you implementation:
#synthesize webDisplayiPad = _webDisplayiPad;
I guess that you got some problems with your Nib/Xib connections.
Please, check the following:
your nib's file's owner class is set to be WebiPadViewController;
your controller's view outlet is connected to the web view.
Set delegate.
web page loads only when u set the delegate.
other wise it will be an empty page
I think you have to load HTML into a UIWebView,
Related
I need to have a text part in my iOS application (in Objective C), with different names written. Each name must be linked to a view with the person's informations.
I don't know how to do that, what to use to be able to generate multiple links in one text, and link each name do the right action. So when I click on the link it should send the name I clicked on to required action.
Anyone knows how to do that with UIViews ? Or UILabels ? or anything..
Also, whenever I put a UIView in my View, it takes longer to load, do you know why ?
Thanks.. Hope that was clear enough !
HermyKa
You can use RTLabel for that,
Add this library in your code and than follow this step,
In .h file add this code
//Import RTLabel
#import "RTLabel.h"
// Add delegate
#interface ViewController : UIViewController<RTLabelDelegate>
#end
And in .m file
- (void)viewDidLoad {
RTLabel *label = [[RTLabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width-262)/2, (self.view.frame.size.height-203)/2, 262, 203)];
label.delegate = self;
// You have to create link for each name
NSString *searchString = #"Dilip Dev Ram";
}
//RTLabel Delegate Method
- (void)rtLabel:(id)rtLabel didSelectLinkWithURL:(NSURL*)url
{
//When user click on one of the name this method will called and url will return the name which user has tapped. You can add condition on name that which view will display.
NSLog(#"did tap on name %#", url);
}
You can use the set of the UILabels with gesture recognizer connected to each. Or the set of UIButtons.
http://www.raywenderlich.com/104744/uigesturerecognizer-tutorial-creating-custom-recognizers
I m just trying to fix all the contents of html according to the device screen.
I have to put html data at proper place for iPhone & iPad in webview.
Please help me!!!
I am going to assume that this question is a very basic one.
Suppose that you have placed a UIWebView in a view in your storyboard, and you have set layout constraints so that the UIWebView fills the view.
Then
Add an IBOutlet to your view controller, and connect it to the web view.
In the view controllers viewDidLoad: method, tell the web view what to display. Here is an example:
-(void)viewDidLoad {
[super viewDidLoad];
NSURL* url = [NSURL URLWithString:#"http://www.apple.com"];
NSURLRequest* r = [NSURLRequest requestWithURL:url];
[_webView loadRequest:r];
}
This loadRequest: call is asynchronous, meaning that the page may not show up immediately.
If this is not answering your question, please add more information.
If I have a UIWebView with mediaPlaybackRequiresUserAction = YES, then later in my app create a new UIWebView and set mediaPlaybackRequiresUserAction = NO on it, it also changes the value of that property on the first instance.
e.g. I have a UIWebView and then present a second UIWebView modally (for an ad), changing mediaPlaybackRequiresUserAction on the modal webView affects the presenting UIWebView.
Any ideas why this is? Are UIWebViews all backed by a single instance?
Link to sample project here.
not sure your app purpose, just try this way:
- (IBAction)unwind:(UIStoryboardSegue *)unwindSegue
{
[self TS_updateLabel];
[[self webView] setMediaPlaybackRequiresUserAction:YES];
[self TS_reloadWebView];
}
....
in method TS_reloadWebView
if (self.webView.isLoading) {
[self.webView stopLoading];
}
[self.webView loadHTMLString:htmlString baseURL:baseURL];
I guess it also is UIWebView bug .... but now this way maybe can solve your problem.
I have an app where on the very last UIViewController there is a UIWebView... the user navigates through the app and at the very end they can transition (modal) to the final UIViewController with a UIWebView... in it's viewDidLoad I have the UIWebView load the page.
The problem is it takes about 8 seconds to load this page and that annoys users.
How can I load the UIWebView way ahead of time (like when the app is first launched!) if that UIViewController hasn't even been presented yet?
I had your same problem for UIWebView inside a UIPageViewController and I found a better solution.
If you are in FirstViewController and you have your webView in your SecondViewController put the loadHTMLString or loadRequest method in the viewDidLoad of your SecondViewController and call [secondViewController.view layoutSubviews] for start loading in your FirstViewController.
Ex:
FirstViewController
-(void)viewDidLoad{
[super viewDidLoad];
// _secondViewController -> An instance of SecondVieController
[_secondViewController.view layoutSubviews];
}
SecondViewController
-(void)viewDidLoad{
[super viewDidLoad];
NSURL *url =[NSURL URLWithString:#"http://www.google.it"];
NSURLRequest *request =[NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
}
What u can do is, load the html string
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:self.path ofType:#"htm"];
NSError * error;
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:&error];
and when u display the webView load this content in your webView
[self.webView loadHTMLString:htmlString baseURL:[[NSBundle mainBundle] bundleURL]];
then your webview need only render your content and doesn't load the data
/// In Swift 2.0
do{
let path = NSBundle.mainBundle().pathForResource("theName", ofType: "html")!
let html = try String(contentsOfFile: path, encoding: NSUTF8StringEncoding)
webView.loadHTMLString(html, baseURL: nil)
}catch{
print("Error is happend")
}
Note: It works the same way for WKWebView
Not sure how much work you want to do for this but I think you could cache/store the data from the URL in something like a NSDictionary, or something similar, using [NSUserDefaults standardUserDefaults] and use that information before actually loading the page.
So, load the first time you use it, after it has been seen then store the data using NSUserDefaults. Next time you go to this page, load from the NSUserDefaults first and call the URL/API in the back ground then update NSUserDefaults with this new data.
If you are requesting or pulling specific information each time, then I don't think this will work. If that is your case, you can probably try loading the data/website in an earlier view controller (probably one that takes the user a while to navigate to) and then send that data to the webview. Something like delegate/protocol or maybe even NSNotificationCenter. Hope this helps.
UIWebView is very tricky object. Simple answer would be: no you can't.
UIWebView won't load a document or an URL if it is not in views hierarchy.
Don't try doing it in different thread/queue either.
What you can do is to add UIWebView to the views hierarchy at the very beginning (as you've mentioned) and then pass it to the last view controller with preloaded data. This may work, but it's not an elegant way.
Side questions is: why does it take 8 secs to load a page? Maybe you can download the content of this page earlier? Is it static or dynamic?
I have a ViewController embedded in a NavigationController. The ViewController has a UIWebView that I'm trying to capture links from. I'm able to intercept the links, but when I try to push the link content to a detail page, it seems to be firing a new Controller but it's invisible. If I click a link for a second time (or if I just click a second link) I see a back button pop up, but again the actual content of the new ViewController is not visible.
Here is the log output for the first link click:
2011-11-07 10:38:27.526 WGGB[43875:f803] *** WebKit discarded an uncaught exception in the webView:decidePolicyForNavigationAction:request:frame:decisionListener: delegate: <NSInternalInconsistencyException> Could not load NIB in bundle: 'NSBundle </Users/sonnyjitsu/Library/Application Support/iPhone Simulator/5.0/Applications/4B3D5152-4EA5-4C84-BB22-A774FCB8B6A7/WGGB.app> (loaded)' with name 'Detail View'
As you can see, it's "loading" the new view, but I see nothing in the simulator. Then the second time I click the same link (or click a new link) I see this in the log:
2011-11-07 10:38:30.605 WGGB[43875:f803] url is http://www.blahblahblah.com/mobile
The method in question is this:
-(BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;
NSRange page = [ urlString rangeOfString: #"/mobile" ];
if (page.location == NSNotFound) {
SingleStoryViewController *detailViewController = [[SingleStoryViewController alloc] initWithNibName:#"Detail View" bundle:nil];
NSString *requestURL =
[NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
[detailViewController.webView loadRequest:
[NSURLRequest requestWithURL:[NSURL URLWithString:requestURL]]];
[self.navigationController pushViewController:detailViewController animated:YES];
NSLog(#"url is %#", url);
return NO;
} else {
return YES;
}
}
Top of my SingleStoryViewController.m
#import "SingleStoryViewController.h"
#implementation SingleStoryViewController
#synthesize webView;
Entire SingleStoryViewController.h
#import
#interface SingleStoryViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIWebView *webView;
#end
Here is a link to an image of my Xcode storyboard, for what it's worth: http://i.imgur.com/I653v.png
I've been working on this for a good 14 to 16 hours now and I'm finally saying 'uncle' and asking for help. Any help would be appreciated, I'm very new to iOS programming so I apologize if there is something really stupid I'm missing.
As you can see, it's "loading" the new view...
That's not what the error says. The error says this:
Could not load NIB in bundle: [...] with name 'Detail View'
I'd start by checking the name of the .xib file in your app -- it sounds like it may not match the name you're using in the code.
Update: Per comments below, it turns out that this is app uses storyboards and doesn't contain any .xib files, so it's no surprise that attempting to load a view controller from a .xib produces an error. The correct way to load a view controller from a storyboard, when necessary, is to use UIStoryboard's -instantiateViewControllerWithIdentifier: method instead of UIViewController's -initWithNibName:bundle:.