[英]Open SharePoint URL in UIWebView iPhone iOS XCode - Embedding credential into URL request to avoid prompt for credential
**我是iOS開發的新手。 我正在開發一個小型應用程序,它可以打開指定的SharePoint網站URL,而無需手動傳遞要求的憑據。 我嘗試打開的URL需要憑據,但我想將這些憑據嵌入到我將要打開的UIWebView控件中的請求中。 我不想在Safari中打開URL。
您能幫我找到解決方法嗎?**
您可以使用-connection:didReceiveAuthenticationChallenge:
代表您的問題。 首先按照以下方式建立一個普通的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];
}
之后,您會收到回電。 在這里,您應該應對憑據的挑戰。
- (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];
}
}
更新使用此鏈接的代碼。
-(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]
}
在頭文件中
@interface yourViewController : UIViewController<UIWebViewDelegate>{
Bool _authed;
}
@屬性(強,非原子)IBOutlet UIWebView * webView;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.