I try to open a specific path in my app, but nothing is displayed in the UIWebView in my simulator, my orignal code is :
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *host = [[NSUserDefaults standardUserDefaults] valueForKey:#"host"];
NSString* complement = #"/manager/";
NSString *fullURL = [NSString stringWithFormat:#"%# %#", host, complement];
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_myWebview loadRequest:requestObj];
}
Host is stored with the bundles settings
1: When you have set the NSUserDefaults for the key "host", have you also called synchronize on NSUserDefaults?
2: Please debug the variable NSString *host, is the right string in it?
3: Instead of NSString *fullURL = [NSString stringWithFormat:#"%# %#", host, complement]; try this host = [host stringByAppendingString:complement];
3.1: Is on your server this specific path available over Desktop Safari?/Is there some index.html at this path?
4: Please debug the variable NSURL *url, is the right string in it?
5: Implement all UIWebViews Delegates and debug them also: UIWebViewDelegate
Related
I'm pretty new to iOS development, and can't find an answer that helps my problem anywhere.
I'm making an app where the user must first login, using a UIWebView.
Once the user logs in, they are taken to the app, but in the background the server has passed a unique authentication key for each user.
Example
The user logs in at the URL www.example.com/login.
on a successful login the URL changes to www.example.com/key/jsaeglihndzlgaskn with the end bit being the key I need to get.
I have a method which gets the last segment of the URL but it takes the starting URL not the one that changes.
NSString *absoluteString= _webView.request.URL.absoluteString;
NSArray* foo = [absoluteString componentsSeparatedByString: #"/"];
NSUInteger arrsize = [foo count];//count the size of array
NSString* bar = foo[arrsize-1];//get last element
Any help would be massively appreciated
Edit
This is how my code now looks, but It still doesn't function as I need it to, any help getting this to work would be greatly appreciated.
NSString *urlString = #"https://probablyrational.com/alpha/dashboard/register/?callback=inline";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[_webView loadRequest:urlRequest];
NSString* absoluteString = [url absoluteString];
NSArray* foo = [absoluteString componentsSeparatedByString: #"/"];//explode() .split()
NSUInteger arrsize = [foo count];//count the size of array
NSString* bar = foo[arrsize-1];//get last element
NSLog(#"bar %#", bar);
//bar = "?callback=inline"
if ([foo[arrsize-2] isEqualToString:#"key"]) {
// user is authenticated
NSLog(#"bar %#", bar);
- (BOOL)_webView:(UIWebView *)_webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *URLString = [[request URL] absoluteString];
if ([URLString isEqualToString:#"https://probablyrational.com/alpha/key"]) {
// The user reached step 3!
}
return YES;
}
EDIT 2
My code is now running but still not tracking the URL change..
NSString *urlString = #"https://probablyrational.com/alpha/dashboard/register/?callback=inline";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[_webView loadRequest:urlRequest];
NSString* absoluteString = [url absoluteString];
NSArray* foo = [absoluteString componentsSeparatedByString: #"/"];//explode() .split()
NSUInteger arrsize = [foo count];//count the size of array
NSString* bar = foo[arrsize-1];//get last element
NSLog(#"bar %#", bar);
//bar = "?callback=inline"
if ([foo[arrsize-2] isEqualToString:#"key"]) {
// user is authenticated
NSLog(#"bar %#", bar);
}
}
- (BOOL)webView:(UIWebView *)webView didFinishLoading:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *absoluteString = [[request URL] query];
if ([absoluteString isEqualToString:#"https://probablyrational.com/alpha/key"]) {
// The user reached step 3!
( NSLog(#"this is the code you're looking for"));
return YES;
}
else
{
( NSLog(#"this is not code you're looking for"));
return 0;
}
}
Any clue how I can get this to work would be life saving, i'm stuck at this point
you can use [foo lastObject]; to get the last component.
I FOUND THE PROBLEM... I found that the value of berria1 have "\n " at the end of the url. where he not get this but that's the problem.
I pass very curious thing and which do not find any explanation.
The fact is that in the application, I'm parsing some news and save these in CoreData, then show a list of news in a UITableView and if I click on one of them brings me to a UIWebView in which position the link with the full story. Now comes the weird ...
If I pass to NSURL the variable which I recovery of CoreData which contains the Web address, do not load, if I pass the same direction to a NSString and this NSString to UIWebView, the UIWebView load this normally. The following is my code.
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.webView.delegate = self;
NSString *berria1 = [[NSString alloc]init];
berria1 = [NSString stringWithFormat:#"%#",self.abisua.noticia];
NSString *berria2 = #"http://www.vitoria-gasteiz.org/we001/was/we001Action.do?idioma=eu&aplicacion=wb021&tabla=contenido&uid=u_47906a5c_14eabe80aeb__7fe9";
NSURL *url = [NSURL URLWithString:berria1];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:urlRequest];
NSLog(#"El contenido de la noticia 1 es %#", berria1);
NSLog(#"El contenido de la noticia 2 es %#", berria2);
}
With the NSLog I see that the values of the variables are the same as you see in the picture below.
The value of the two variables in the NSLog
Image - Debug window - URL is nil
That could be happening?
Thank you.
Its not berria1 which holds the URL its berria2.
NSURL *url = [NSURL URLWithString:berria2];
Here you go give it a try the site is pretty slow so it takes a time to load
NSString *berria2 = #"http://www.vitoria-gasteiz.org/we001/was/we001Action.do?%20%20%20%20idioma=eu&aplicacion=wb021&tabla=contenido&uid=u_47906a5c_14eabe80aeb__7fe9";
NSURL *myUrl = [NSURL URLWithString:berria2];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myUrl];
[self.webView loadRequest:myRequest];
Try this. I didn't see any code how you load the request to webview. But I added the code with that part. Replace it with your webview.
NSString *myurlString = [NSString stringWithFormat:#"%#",self.abisua.noticia];
NSString *cleanedUrl = [myurlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
NSURL *myUrl = [NSURL URLWithString:cleanedUrl];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myUrl];
UIWebView *mywebview = [[UIWebView alloc] init];
[mywebview loadRequest:myRequest];
I finally found and fixed the problem.
The problem was that the variable received whith Core Data and pass to NSString, I do not know how, but the link have added this characters at the end of URL "\n "
Berria1 variable value in debug area was:
berria1 __NSCFString * #"http://www.vitoria-gasteiz.org/we001/was/we001Action.do?idioma=eu&aplicacion=wb021&tabla=contenido&uid=u_47906a5c_14eabe80aeb__7fe9\n " 0x0000000126d9e6c0
Then when I passed this value to NSURL and this found these characters, NSURL value pass to nil.
Then I solved this, passing this NSString to a NSMutableString and erasing these characters before pass to NSURL with this code:
NSString *berria1 = self.abisua.noticia;
NSMutableString * miCadena = [NSMutableString stringWithString: berria1];
[miCadena deleteCharactersInRange: [miCadena rangeOfString: #"\n "]];
Thanks everyone for your help
hi am new to iOS developer,i have seen your code in that your adding second string to url why still understand just try adding 2nd string
NSURL *url = [NSURL URLWithString:berria2];
I'm attempting to write a SplitView control program in which pressing a table cell in the masterViewController causes an associated web page to load in the detail view controller. I have the following method in the detail view controller that I can confirm is getting called and is receiving the correct input:
-(void)masterAction:(id)sender {
NSString *http = #"http://";
http = [http stringByAppendingString:sender];
_urlString = http;
NSURL *url= [NSURL URLWithString:_urlString];
[self.web loadRequest:[NSURLRequest requestWithURL:url]];
}
However, nothing is loading. Any ideas why this might be? The only way I've been able to get anything at all to load is to insert something similar to the following in my viewDidLoad method:
NSURL *url= [NSURL URLWithString:#"http://www.google.com];
[self.web loadRequest:[NSURLRequest requestWithURL:url]];
The method is being called using:
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *thread = [self.issueData objectForKey:#"responseData"];
NSDictionary *feed = [thread objectForKey:#"feed"];
NSArray *entries = [feed objectForKey:#"entries"];
NSDictionary *posts = entries[indexPath.row];
NSString *urlString = [posts objectForKey:#"link"];
NSArray *split = [urlString componentsSeparatedByString:#"url=http://"];
NSString *url = [split objectAtIndex:1];
[self.delegate masterAction:url];
}
set the delegate of webview.
and try this.
NSString *myUrl = #"http://www.YourWebSite.com";
NSURL *webUrl = [NSURL URLWithString:myUrl];
[webObj loadRequest:[NSURLRequest requestWithURL:webUrl]];
I duplicated this code in a test project and the only piece of code where something can go wrong is if you are forgetting to put www. after the http:// before the domain name.
Trying changing your masterAction method to the one below:
- (void) masterAction: (id) sender
{
if (![sender isKindOfClass:[NSString class]]) return;
NSString *http = #"http://www.";
NSString *urlString = [http stringByAppendingString:sender];
NSURL *url = [NSURL URLWithString:urlString];
[self.web loadRequest:[NSURLRequest requestWithURL:url]];
}
If this is not the issue and the string being sent to the method contains the www. try setting the delegate of the UIWebView to see if any error is thrown when loading the request.
I have looked everywhere and have not found exactly what I am looking for, so here is my question:
I have a basic app I am playing around with. I have created a webview and would like to be able to download a file from the website that loads in the webview and save the file to say the Downloads folder on the local machine. The site loads fine inside the webview, now how do I download a file, say an .xml file from the site and save it to the Downloads folder on the local machine?
This is what I have so far:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];//<-- example site
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[webView mainFrame] loadRequest:request];
}
I would like to be able to download a file (possible using a delegate) then save it to a location on the local computer. I am pretty new to this so I'd appreciate any help.
The issue has been resolved. I added the following code to make it work:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSURL *url = [NSURL URLWithString:#"http://www.google.com"]; // <-- example website
NSURLRequest *request = [NSURLRequest requestWithURL:url];
self.webView.policyDelegate = self;
[self.webView setDownloadDelegate:self];
[[self.webView mainFrame] loadRequest:request];
}
- (void)webView:(WebView *)webView decidePolicyForMIMEType:(NSString *)type
request:(NSURLRequest *)request
frame:(WebFrame *)frame
decisionListener:(id < WebPolicyDecisionListener >)listener
{
if([type isEqualToString:#"application/octet-stream"]) //this is the type I was looking for
{
//figure out how to save file here
[listener download];
NSURLDownload *downLoad = [[NSURLDownload alloc] initWithRequest:request delegate:self];
if(downLoad)
{
[self download:downLoad decideDestinationWithSuggestedFilename:#"filename.ext"];
NSLog(#"File Downloaded Succesfully");
//[webView close];
[self.window close];
}
else
{
NSLog(#"The download failed");
}
}
//just ignore all other types; the default behaviour will be used
}
-(void)download:(NSURLDownload *)download decideDestinationWithSuggestedFilename:(NSString *)filename
{
NSString *destinationFileName;
NSString *homeDirectory = NSHomeDirectory();
destinationFileName = [[homeDirectory stringByAppendingPathComponent:#"Documents"] stringByAppendingPathComponent:filename];
[download setDestination:destinationFileName allowOverwrite:NO]; //file is being saved to the Documents folder on the local machine
}
Hope this will be helpful to someone else.
I am developing a simple testing app for iPhone. I want to click a button to change the URL of webview, then load the new URL. Here is my code:
NSString *nurl = [NSString stringWithFormat: #"http://www.abc.com/abc.aspx?SN=%#&fdate=%#&tdate=%#", SNstr, fDate, tDate];
NSLog(#"################: %#", nurl);
NSURL *url = [NSURL URLWithString:nurl];
NSLog(#"################: %#", url);
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[Logs loadRequest:req];
It is working well inside viewDidLoad.
I move it into a button:
-(IBAction)CheckLog:(id)sender
{
// codes here
}
When I press the button, it gives nurl's value correct, but url's value is null. So nothing is changed on the UIWebView Logs.
Why could this be, when the code runs correctly in viewDidLoad?
your button action will be like this...
-(IBAction)CheckLog:(id)sender
{
[[NSURLCache sharedURLCache] removeAllCachedResponses];
NSString *nurl = [NSString stringWithFormat: #"http://www.abc.com/abc.aspx?SN=%#&fdate=%#&tdate=%#", SNstr, fDate, tDate];
NSURL *url = [NSURL URLWithString: [nurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ];
// NSURL *theURL = [NSURL URLWithString:#"https://www.google.co.in/"]; // Your new URL
[self.myWebView loadRequest:[NSURLRequest requestWithURL:url]];
}
The first thing to check is that the method is definitely being called. Are your NSLog statements being printed? If not, check that you have connected the button to the IBAction correctly in Interface Builder.
I can't see anything in your code that is immediately jumping out to me as incorrect, so your best bet is to set a breakpoint at the start of the method and step through it line-by-line, checking the values of each variable as you go.