Web page didn't open in iOS app - ios

I have an iOS app which has XML file, that holds data for an app. That file contains links for web pages. For some reason, when I try to display this links with UIWebView, its not showing. There is white screen with black rectangle -
I know this problem is in my web links, but its perfectly correct when I try to open it in Mac or Windows browsers (in fact, I just copy-paste it from Wiki). For example, there is one of my links: http://ru.wikipedia.org/wiki/Московский_Кремль
And there is my code. It simple, but it do work when i test it with other link, like: http://apple.com
Code is below:
-(void)loadWebView{
NSURL *urlik = [NSURL URLWithString:self.entityLinkToWiki];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:urlik];
[self.myWebView loadRequest:requestObj];
}
// Note: self.entityLinkToWiki look like shown above - http://ru.wikipedia.org/wiki/Московский_Кремль
Of course I just don't want to force user to quit app and open link in Safari. Is there any way to show my stored web links in internal browser? And why is there such strange error? I mean links, that pretty fine in PC/Mac browsers doesn't work here?
UPDATED.
Guys, that was my fault and i terribly sorry for confusing you, and thank you for help. The problem is here: when i try to pass link string from one controller to another, somehow it lose part of text, and finally self.entityLinkToWiki = Московский_Кремль, not http://ru.wikipedia.org/wiki/Московский_Кремль as it should be!
My app have structure of table view, firstly, it load tableview, after that it pass values from XML which depends of indexPath.row. All values except web links works just fine, and i will explain the way i did this. As you request, there is part of web link in xml file:
<webpage>http://ru.wikipedia.org/wiki/Московский_Кремль</webpage>
To get that, i did following:
In "first" (tableView list) controller i pass values like this:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"detailSegue"]){
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
NSString *string = [[self.listOfPlaceDetails objectAtIndex:indexPath.row]objectForKey:#"description"];
DetailViewController *detailController = segue.destinationViewController;
detailController.descriptionStringShort =string;
....a lot of code here
detailController.fourthPhotoString = [[self.listOfPlaceDetails objectAtIndex:indexPath.row] objectForKey:#"imageFourth"];
detailController.webLink = [[self.listOfPlaceDetails objectAtIndex:indexPath.row]
objectForKey:#"webpage"];
}
}
Image link has nothing to do with web link, i just post it, so you get picture that i pass values in this way and it work fine. Then, i pass values from "detail" controller to controller, that finally have UIWebView:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
.... a lot of code here
if ([segue.identifier isEqualToString:#"WebSegue"]){
WebViewController *webController = segue.destinationViewController;
webController.entityLinkToWiki = self.webLink;
NSLog(#"WebSegue link is %#", self.webLink);
}
}
And NSLog says, that self.webLink is already - Московский_Кремль (incorrect value). Therefore, i didn't get correct link for my UIWebView. So now, my question is - how to get correct link passing it like i did? I don't want to bother load it from XML file in final controller, because i need information of indexPath.row, to get correct value (i have a lot of entities in XML file).
Thank you for any help you provide, and sorry about first post (in which i didn't bother to look at console and see, that my link was incorrect).

You have asked multiple questions. If you just want to open the Kremlin link in your browser - Use NSUTF8StringEncoding.
NSString doesn't have any function built in that really does URL encoding properly, but this one works for extended characters. This will definitely handle your foreign characters. I tested the following -
- (void) viewDidLoad{
[super viewDidLoad];
self.myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
self.myWebView.delegate = self;
NSString *myString = #"http://ru.wikipedia.org/wiki/Московский_Кремль";
NSURL *urlik = [NSURL URLWithString:[myString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:urlik];
[self.myWebView loadRequest:requestObj];
[self.view addSubview:self.myWebView];
}

Related

Dealing with the Done button after Quicklook, Objective C

This has got to be the dumbest question of the day, but I'm just not getting it.
I create a Quicklook, which shows just fine. When I hit the Done button, it just reappears. How do I intercept the Done button? Or more generally, control what is displayed in what I assume is a navbar. Here is the relevant code:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
/*
* get the path to the pdf resource.
*/
NSString *path = [[NSBundle mainBundle] pathForResource:#"article" ofType:#"pdf"];
NSURL *docURL = [NSURL fileURLWithPath:path];
/*
* create the Quicklook controller.
*/
QLPreviewController *qlController = [[QLPreviewController alloc] init];
PreviewItem *item = [[PreviewItem alloc] initPreviewURL:docURL WithTitle:#"Article"];
self.pdfDatasource = [[PDFDataSource alloc] initWithPreviewItem:item];
qlController.dataSource = self.pdfDatasource;
/*
* present the document.
*/
[self presentViewController:qlController animated:YES completion:nil];
}
I assume I am missing something obvious.
Thank you,
Ken
Did you tried Taking your ViewDidAppear code To ViewDidLoad ? As when you click on done button all the views of the Controller are being Loaded again except ViewDidLoad. So the quicklook view Appears again. Just Try
The trick was to roll everything back into the original viewcontroller. That way when I hit the done button it goes back to the original viewcontroller, which is exactly what I wanted. so instead of having a separate class, I just incorporated the calls right into my main viewcontroller. I suspect that there is still a way to do with a cunning use of delegates, but in case anyone else is having the same issue, this was a solution that worked for me.
Thank you for your attention and help.
Ken

Passing value of URL link by NSString

The problem is here: when i try to pass link string from one controller to another, somehow it lose part of text, and finally my NSString, which i named self.entityLinkToWiki = Московский_Кремль, not http://ru.wikipedia.org/wiki/Московский_Кремль as it should be!
My app have structure of table view, firstly, it load tableview, after that it pass values from XML which depends of indexPath.row. All values except web links works just fine, and i will explain the way i did this. There is part of web link in xml file:
http://ru.wikipedia.org/wiki/Московский_Кремль
To get that, i did following:
In "first" (tableView list) controller i pass values like this:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"detailSegue"]){
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
NSString *string = [[self.listOfPlaceDetails objectAtIndex:indexPath.row]objectForKey:#"description"];
DetailViewController *detailController = segue.destinationViewController;
detailController.descriptionStringShort =string;
....a lot of code here
detailController.fourthPhotoString = [[self.listOfPlaceDetails objectAtIndex:indexPath.row] objectForKey:#"imageFourth"];
detailController.webLink = [[self.listOfPlaceDetails objectAtIndex:indexPath.row]
objectForKey:#"webpage"];
}
}
Image link has nothing to do with web link, i just post it, so you get picture that i pass values in this way and it work fine. Then, i pass values from "detail" controller to controller, that finally have UIWebView:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
.... a lot of code here
if ([segue.identifier isEqualToString:#"WebSegue"]){
WebViewController *webController = segue.destinationViewController;
webController.entityLinkToWiki = self.webLink;
NSLog(#"WebSegue link is %#", self.webLink);
}
}
And NSLog says, that self.webLink is already - Московский_Кремль (incorrect value). Therefore, i didn't get correct link for my UIWebView. So now, my question is - how to get correct link passing it like i did? I don't want to bother load it from XML file in final controller, because i need information of indexPath.row, to get correct value (i have a lot of entities in XML file).
Thank you for any help you provide

PreLoad UIWebView on a not yet displayed UIViewController

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?

Displaying a PDF with UIWebView Not Working

So, I realize that there have been many questions regarding using a UIWebView to display a PDF in an app (on the iPad). I have reviewed everything I can find but I can't seem to find any satisfaction.
What I'm trying to do is very basic so I really don't know why it's not working. All I need to do is display a locally stored PDF in the UIWebView. Essentially, all I'm getting is a black screen. If someone could take a look at my code, I'd really appreciate it. Thanks.
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Segue to the materials screen.
if ([segue.identifier isEqualToString:#"materials"]) {
PDFViewController *pdfViewController = [segue destinationViewController];
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
int row = [path row];
Lesson *selected = [purchasedLessons objectAtIndex:row];
pdfViewController.selectedLesson = selected ;
//Start Load PDF in UIWebView
pdfViewController.pdfWindowTitle.title = selected.titleAndSubtitle;
pdfViewController.pdfWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 1024, 704)];
NSString *urlAddress = [[NSBundle mainBundle] pathForResource:#"moonlightSonataFirstMovement" ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[pdfViewController.pdfWebView loadRequest:requestObj];
//End Load PDF
}
}
I've checked to see if my object is bring passed into the scene properly (and it is) and if I am getting a proper request out; i'm getting:
<NSURLRequest file://localhost/Users/MYCOMPUTER/Library/Application%20Support/iPhone%20Simulator/5.0/Applications/AB161E57-D942-44C2-AA75-030087820BED/iLessons%20Piano.app/moonlightSonataFirstMovement.pdf>
Also, I've have this error message:
iLessons Piano[23080:f803] DiskImageCache: Could not resolve the absolute path of the old directory.
Additionally, the NSLog message gets printed out 8 times for some reason.
The only thing I can think of is that I need to do this loading when I call my prepareForSegue function in the previous scene. That or use a different method like Quartz. But I'd rather use a UIWebView since all I really need to do is display and allow scrolling.
DiskImageCache: Could not resolve the absolute path of the old
directory.
This one isn't the real reason the app crashes. This warning can be fixed by assigning the view in the Storyboard. It seems like it's connected already but it's grey. So assign it again and it will be fine.
The real issue for me was that the PDF images were 300 DPI and it took too long to load the application. Since the debugger keeps the app from crashing it seems to work fine but the loading will take too long without the debugger and will result in a timeout crash.
There's a few things you can do. You can downscale your PDF which might be a good thing anyway since older device are even slower and it's nice to support those as well. But what really fixes it is by delaying the PageView initialization. Here's what I did:
In the RootViewController I moved the code from the viewDidLoad to a new function setupPageViewer.
And put this in the viewDidLoad:
[self performSelector:#selector(setupPageViewer) withObject:nil afterDelay:0.0f];
The delay 0.0 means it will be taken care of in the next frame which gives you the opportunity to show a loading indicator.
Enjoy!
im not sure about whats going on .. you view and webView both holding nil value.. as i told you im not involved in the storyBoard yet .. but you as a workaround solution maybe this fix your problem
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1027, 768)];
pdfViewController.view = view;
pdfViewController.pdfWebView = [[UIWebView alloc] initWithFrame:pdfViewController.view.frame];
NSString *urlAddress = [[NSBundle mainBundle] pathForResource:#"moonlightSonataFirstMovement" ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[pdfViewController.view addSubview:pdfViewController.pdfWebView];
[pdfViewController.pdfWebView loadRequest:requestObj];
As a workaround, you can disable or remove your 'All Exceptions' breakpoint. This might make debugging a little more difficult, but it's not as bad as having to relaunch the application all the time.
This is the breakpoint causing the issue. I had set it so long ago that I'd forgotten it was there

Problems using UIWebView to implement info page

In my app I have a UIWebView-based view controller showing info and credits about the app itself. As part of the info, the app version is displayed, as retrieved from the infoDictionary of the mainBundle. So this string is not in the original HTML; it is set programmatically, by replacing a placeholder.
Therefore the sequence is:
1) I load the HTML into a NSString
2) I replace the placeholder with the actual version
3) I show the resulting string in UIWebView by invoking method loadHTMLString:baseURL:
The HTML also has hyperlinks to some web pages in the internet.
Everything is fine, but for this problem:
If I touch a hyperlink, and therefore navigate to the corresponding web page, I will not be able to go back to my original info page (canGoBack returns NO, goBack does nothing).
Any suggestion?
Thanks in advance.
In the end, I have found a satisfactory solution, that I would like to share.
1) I changed the HTML code of my info page, by inserting the following code where I want to show the app version:
<p>App version:
<script type="text/javascript">
var args = document.location.search.substring(1).split("&");
for (var i in args) {
var nameValue = args[i].split("=");
if (nameValue[0] == "version")
var version = nameValue[1];
}
if (version)
document.write(version);
</script>
</p>
This piece of code will parse the query part of the URL, looking for a "version=<version>" argument, and use its value to show the app version.
2) When preparing the URL to be used by the UIWebView, I retrieve the app version from the main bundle:
NSString* version = [[[NSBundle mainBundle] infoDictionary] objectForKey:#"CFBundleVersion"];
Then I append it at the end of my URL string:
NSString* urlString = [NSString stringWithFormat:#"<my_info_page>.html?version=%#", version];
3) Finally, I use the UIWebView to show the page:
NSURL* url = [[NSURL alloc] initWithString:urlString];
NSURLRequest* request [[NSURLRequest alloc] initWithUrl:url];
[mWebView loadRequest:urlRequest];
[urlRequest release];
[url release];
where mWebView is an IBOutlet, of type UIWebView, properly connected to the web view in the XIB file.
It works correctly on my iPhone 4, including the back/forward functions of the web view when following hyperlinks, while keeping the user inside the app.
I would break out of the app and load Safari in this case:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
[[UIApplication sharedApplication] openURL:request.URL];
return NO;
}
The only downside is that the user comes out of your app. You could put an alertview in that method to warn them first...
Alternatively, you'll have to code your own back button.

Resources