Debugging xcode phase? - ios

I'm getting one error, Ive made a UIPageController for the web but I cant seem to find the problem with it, there is only one error, please help. code bellow - more code on demand.
#interface ContentViewController ()
#end
#implementation ContentViewController
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[_webView loadHTMLString: _dataObject baseURL:[NSURL URLWithString:#"http://"]];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#end
[_webView loadHTMLString: _dataObject baseURL:[NSURL URLWithString:#"http://"]];
My "Error is above"
Please help
#import <UIKit/UIKit.h>
#interface ContentViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIView *webView;
#property (strong, nonatomic) id dataObject;
#end

Change the .h file to this:
#import <UIKit/UIKit.h>
#interface ContentViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIWebView *webView;
#property (strong, nonatomic) id dataObject;
#end
Beside that if you want to load a webpage in UIWebView from a url then you can do like this:
self.webView.delegate = self;
self.webView.scalesPageToFit =YES; // This will fit the page within the screen
NSURL *Url = [NSURL URLWithString:#"http://facebook.com/xxx/xxx"];
NSURLRequest *req = [[NSURLRequest alloc] initWithURL:Url];
[self.webView loadRequest:req];
If you want to load a HTML file from document then do:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"Test" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlString baseURL:nil];
Or if you just want to load a string then do:
NSString* htmlString = #"";
[webView loadHTMLString:htmlString baseURL:nil];
Hope this helps .. :)

Related

Crash after setting WebView Delegate

I have developed the webView in method viewDidLoad in ViewController
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
id <UIWebViewDelegate> delegate =[[MyDelegate alloc] init];
webView.delegate = delegate;
NSError *error;
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSString *htmlContent = [[NSString alloc] initWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:&error];
[webView loadHTMLString:htmlContent baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
[self.view addSubview:webView];
I set the delegate on instance of class MyDelegate.
In MyDelegate Class:
#import <UIKit/UIKit.h>
#interface MyDelegate : NSObject <UIWebViewDelegate>
#end
#import "MyDelegate.h"
#implementation MyDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
return YES;
}
#end
But app my crash during start the loasding.
If I not load html content, but url ('google.com' for example) crash happens.
When I comment this 'webView.delegate = delegate;' crash doesn't happens.
I know that I can use this in ViewController.h:
#interface ViewController : UIViewController<UIWebViewDelegate>
and this in viewDidLoad:
webView.delegate = self;
but I need use other class as delegate (not ViewController), but webview must be located in ViewController.
How I can make this?
Help me!
Let me point to the root cause.
UIWebView delegate attribute is a weak reference ("unowned(unsafe)" in Swift source code), which means its memory can be freed at any time.
So to solve this, you have to keep a reference into your controller as a class attribute.
Example of solution tested successfully in Swift:
class MyUIViewController : UIViewController{
let leftDelegate:MyWebViewDelegate = MyWebViewDelegate()
...
}
- (void)viewDidLoad {
[super viewDidLoad];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
id<UIWebView> delegate =(id)self;
webView.delegate = delegate;
NSError *error;
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSString *htmlContent= [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:&error];
[webView loadHTMLString:htmlContent baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
[self.view addSubview:webView];
}
MyDelegate.h
#import <UIKit/UIKit.h>
#protocol UIWebView <UIWebViewDelegate>
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
#end
#interface MyDelegate : NSObject
#end
In your ViewController itself you can implement UIWebViewDelegate.
-(void)viewDidLoad
{
[super viewDidLoad];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
webView.delegate = self;
NSError *error;
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSString *htmlContent= [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:&error];
[webView loadHTMLString:htmlContent baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
[self.view addSubview:webView];
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
return YES;
}
I'm posting a separate answer because I think there's a need to provide explicit Objective-C solution, since OP might miss David GONZALEZ's answer because it's in Swift, although he absolutely nailed it.
The problem is that by the time web view calls its delegate, that delegate has been deallocated. So the solution would be to add property of MyDelegate type to ViewController private extension declaration:
#interface ViewController ()
#property (nonatomic, strong) MyDelegate* webViewDelegate;
#end
And then to store MyDelegate instance created in -viewDidLoadin that property:
...
id <UIWebViewDelegate> delegate =[[MyDelegate alloc] init];
webView.delegate = delegate;
self. webViewDelegate = delegate;
...

Passing URLs with button touch in Objective C

I'm building a basic app for my school and can't figure out how to change the URL to be presented based on the button pressed. I have four buttons and currently have them all linked to temporary URLs, but when I run my app, the webview appears blank and my NSLog returns null for the URL. What am I doing wrong? Here is my code:
TWViewController.h:
#import <UIKit/UIKit.h>
#interface TWViewController : UIViewController
- (IBAction)gradesPressed:(id)sender;
- (IBAction)facPressed:(id)sender;
- (IBAction)newsPressed:(id)sender;
- (IBAction)sportsPressed:(id)sender;
#property (strong, nonatomic) NSURL *url;
#end
TWViewController.m:
#import "TWViewController.h"
#import "TWWebViewController.h"
#interface TWViewController ()
#end
#implementation TWViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)gradesPressed:(id)sender{
TWWebViewController *webView = [[TWWebViewController alloc] init];
webView.website = [NSURL URLWithString:#"http://google.com"];
}
- (IBAction)facPressed:(id)sender {
TWWebViewController *webView = [[TWWebViewController alloc] init];
webView.website = [NSURL URLWithString:#"http://yahoo.com"];
}
- (IBAction)newsPressed:(id)sender {
TWWebViewController *webView = [[TWWebViewController alloc] init];
webView.website = [NSURL URLWithString:#"http://facebook.com"];
}
- (IBAction)sportsPressed:(id)sender {
TWWebViewController *webView = [[TWWebViewController alloc] init];
webView.website = [NSURL URLWithString:#"http://apple.com"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
//
// TWWebViewController *wbc = (TWWebViewController *)segue.destinationViewController;
//}
#end
TWWebViewController.h:
#import <UIKit/UIKit.h>
#interface TWWebViewController : UIViewController
{
UIWebView *webView;
NSString *website;
}
#property (strong, nonatomic) IBOutlet UIWebView *webView;
#property (strong, nonatomic) NSURL *website;
#end
TWWebViewController.m:
#import "TWWebViewController.h"
#import "TWWebViewController.h"
#interface TWWebViewController ()
#end
#implementation TWWebViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:self.website];
[self.webView loadRequest:urlRequest];
NSLog(#"%#", website);
// Do any additional setup after loading the view.
}
I'm fairly new to iOS Development, so I'm not quite sure if I'm even on the right track. Any help would be appreciated, thanks!
You need to push your controller after initializing it: Also, your NSString is not an NSURL object so do this also in your TWWebViewController: NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:self.website]];
TWWebViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:
[NSURL URLWithString:self.website]];
[self.webView loadRequest:urlRequest];
}
TWViewController.m:
- (IBAction)sportsPressed:(id)sender
{
TWWebViewController *webView = [self.storyboard
instantiateViewControllerWithIdentifier:#"WebView"];
webView.website = #"http://apple.com";
[self.navigationController pushViewController:webView animated:YES];
}
Also don't forget to do this in your storyboard file:

Issues initializing UIWebView inside ViewController

I want to initialize a UIWebview object inside of my WebViewController. I'm having a bit of trouble initializing it, and directing it to the URL that is sent to it from the previous ViewController.
In the viewDidLoad of my WebViewController, I attempt to initialize it with the following:
UIWebView *tempWebview = [[UIWebView alloc]initWithFrame:theFrame];
NSURL *url = [NSURL URLWithString:_itemURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
self.myWebView = tempWebview;
[tempWebview loadRequest:urlRequest];
_myWebView.delegate=self;
[WebViewController loadRequest:urlRequest];
However, this gives me errors stating:
use of undeclared identifier'theFrame', and no known class method for selector loadRequest.
Full code is below:
WebViewController.h:
#import <UIKit/UIKit.h>
#import "MatchCenterViewController.h"
#interface WebViewController : UIViewController
#property (strong, nonatomic) NSURL *itemURL;
#property (weak, nonatomic) IBOutlet UIWebView *myWebView;
#end
WebViewController.m:
#import "WebViewController.h"
#interface WebViewController ()
#end
#implementation WebViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIWebView *tempWebview = [[UIWebView alloc]initWithFrame:theFrame];
NSURL *url = [NSURL URLWithString:_itemURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
self.myWebView = tempWebview;
[tempWebview loadRequest:urlRequest];
_myWebView.delegate=self;
[WebViewController loadRequest:urlRequest];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#end
First you need to define that you are going to use the UIWebView Delegate in your .h file as:
#interface WebViewController : UIViewController <UIWebViewDelegate>
When you already have a webView defined in your Xib file, why are you creating another UIWebView and assigning it to the IBOutlet's webView? You could simply do:
self.myWebView.delegate = self; //Note that I have set the delegate first before calling LoadRequest
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.myWebView setScalesPageToFit:YES];
[self.myWebView loadRequest:request];

iOS segues data not being passed

Data is not being passed from one VC to another, can't for the life of me figure it out. I'm trying to pass an NSURL or NSURLRequest and when I log out the request/url object on the second VC, I get null.
First VC.h
#import <UIKit/UIKit.h>
#import "ILAuthClient.h"
#import "ILAuthLoginViewController.h"
#interface ILViewController : UIViewController
#property (strong, nonatomic) NSString *string;
#property (strong, nonatomic) NSURL *request;
#end
First VC.m (relevant section)
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"authLogin"]){
NSURL *url = [self authenticateWithInstagram];
ILAuthLoginViewController *authModal = [[ILAuthLoginViewController alloc]init];
authModal.delegate = self;
authModal.url = url;}
Second VC.h
#import <UIKit/UIKit.h>
#interface ILAuthLoginViewController : UIViewController <UIWebViewDelegate>
#property (strong, nonatomic) IBOutlet UIWebView *webView;
#property (strong, nonatomic) NSURLRequest *request;
#property (strong, nonatomic) NSURL *url;
#property (strong, nonatomic) NSString *string;
#end
Second VC.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.webView.delegate = self;
self.request = [[NSURLRequest alloc]initWithURL:self.url];
[self.webView loadRequest:self.request];
NSLog(#"Request %#", self.request);
}
When you enter prepareForSegue your second view controller has already been allocated for you and is available as the destinationViewController property on the segue. You are allocating a new one and assigning properties but this object isn't used.
Your method should be
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"authLogin"]){
NSURL *url = [self authenticateWithInstagram];
ILAuthLoginViewController *authModal = (ILAuthLoginViewController *)segue.destinationViewController;
authModal.delegate = self;
authModal.url = url;
}
}
This line ...
ILAuthLoginViewController *authModal = [[ILAuthLoginViewController alloc]init];
should be changed to this
ILAuthLoginViewController * authModal = (ILAuthLoginViewController *) segue.destinationViewController;
The one you are allocing isn't the one the storyboard is presenting
Two things to check for.
1.Your call is wrong while passing through segue. It should be
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"authLogin"]){
NSURL *url = [self authenticateWithInstagram];
ILAuthLoginViewController *authModal = (ILAuthLoginViewController *)segue.destinationViewController;
authModal.delegate = self;
authModal.url = url;
}
}
2.Make sure your url string is well formed.
NSURL *url = [NSURL URLWithString:[#"YOURURL" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
Hope its helpful.

No visible #interface 'UIView' declares the selector 'loadRequest'

I am creating a webView application by following this tutorial. I followed all the steps exactly but got this error:
No visible #interface 'UIView' declares the selector 'loadRequest'
-
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *fullURL = #"http://google.com/";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_viewWeb loadRequest:requestObj];
// Do any additional setup after loading the view, typically from a nib.
}
How can i fix it ?
As mentioned by #NiravPatel, #Chancy. I did this mistake in step 15.
I was using
#property (strong, nonatomic) IBOutlet UIView *viewWeb;
instead of
#property (strong, nonatomic) IBOutlet UIWebView *viewWeb;

Resources