Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

RipsMctits

macrumors regular
Original poster
Oct 19, 2011
125
24
Amateur developer here again. If it's not one thing, it's another. I conceded defeat to setting up my own push notifications, but this new problem is a deal breaker. I'm using UIWebView to display an updated app page on my server. It looks just like a page in the app, but I'm using web view to easily update the page in the background from my server. On this page is an image map with a link to "http://maps.apple.com/?q=&daddr=xxxxxxxxxx&saddr=current,+location".

Opening from Safari on OS X and iOS both kick out to the Apple maps app like they should. However, inside of my UIWebView, the web version of google maps is loaded (with the wrong location). Why? I'm using Apple's maps link. I have no problem using google maps, but I would prefer to automatically kick out to Apple Maps. My code for this part looks like this:

Code:
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSURL *url = [NSURL URLWithString:@"http://xxxxxxxx.com/eesales/sample/index.html"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [_webView loadRequest:request];
    
    if (([url.scheme isEqualToString:@"http"] || [url.scheme isEqualToString:@"https"]) && [url.host isEqualToString:@"maps.apple.com"]) { //it's an apple maps app request
        NSLog(@"Attempting Apple Maps app open");
        [[UIApplication sharedApplication]openURL:url];
    }
    
}

Notice the 'if' statement on the end, which I got from stackOverflow, that is supposed to be handling my URL request and pushing it to apple maps, yes? Why isn't it doing what it's supposed to be doing?

I would be okay with using Google Maps the way it is, if the user's location was correct, but I've tried legitimate methods for using CoreLocation and they don't seem to affect UIWebView anyway. So I just want to kick out to Apple Maps from my webpage and make this easy on myself. How do I do it? What am I doing wrong? Why is UIWebView so finicky?
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Why are you trying to use openURL: inside your viewDidLoad:? Shouldn't you be trying to open the URL in response to the click on the image map?
 

RipsMctits

macrumors regular
Original poster
Oct 19, 2011
125
24
Why are you trying to use openURL: inside your viewDidLoad:? Shouldn't you be trying to open the URL in response to the click on the image map?

Probably. I don't know what I'm doing.

If you're talking specifically about the url scheme code, I put it there because it's not valid anywhere else I tried to put it.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Probably. I don't know what I'm doing.

Here's your opportunity to step away from the real coding and take the time to learn and get comfortable with the basics of iOS / Objective-C programming. What resources, if any, are you using to educate yourself? Please be specific.

Yes, sometimes you do need to "try things" even if you don't entirely understand what they do. But I don't think this is one of those times.
 

RipsMctits

macrumors regular
Original poster
Oct 19, 2011
125
24
I have plans for starting from scratch after I'm finished with this project, but I'm kind of in the **** here so I'm just trying to figure it out.
 

JohnsonK

macrumors regular
Mar 6, 2014
142
0
I have plans for starting from scratch after I'm finished with this project, but I'm kind of in the **** here so I'm just trying to figure it out.

Without the basics, I don't think you will be able to finish any project. And that's not just for programming projects.

"After I build this bridge I am going to start learning how to build bridges"
 

kage207

macrumors 6502a
Jul 23, 2008
971
56
Amateur developer here again. If it's not one thing, it's another. I conceded defeat to setting up my own push notifications, but this new problem is a deal breaker. I'm using UIWebView to display an updated app page on my server. It looks just like a page in the app, but I'm using web view to easily update the page in the background from my server. On this page is an image map with a link to "http://maps.apple.com/?q=&daddr=xxxxxxxxxx&saddr=current,+location".

Opening from Safari on OS X and iOS both kick out to the Apple maps app like they should. However, inside of my UIWebView, the web version of google maps is loaded (with the wrong location). Why? I'm using Apple's maps link. I have no problem using google maps, but I would prefer to automatically kick out to Apple Maps. My code for this part looks like this:

