Olex
Olex

Reputation: 1656

Restrict UIWebView to stay on Facebook page

I need to make a simple UIWebView for iPad, for event when users will login with their accounts and be able to see fixed Facebook public page and be able to scroll this page, comment or like posts but not be able to go to somewhere else except this page. So I am using

-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
    {
        NSURL *url = request.URL;
        NSString *urlString = url.absoluteString;
        NSRange range = [urlString rangeOfString:@"http://m.facebook.com/groups/10621329941138"];
        if (range.location != NSNotFound)
        {
            return YES;
        }
        else
        {
            return NO;
        }
    }

Everytime when user end his session hostess will logout from this page to login page. After some experiments I stuck with a few issues:

Thanks in advance!

Upvotes: 0

Views: 349

Answers (2)

trumpetlicks
trumpetlicks

Reputation: 7065

I have done something like this before and I would suggest something like

return [urlString hasPrefix:@"http://m.facebook.com/"];

This is potentially a safer option as well, as it limits to a domain versus simply a keyword check for the word facebook anywhere in the path of the full URL.

You could get a bit more fancy and check for "facebook.com" anywhere after the "http:// and just prior to the second "/". Can probably also use the baseURL method of the NSURL to help here.

Also, are you absolutely sure you have your UIWebView delegate setup properly?

Upvotes: 1

jjv360
jjv360

Reputation: 4140

Try this:

if ([url.host.lowercaseString rangeOfString:@"facebook"].location == NSNotFound) {

    // Don't go since we're somewhere else now
    return NO;

} else {

    // We can go here
    return YES;

}

Upvotes: 1

Related Questions