0

I am trying to check the current URL in a UIWebView with the following code:

- (void)webViewDidFinishLoad:(UIWebView *)webView {

    NSString *currentURL = webView.request.URL.absoluteString;

    NSLog(@"Current URL: %@", currentURL);

    NSString *loginpage = @"file:///var/mobile/Applications/652BAC0C-DEAE-4695-9195-BE617B9D5106/School%20VLE%20Business.app/login.html";

    if([currentURL isEqualToString:loginpage])
    {
        NSLog(@"Inputting user details...");

        NSString *result;
        result = [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"$('#usernamefield').val('%@');", username]];
        result = [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"$('#passwordfield').val('%@');", password]];
        result = [webView stringByEvaluatingJavaScriptFromString:@"$('#submitbutton').click();"];

        NSLog(@"Logged in!");

    }
}

But the code in the local URL (652BAC0C-DEAE-4695-9195-BE617B9D5106 for me) isn't going to be the same for everybody, so how can I do the same check but without the long code?

4 Answers 4

8

This would work as a more general solution of determining whether a given URL is from the local file system or is being loaded remotely:-

NSString *str = @"file://var/mobile/Applications/652BAC0C-DEAE-4695-9195-BE617B9D5106/School%20VLE%20Business.app/login.html";
NSURL *url = [NSURL URLWithString:str];
if ([[url scheme] isEqualToString:@"file"]) {
    // URL is a (local) file...
}

(Which answers the subject of your question How can I check if URL is local file?)

Sign up to request clarification or add additional context in comments.

Comments

3
NSString *loginpage = [[NSBundle mainBundle] pathForResource:@"login" ofType:@"html"];

Since you're using spaces in your app's (which I recommend you not to do anymore), you can use the following code to simulate those %20 escape strings:

NSString *loginpage = [[[NSBundle mainBundle] pathForResource:@"login" ofType:@"html"] stringByReplacingOccurrencesOfString:@"%20" withString:@" "];

1 Comment

Comment out the NSLog(...) line and put the following line under NSString *loginpage ...: NSLog(@"URL: [%@]:[%@]", currentURL, loginpage);
2

You can use these lines to search in a string:

if ([your_complete_string rangeOfString:@"YOUR_SEARCH_STRING"].location != NSNotFound){

    // Do your stuff here if found

}

1 Comment

Though this isn't really what he asked, it really can be a good solution to his question.
-1

You can find using fileReferenceUrl

//if local file
NSURL *url = [NSURL fileURLWithPath:songsList[index]];
if(url.fileReferenceURL != nil){
    //is local URL
}else{
    //Is live
   url = [NSURL URLWithString:songsList[index]];
}

1 Comment

read proper question after giving the answers. The question of URL check nil or not, check URL is the Local file path or not.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.