Code:
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSURL *url = [NSURL URLWithString:@"http://xxxxxxxx.com/eesales/sample/index.html"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [_webView loadRequest:request];
    
    if (([url.scheme isEqualToString:@"http"] || [url.scheme isEqualToString:@"https"]) && [url.host isEqualToString:@"maps.apple.com"]) { //it's an apple maps app request
        NSLog(@"Attempting Apple Maps app open");
        [[UIApplication sharedApplication]openURL:url];
    }
    
}

Notice the 'if' statement on the end, which I got from stackOverflow, that is supposed to be handling my URL request and pushing it to apple maps, yes? Why isn't it doing what it's supposed to be doing?

I would be okay with using Google Maps the way it is, if the user's location was correct, but I've tried legitimate methods for using CoreLocation and they don't seem to affect UIWebView anyway. So I just want to kick out to Apple Maps from my webpage and make this easy on myself. How do I do it? What am I doing wrong? Why is UIWebView so finicky?
Why aren't you using Google Maps iOS SDK or MapKit? It'd be a lot easier and update the view in the App instead of moving them to other apps.

Edit: Found it
Code:
if (([url.scheme isEqualToString:@"http"] || [url.scheme isEqualToString:@"https"]) && [url.host isEqualToString:@"maps.apple.com"])
The url.host is set to your 'xxxxxxxx.com' so it is false.

Also, you really should take JohnsonK's advice.

Lastly, the reason I'm assussuming the Web View of the Google Maps is displaying the wrong location due to variable types between CoreLocation and your JavaScript.
 
Last edited:

RipsMctits

macrumors regular
Original poster
Oct 19, 2011
125
24
Why aren't you using Google Maps iOS SDK or MapKit? It'd be a lot easier and update the view in the App instead of moving them to other apps.

Edit: Found it
Code:
if (([url.scheme isEqualToString:@"http"] || [url.scheme isEqualToString:@"https"]) && [url.host isEqualToString:@"maps.apple.com"])
The url.host is set to your 'xxxxxxxx.com' so it is false.

Also, you really should take JohnsonK's advice.

Lastly, the reason I'm assussuming the Web View of the Google Maps is displaying the wrong location due to variable types between CoreLocation and your JavaScript.

I only put the X's in there for privacy reasons. When you talk about using MapKit and such, I COULD do that easily if I didn't have to update the page the way I'm doing it. I know how to use maps outside of WebView. I've used it in tutorials, and I've made a standalone maps app just to figure it all out. But that implementation being totally inside the app, it's a lot more straightforward than what I'm trying to do here.

Using my own brain, I figured the easiest way to do what I needed would be to update the pages via the WebView like I'm doing now. Makes sense to me. What doesn't make sense to me is why WebView isn't cooperating with what I'm trying to do, so I made the thread.

All I needed to do, was click a link on my page and have it handled by the maps app. Since I couldn't figure that part out, I figured I could just fix the problem by using location services. That didn't work how I expected it to either. I know how to use MapKit/Location services from completely inside the app. That works fine. WebView is the X factor in this equation.

I know I sound like a complete noob here, but I know some things. I wasn't originally planning on being a programmer. I just kind of fell into it, and now I'm doing the best I can. My wheelhouse is graphic design. I wasn't exactly expecting to be in this position I am in now. I understand how frustrating it can be for people like me to come in here and ask stupid questions and such. However, I didn't expect this reaction. This is something akin to a music major telling a new musician that he better learn music theory before he tries to write any songs.

I really am going to be taking this back to square one after I figure this problem out, but I don't feel like I just need to stop here and start there IMMEDIATELY, before I even know the why's and how's of this issue. Should I learn music theory before someone tells me how to play some chords? I didn't have any trouble doing other things on my own. I have some previous experience in web development and such...I'm not completely new to ALL code. I really want to learn. I understand that I need to start from the beginning now better than before, because I tried it on my own first.

I'm not just going to sit on this while I spend the next several months taking courses and reading books. I want to figure this out so I know what the problem is now, not months later. For one small issue, that is some serious pre-requisite.
 
Last edited:

kage207

macrumors 6502a
Jul 23, 2008
971
56
I only put the X's in there for privacy reasons. When you talk about using MapKit and such, I COULD do that easily if I didn't have to update the page the way I'm doing it. I know how to use maps outside of WebView. I've used it in tutorials, and I've made a standalone maps app just to figure it all out. But that implementation being totally inside the app, it's a lot more straightforward than what I'm trying to do here.
Ok. I don't think you understood me. I know you put the xxxxxxx.com there for privacy things.

Code:
&& [url.host isEqualToString:@"maps.apple.com"]

This is what I'm talking about. Since url.host is actually equal to xxxxxxxx.com because you set it here in this line of code:

Code:
NSURL *url = [NSURL URLWithString:@"http://xxxxxxxx.com/eesales/sample/index.html"];

You never changed the url.host property to be set to "maps.apple.com". So this condition is always false and since you are using && (an and statement) a T & F = F.
 

RipsMctits

macrumors regular
Original poster
Oct 19, 2011
125
24
Ok. I don't think you understood me. I know you put the xxxxxxx.com there for privacy things.

Code:
&& [url.host isEqualToString:@"maps.apple.com"]

This is what I'm talking about. Since url.host is actually equal to xxxxxxxx.com because you set it here in this line of code:

Code:
NSURL *url = [NSURL URLWithString:@"http://xxxxxxxx.com/eesales/sample/index.html"];

You never changed the url.host property to be set to "maps.apple.com". So this condition is always false and since you are using && (an and statement) a T & F = F.

Thank you. Seriously. Changing that part of the code to xxxxxx.com worked, but it just makes it so when I click to get to the view controller I'm using for the webview, it kicks what should be in my web view out to Safari. But I need it to wait until I actually click the link on my map before it starts handing off URLs to shared applications. Progress! Now I understand a bit better what the code is doing. I just am not utilizing it correctly.

I know why it's kicking out to Safari (because I told it to kick anything from xxxxxx.com out to a shared application). But that's a problem, because the index I'm loading in the web view is from xxxxxx.com.

I thought the first part of the code was just defining what to load inside the web view, and the second part was telling the app how to handle the URL to kick out to Maps. I didn't know they had anything to do with each other.

How do I define the url.host as maps.apple.com without changing the previous code for the xxxxxx.com/etc/etc/index.html?

The code I got for the url.scheme stuff came from here, but using the BOOL just throws up errors because of things not being defined, so I put it in the section for the web view, where they are defined.
 
Last edited:

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
...but it just makes it so when I click to get to the view controller I'm using for the webview, it kicks what should be in my web view out to Safari. But I need it to wait until I actually click the link on my map before it starts handing off URLs to shared applications.

That's probably because you put that code in viewDidLoad and not in code that responds to a click on the image map.
 

kage207

macrumors 6502a
Jul 23, 2008
971
56
How do I define the url.host as maps.apple.com without changing the previous code for the xxxxxx.com/etc/etc/index.html?

The code I got for the url.scheme stuff came from here, but using the BOOL just throws up errors because of things not being defined, so I put it in the section for the web view, where they are defined.

Replace your code of:

Code:
 NSURL *url = [NSURL URLWithString:@"http://xxxxxxxx.com/eesales/sample/index.html"];

to:

Code:
 NSURL *url = [NSURL URLWithString:@"http://maps.apple.com/?q=&daddr=xxxxxxxxxx&saddr=current,+location"];

The problem is that you are trying to open a WebView in Apple Maps. This does not work as Apple Maps does not render web views. It works on Safari for iOS and OS X because the line 'http://maps.apple.com/?q=&daddr=xxxxxxxxxx&saddr=current,+location' is a link inside your page. If a user clicks it, the Map opens in Apple Maps, right? You can either open Safari and then have it kick to Apple Maps via the link click or you can have it open directly to Apple maps by using the above link that you use in your xxxxxxxx.com/etc/etc/index.html.

Also, don't forget to change this:

Code:
if (([url.scheme isEqualToString:@"http"] || [url.scheme isEqualToString:@"https"]) && [url.host isEqualToString:@"maps.apple.com"]) { //it's an apple maps app request
        NSLog(@"Attempting Apple Maps app open");
        [[UIApplication sharedApplication]openURL:url];
    }

to:

Code:
if ((url.scheme isEqualToString:@"http"] && [url.host isEqualToString:@"maps.apple.com"]) { //it's an apple maps app request
        NSLog(@"Attempting Apple Maps app open");
        [[UIApplication sharedApplication]openURL:url];
    }

Though technically you don't need the above. You just need:

Code:
[[UIApplication sharedApplication]openURL:url];

EDIT: I also think that the above code will make it work by the means of YourApp --> Safari --> Apple Maps.

You need to look into how the file system works and passing an app some URL parameters for iOS to open directly to Apple Maps from your app.
 

RipsMctits

macrumors regular
Original poster
Oct 19, 2011
125
24
Replace your code of:

Code:
 NSURL *url = [NSURL URLWithString:@"http://xxxxxxxx.com/eesales/sample/index.html"];

to:

Code:
 NSURL *url = [NSURL URLWithString:@"http://maps.apple.com/?q=&daddr=xxxxxxxxxx&saddr=current,+location"];

The problem is that you are trying to open a WebView in Apple Maps. This does not work as Apple Maps does not render web views. It works on Safari for iOS and OS X because the line 'http://maps.apple.com/?q=&daddr=xxxxxxxxxx&saddr=current,+location' is a link inside your page. If a user clicks it, the Map opens in Apple Maps, right? You can either open Safari and then have it kick to Apple Maps via the link click or you can have it open directly to Apple maps by using the above link that you use in your xxxxxxxx.com/etc/etc/index.html.

Also, don't forget to change this:

Code:
if (([url.scheme isEqualToString:@"http"] || [url.scheme isEqualToString:@"https"]) && [url.host isEqualToString:@"maps.apple.com"]) { //it's an apple maps app request
        NSLog(@"Attempting Apple Maps app open");
        [[UIApplication sharedApplication]openURL:url];
    }

to:

Code:
if ((url.scheme isEqualToString:@"http"] && [url.host isEqualToString:@"maps.apple.com"]) { //it's an apple maps app request
        NSLog(@"Attempting Apple Maps app open");
        [[UIApplication sharedApplication]openURL:url];
    }

Though technically you don't need the above. You just need:

Code:
[[UIApplication sharedApplication]openURL:url];

EDIT: I also think that the above code will make it work by the means of YourApp --> Safari --> Apple Maps.

You need to look into how the file system works and passing an app some URL parameters for iOS to open directly to Apple Maps from your app.

I appreciate all the in-depth responses and help. I need a way to do this without changing the "http://xxxxxxxx.com/eesales/sample/index.html" because that is what I need to open in the web view. Changing that part of the code causes maps to kick out just fine, but it is skipping the view controller with the web view on it completely now and just going to maps from the static address, and that isn't exactly what I need to be able to do.

That address will change weekly or more, which is why I need to display the web page in the web view, then have the app kick out to maps after clicking the link on my web page. The address cannot be static, as it has to be updated without the user updating the app, which is why I'm using the web view to display the changing pages and addresses.

Obviously I'm not telling the app to kick out to a shared application in the right place. I just don't know how to define the action that I'm trying to do. Namely, when the link is clicked in the web view, THEN kick out to maps with the URL clicked on the page. It works natively from Safari on iOS and OS X. I don't know why Web View has to be so difficult with this function.
 
Last edited:

kage207

macrumors 6502a
Jul 23, 2008
971
56
I appreciate all the in-depth responses and help. I need a way to do this without changing the "http://xxxxxxxx.com/eesales/sample/index.html" because that is what I need to open in the web view. Changing that part of the code causes maps to kick out just fine, but it is skipping the view controller with the web view on it completely now and just going to maps from the static address, and that isn't exactly what I need to be able to do.

That address will change weekly or more, which is why I need to display the web page in the web view, then have the app kick out to maps after clicking the link on my web page. The address cannot be static, as it has to be updated without the user updating the app, which is why I'm using the web view to display the changing pages and addresses.

I'd need to see more of your code, such as where you are calling the web view and how you are calling it.

But you should really read this: https://developer.apple.com/library...neOS/DisplayWebContent/DisplayWebContent.html

You're missing this line mainly:

Code:
[self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]]];
 
Last edited:

RipsMctits

macrumors regular
Original poster
Oct 19, 2011
125
24
I appreciate the link. I'm reading it now. As for how I'm calling the web view and how, I have a view controller with three buttons on it, each leading to a different view controller. One of the view controllers is displaying the web view, the other two are displaying separate view controllers with different information on them. The one I am concerned with has the name SaleViewController.

I have my SaleViewController.h with this:

Code:
@property (strong, nonatomic) IBOutlet UIWebView *webView;

And in my SaleViewController.m, I have this:

Code:
#import "SaleViewController.h"


@interface SaleViewController ()

@end

@implementation SaleViewController

- (void)viewWillAppear:(BOOL)animated

{
    self.navigationController.navigationBar.hidden = NO;
    
    _webView.scrollView.scrollEnabled = NO;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSURL *url = [NSURL URLWithString:@"http://xxxxxx.com/eesales/sample/index.html"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [_webView loadRequest:request];
    
    }

That is all the code I have currently for the web view.

I took out anything related to handling the URLS or anything for now.

This is the base code I have that actually functions as it is supposed to. Any functionality I need would be added to this.

----------

I read the documentation you provided, and now my code for the web view looks like this:

Code:
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://xxxx.com/eesales/sample/index.html"]]];
    
    }

Much cleaner. :) Thanks. Definitely going to add in some of the other stuff in that documentation as well. Very useful.
 
Last edited:

kage207

macrumors 6502a
Jul 23, 2008
971
56
...
----------

[/COLOR]I read the documentation you provided, and now my code for the web view looks like this:

Code:
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://xxxx.com/eesales/sample/index.html"]]];
    
    }

Much cleaner. :) Thanks. Definitely going to add in some of the other stuff in that documentation as well. Very useful.

No problem.
 

RipsMctits

macrumors regular
Original poster
Oct 19, 2011
125
24
I don't know if that means you're giving up on the consultation for now, but I will still post this if you or anyone else has any input. I'm currently perusing fiverr for solutions in the event I cannot figure this out.

I tried utilizing some of the new methods from the documentation, but it didn't really help matters, but I'm glad they're in there. The code is cleaner, and more things are accounted for, but I've ultimately gone around the squares back to one again.

I don't know why this has to be so complicated, haha. A maps link kicks out to Maps automatically just fine in mobile Safari and OS X, and yet I can't get anywhere with this in the WebView. I've been researching this issue for weeks now, posting a handful of different places, and I still can't get around it.
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
The normal way to intercept clicks on links in a UIWebview is by use of the UIWebView delegate protocol. Set your view controller as the delegate, implement webView:shouldStartLoadWithRequest:navigationType: and then that method will be called for any type of load request, like clicks on a link in the page. Put your code there to determine if the link is one of your map links. If it is then do what you want, like call loadURL, and return NO. Otherwise return YES.

This seems to be what you're asking about.
 

RipsMctits

