简体   繁体   English

在UIWebView iPhone iOS XCode中打开SharePoint URL-将凭据嵌入URL请求中,以避免提示输入凭据

[英]Open SharePoint URL in UIWebView iPhone iOS XCode - Embedding credential into URL request to avoid prompt for credential

**I am new to iOS development. **我是iOS开发的新手。 I am developing small application which can open specified SharePoint site URL without manually passing require credential. 我正在开发一个小型应用程序,它可以打开指定的SharePoint网站URL,而无需手动传递要求的凭据。 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. 我尝试打开的URL需要凭据,但我想将这些凭据嵌入到我将要打开的UIWebView控件中的请求中。 I don't want to open the URL in Safari. 我不想在Safari中打开URL。

Would you please help me finding the solution?** 您能帮我找到解决方法吗?**

You can use -connection:didReceiveAuthenticationChallenge: delegate for your problem. 您可以使用-connection:didReceiveAuthenticationChallenge:代表您的问题。 First make a normal NSURLConnection as follow, 首先按照以下方式建立一个普通的NSURLConnection

- (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; @属性(强,非原子)IBOutlet UIWebView * webView;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM