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.
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 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
I've start working with XCDYouTubeVideoPlayer. It does seem to have, some small issues. When I use the method (like below) to call the player it opens it and close it right away.
I've imported following frameworks:
mediaplayer
AVfoundation
and added the `XCDYouTubeVideoPlayerViewController.h and .m
In the viewController.m I've added this method:
- (IBAction) play:(id)sender
{
[self.view endEditing:YES];
NSString *link = #"m01MYOpbdIk";
XCDYouTubeVideoPlayerViewController *videoPlayerViewController = [[XCDYouTubeVideoPlayerViewController alloc] initWithVideoIdentifier:link];
[self presentMoviePlayerViewControllerAnimated:videoPlayerViewController];
}
At the moment its opening and closing the XCDYouTubeVideoPlayer right a way. What am i doing wrong?
I not understand why, but for me , in iOS8 the line
XCDYouTubeVideoPlayerViewController.m at #79
was calling the method :
- (id) initWithContentURL:(NSURL *)contentURL
{
#throw [NSException exceptionWithName:NSGenericException reason:#"Use the
` initWithVideoIdentifier:` method instead." userInfo:nil];
}
I just comment it and works!
XCDYouTubeVideoPlayer library build the youtube "streaming link", by appending the url with the signature provided. It seems that the "url" now has the signature along with it and the "sig" key comes null, which thereby negates the if statement in the function
(NSURL *) videoURLWithData:(NSData *)data error:(NSError * __autoreleasing *)error
Goto file
XCDYouTubeVideoPlayerViewController.m at #248
you will see a for-loop
for (NSString *streamQuery in streamQueries)
{
NSDictionary *stream = DictionaryWithQueryString(streamQuery, queryEncoding);
NSString *type = stream[#"type"];
NSString *urlString = stream[#"url"];
NSString *signature = stream[#"sig"];
if (urlString && signature && [AVURLAsset isPlayableExtendedMIMEType:type])
{
NSURL *streamURL = [NSURL URLWithString:[NSString
stringWithFormat:#"%#&signature=%#",
urlString,
signature]];
streamURLs[#([stream[#"itag"] integerValue])] = streamURL;
}
change it to this (remove the signature variable from if statement and modify the URLWithString)
for (NSString *streamQuery in streamQueries)
{
NSDictionary *stream = DictionaryWithQueryString(streamQuery, queryEncoding);
NSString *type = stream[#"type"];
NSString *urlString = stream[#"url"];
if (urlString && [AVURLAsset isPlayableExtendedMIMEType:type])
{
NSURL *streamURL = [NSURL URLWithString:urlString];
streamURLs[#([stream[#"itag"] integerValue])] = streamURL;
}
Desperately trying to figue out how to load a local image (saved in local xcode bundle) into the following HTML through a UIWebView. I have spent countless hours following other guides but nothing seems to work, all throw erros. Am trying to add a local image in the following NSString where I have written IMAGEGOESHERE -
{
[super viewDidLoad];
self.title = _restaurant.title;
[[self navigationController] setNavigationBarHidden:NO animated:NO];
self.activityIndicatorView.hidden = NO;
[self.activityIndicatorView startAnimating];
[self loadImageInNewThread];
NSString *webViewContent = [NSString stringWithFormat:#"<html><head><style>* {font-family: Helvetica}</style></head><body><center>%# - %#<br><br><b>Restaurant:</b>%#</b><br>IMAGEGOESHERE<br></center></b></body></html>", _restaurant.openingTime,_restaurant.closingTime, _restaurant.name];
[self.webView loadHTMLString:webViewContent baseURL:nil];
}
I hope you can help as its driving me mad!
You need to reference first the path of your image:
NSString *pathImg = [[NSBundle mainBundle] pathForResource:#"yourimage" ofType:#"png"];
and then specify your path in your webViewContent along with the size of your image:
NSString* webViewContent = [NSString stringWithFormat:
#"<html>"
"<body>"
"<img src=\"file://%#\" width=\"200\" height=\"500\"/>"
"</body></html>", pathImg];
This code should work,
Just replace yourFile with the name of your PDF, and webView with the name of your webView.
Then Replace ofType to the extension of your image.
//PDF View
//Creating a path pointing to a file.pdf
NSString *path = [[NSBundle mainBundle] pathForResource:#"yourFile" ofType:#"pdf"];
//Creating a URL which points towards our path
NSURL *url = [NSURL fileURLWithPath:path];
//Creating a page request which will load our URL (Which points to our path)
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//Telling our webView to load our above request
[webView loadRequest:request];
NSString *pathImg = [[NSBundle mainBundle] pathForResource:#"yourimage" ofType:#"png"];
NSString* webViewContent = [NSString stringWithFormat:
#"<html>"
"<body>"
"<img src=\"file://%#\" width=\"200\" height=\"500\"/>"
[webView loadHTMLString:webViewContent baseURL:nil];
You must add the last line with a base url of nil