macrumors regular
Original poster
Oct 19, 2011
125
24
The normal way to intercept clicks on links in a UIWebview is by use of the UIWebView delegate protocol. Set your view controller as the delegate, implement webView:shouldStartLoadWithRequest:navigationType: and then that method will be called for any type of load request, like clicks on a link in the page. Put your code there to determine if the link is one of your map links. If it is then do what you want, like call loadURL, and return NO. Otherwise return YES.

This seems to be what you're asking about.

I will give that a shot. Thanks buddy. This is driving me up the wall. I hope this one works, lol.
 

RipsMctits

macrumors regular
Original poster
Oct 19, 2011
125
24
That seems to be similar to the link I mentioned earlier, and I'm too unfamiliar with things to get that to work correctly. I have <UIWebViewDelegate> set at the top of my .m file like I should. Using this code from the page I linked:

Code:
if (([url.scheme isEqualToString:@"http"] || [url.scheme isEqualToString:@"https"]) && [url.host isEqualToString:@"maps.apple.com"]) { //it's an apple maps app request
    NSLog(@"Attempting Apple Maps app open");
    [[UIApplication sharedApplication]openURL:url];
    return NO;
}


So it looks like:

Code:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
    if (([url.scheme isEqualToString:@"http"] || [url.scheme isEqualToString:@"https"]) && [url.host isEqualToString:@"maps.apple.com"]) { //it's an apple maps app request
        NSLog(@"Attempting Apple Maps app open");
        [[UIApplication sharedApplication]openURL:url];
        return NO;
    }
}

Xcode wants me to define 'url' in both instances, but I don't know how to define it in general terms so I don't have a static address every time. The url is going to change pretty often, so I don't really want to define that. If I could just use the part of the code above and have it work that way, it would be perfect.
 

JohnsonK

macrumors regular
Mar 6, 2014
142
0
That's what we meant by learning the basics first. Everything depends on the basics.

I believe you are stitching together bits and pieces of code from all over the web, from completely different solutions and ultimately not understanding what the code is doing at all. Try to calmly read it, line by line, see if it makes sense. It doesn't to me!

You original question was, if I am not wrong, why you were clicking a maps.apple.com link and maps.google.com was loading instead.

If that is still the case, are you sure it was a link pointing to maps.apple.com? CHECKING whether an url is maps.apple.com won't magically make it load maps.apple.com if its not.

If the link you clicked points to maps.google.com, it will load maps.google.com. This is where you should intercept the click by using the UIWebView delegate protocol and only then, forward it to maps.apple.com with the appropriate extracted coordinates from the link
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
@Rips, yes that link is doing what you need to be doing. But he doesn't give all the steps because he knows what they are. This tutorial isn't great but shows all the steps

http://hayageek.com/uiwebview-uiwebviewdelegate-tutorial/

BTW, the request has the URL you need in itself.

You can combine the two or find another tutorial so you can figure out what you need to do to make the delegate protocol work.
 

kage207

macrumors 6502a
Jul 23, 2008
971
56
I don't know if that means you're giving up on the consultation for now, but I will still post this if you or anyone else has any input. I'm currently perusing fiverr for solutions in the event I cannot figure this out.

I tried utilizing some of the new methods from the documentation, but it didn't really help matters, but I'm glad they're in there. The code is cleaner, and more things are accounted for, but I've ultimately gone around the squares back to one again.

I don't know why this has to be so complicated, haha. A maps link kicks out to Maps automatically just fine in mobile Safari and OS X, and yet I can't get anywhere with this in the WebView. I've been researching this issue for weeks now, posting a handful of different places, and I still can't get around it.
You made it seem like I solved your problem.

Xcode wants me to define 'url' in both instances, but I don't know how to define it in general terms so I don't have a static address every time. The url is going to change pretty often, so I don't really want to define that. If I could just use the part of the code above and have it work that way, it would be perfect.
You've never mentioned this. How do you solve the changing (updating) URL address problem on site?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.