0

**I am new to iOS development. I am developing small application which can open specified SharePoint site URL without manually passing require credential. The URL I am trying to open needs credential but I want to embed these credential to the request I will make to open the URL ins UIWebView control. I don't want to open the URL in Safari.

Would you please help me finding the solution?**

GDroid
  • 1,254
  • 1
  • 16
  • 36

1 Answers1

4

You can use -connection:didReceiveAuthenticationChallenge: delegate for your problem. First make a normal NSURLConnection as follow,

- (void) someMethod
{
    NSURLRequest* request = [[NSURLRequest alloc] 
         initWithURL:[NSURL urlWithString:@"Your sharepoint web url"]

    NSURLConnection* connection = [[NSURLConnection alloc] 
         initWithRequest:request delegate:self];

    [connection release];
    [request release];
}

After that your receive the call back. In here you should handle the challenge of credentials.

- (void) connection:(NSURLConnection *)connection 
      didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    //  Make sure to use the appropriate authentication method for the server to
    //  which you are connecting.
    if ([[challenge protectionSpace] authenticationMethod] == 
             NSURLAuthenticationMethodBasicAuth)
    {
            //  This is very, very important to check.  Depending on how your 
            //  security policies are setup, you could lock your user out of his 
            //  or her account by trying to use the wrong credentials too many 
            //  times in a row.
        if ([challenge previousFailureCount] > 0)
        {
            [[challenge sender] cancelAuthenticationChallenge:challenge];

            UIAlertView* alert = [[UIAlertView alloc] 
                            initWithTitle:@"Invalid Credentials" 
                                  message:@"The credentials are invalid." 
                                 delegate:nil 
                        cancelButtonTitle:@"OK" 
                        otherButtonTitles:nil];
            [alert show];
            [alert release];      
        }
        else
        {
            [challenge useCredential:[NSURLCredential 
                   credentialWithUser:@"someUser" 
                             password:@"somePassword" 
                          persistence:NSURLCredentialPersistenceForSession 
           forAuthenticationChallenge:challenge]];
        }
    }
    else
    {
        //  Do whatever you want here, for educational purposes, 
            //  I'm just going to cancel the challenge
        [[challenge sender] cancelAuthenticationChallenge:challenge];
    }
}

Update Use this code for this link.

 -(void)viewDidLoad{
        NSString *strWebsiteUlr = [NSString stringWithFormat:@"http://www.roseindia.net"];

        NSURL *url = [NSURL URLWithString:strWebsiteUlr];

       NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

       [webView loadRequest:requestObj];
            [webview setDelegate:self]
     }

In header file

@interface yourViewController : UIViewController<UIWebViewDelegate>{
  Bool _authed;
}

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

Community
  • 1
  • 1
Hasintha Janka
  • 1,608
  • 1
  • 14
  • 27
  • Hi thanks for your reply. But I am not sure that how will I get - (void) connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge method in my code? Also I have placed UIWebView in ViewController. Do I have to add any inheritance or super class for my viewcontroller class? – GDroid Feb 28 '13 at 04:25
  • It is not a problem. You can use it's in UIWebview. Check this link.http://stackoverflow.com/questions/1769888/how-to-display-the-authentication-challenge-in-uiwebview – Hasintha Janka Feb 28 '13 at 04:46
  • Would you mind sending me one page app having this code in please as sample? I am really struggling since long... gmpat4u@gmail.com – GDroid Feb 28 '13 at 05:00
  • That link include full code required for your problem. did you test this code. – Hasintha Janka Feb 28 '13 at 05:14
  • And don't forget to set delegate in UIWebview. – Hasintha Janka Feb 28 '13 at 05:15
  • If I copy paste that code in my viewcontroller.m file; it shows me error at many places. Error with red dot. – GDroid Feb 28 '13 at 05:18
  • You have to change the variable as you need. Because i don't know what are the variable name in your code. – Hasintha Janka Feb 28 '13 at 05:21
  • I set delegate in UIWebView to ViewController ( container ViewController of that UIWebView ) – GDroid Feb 28 '13 at 05:21
  • Ofcourse I did variable changes but it shows error in Keywords not in variables. – GDroid Feb 28 '13 at 05:25
  • I can send you my application code if you like and you can update it and send me back if you like. – GDroid Feb 28 '13 at 05:26
  • OK. Upload it to some website(dropbox) and give me the link. – Hasintha Janka Feb 28 '13 at 05:34
  • There is a function called "LoadUrlWithAuthenticationChallenge" in "DTRViewController.m" which you can modify OR whatever way you want to modify. Thanks. – GDroid Feb 28 '13 at 05:41
  • https://www.dropbox.com/s/zeqf6eccir3y7jz/WebViewTest%202.zip, I can't est without url. But i think this will be help. – Hasintha Janka Feb 28 '13 at 05:50
  • Thanks!! This seems working. But first time it does not authenticate and second time if i press button it loads my SharePoint URL... Let me know if any quick tip for this, otherwise I will figure out. Thanks for your quick awesome support...!! – GDroid Feb 28 '13 at 07:14
  • I think you are experiencing some delay for loading. Put break points for all methods and try to understand it or press one time wait few seconds. If i'm correct it should be working. – Hasintha Janka Feb 28 '13 at 07:22
  • People looking for sample code for this solution, please download this ZIP : http://dl.dropbox.com/u/7874785/WebViewTest%202.zip – GDroid Feb 28 '13 at 10:43