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.
Related
I'm pretty new to objective-c and need some tips for my challenge.
I have 2 view controllers and need to show xml data retrieved from FirstViewController to the TermsViewController.
I'm successful getting user input and retrieve xml objects I need.
But don't know how to show the user name in the TermsViewController.m
Since data is downloaded async, can't figure out how to implement this for IOS 6.
Thanks in advance.
FirstViewController.h
#import <UIKit/UIKit.h>
#interface FirstViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIButton *accessButton;
#property (weak, nonatomic) IBOutlet UITextField *codeField;
#property (weak, nonatomic) NSString *codeUser;
#property (strong, nonatomic) NSString *nameUser;
#property (strong, nonatomic) NSDictionary *xmlDictionary;
#end
TermsViewController.h
#import <UIKit/UIKit.h>
#interface TermsViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *nameLabel;
#property (strong, nonatomic) NSString *nameUserTerms;
#end
FirstViewController.m
#import "FirstViewController.h"
#import "TermsViewController.h"
#import "XMLReader.h"
#interface FirstViewController ()
#property (nonatomic, strong) NSMutableURLRequest *postRequest;
#property NSUInteger responseStatusCode;
#property (nonatomic, strong) NSString *theXML;
#end
#implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)accessButton:(UIButton *)sender {
self.codeUser = self.codeField.text;
NSString *xmlCode = [NSString stringWithFormat:
#"<?xml version='1.0' encoding='utf-8'?>\n"
"<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>"
"<soap:Body>\n"
"<GetInterview xmlns='http://www.url.com/url.JCV'>\n"
"<Codigo>"
"%#"
"</Codigo>\n"
"</GetInterview>\n"
"</soap:Body>\n"
"</soap:Envelope>", self.codeUser];
NSLog(#"User code is: %#", self.codeUser);
NSLog(#"XML is: %#", xmlCode);
self.postRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"http://www.url.com/url.JCV/web.url.asmx"]];
[self.postRequest setValue:#"text/xml" forHTTPHeaderField:#"Content-Type"];
[self.postRequest setHTTPMethod:#"POST"];
[self.postRequest setHTTPBody:[NSMutableData dataWithBytes:[xmlCode UTF8String] length:strlen([xmlCode UTF8String])]];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:self.postRequest delegate:self];
if (conn) {
NSLog(#"Connected to: %#", conn);
} else {
NSLog(#"Connection Error");
}
[self.codeField resignFirstResponder];
}
FirstViewController.m connectionDidFinishLoading method
- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
if (self.responseStatusCode == 200) {
NSLog(#"Succeeded! Received %lu bytes of data",[self.theXML length]);
// Parse the XML into a dictionary
NSError *parseError = nil;
self.xmlDictionary = [XMLReader dictionaryForXMLString:self.theXML options:XMLReaderOptionsProcessNamespaces error:&parseError];
NSLog(#"%#", self.xmlDictionary);
//name of the candidate
self.nameUser = [[[[[[[self.xmlDictionary objectForKey:#"Envelope"] objectForKey:#"Body"] objectForKey:#"GetInterviewResponse"] objectForKey:#"GetInterviewResult"] objectForKey:#"Obj"] objectForKey:#"ProfissionalName"] objectForKey:#"text"];
NSLog(#"User name is: %#", self.nameUser);
TermsViewController *nv = [[TermsViewController alloc] init];
nv.nameUserTerms = self.nameUser;
//check
NSLog(#"User name stored: %#", nv.nameUserTerms);
[self performSegueWithIdentifier:#"goToTerms" sender:self];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error!"
message:#"bla bla."
delegate:self
cancelButtonTitle:#"Try again"
otherButtonTitles:nil];
[alert show];
}
TermsViewController.m
#import "TermsViewController.h"
#interface TermsViewController ()
#end
#implementation TermsViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.nameLabel.text = self.nameUserTerms;
//this check is returning NULL
NSLog(#"User name: %#", self.nameUserTerms);
}
#end
You should use prepareForSegue to exchange data between controllers.
Remove these lines from your code:
TermsViewController *nv = [[TermsViewController alloc] init];
nv.nameUserTerms = self.nameUser;
And put them in a method like this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"goToTerms"]) {
TermsViewController *nv = segue.destinationViewController;
nv.nameUserTerms = self.nameUser;
}
}
When I receive my push notification, from my app delegate I handle the push in handleRemoteNotifications:(NSDictionary *)userInfo and from there I call a firstViewController class method to send the push url, like this:
-(void)handleRemoteNotifications:(NSDictionary *)userInfo {
// get link
NSDictionary *aps = (NSDictionary *)[userInfo objectForKey:#"aps"];
if ([aps objectForKey:#"link"])
{
NSString* jobID = [NSString stringWithFormat:#"%#",[aps objectForKey:#"link"]];
NSLog(jobID,nil);
[FirstViewController viewController:jobID];
}
}
this is my firstViewController.h:
#import <UIKit/UIKit.h>
#interface FirstViewController : UIViewController <UIWebViewDelegate>
#property (readwrite, strong, nonatomic) IBOutlet UIWebView *firstview;
#property (weak, nonatomic) IBOutlet UIBarButtonItem *back;
#property (weak, nonatomic) IBOutlet UIBarButtonItem *stop;
#property (weak, nonatomic) IBOutlet UIBarButtonItem *refresh;
#property (weak, nonatomic) IBOutlet UIBarButtonItem *forward;
+(void)viewController:(NSString*)link;
- (void)updateButtons;
#end
An I'm trying to load the received push url in the current webView like this:
+(void)viewController:(NSString*)link{
NSURL *url = [NSURL URLWithString:link];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
FirstViewController *new = [[FirstViewController alloc]init];
[new loadPush:(NSURLRequest*)requestObj];
}
-(void)loadPush:(NSURLRequest*)requestObj{
[_firstview loadRequest:requestObj];
}
Everything works fine when I compile, except the url don't load. What am I doing wrong?
Can you put a NSLog after NSURL *url to test the link string and check the NSURL is not nil? Then if you receive a good URL, try to test it on your browser.
EDIT:
Maybe you have to add a webView on the new ViewController instanced.
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
self.webView = webView;
[self.view addSubview:self.webView];
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:
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];
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 .. :)