Evgeniy Kleban
Evgeniy Kleban

Reputation: 6940

Web page didn't open in iOS app

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 -

enter image description here

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).

Upvotes: 0

Views: 142

Answers (1)

raurora
raurora

Reputation: 3663

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];

}

Upvotes: 1

Related Questions