我正在为 SharePoint Online 开发一个应用程序,并希望在我的 ios 应用程序中使用 SharePoint Rest 界面。有人可以告诉我在 iOS 中使用 SharePoint Rest 界面的步骤吗
4 回答
我明白了,以下是要遵循的步骤:
在你的 ios 应用程序中包含 RestKit。
在您的主屏幕中创建一个 UIView 并加载登录页面。
在 UIWebView 中加载 http://server name/Pages/default.aspx
在 webViewDidFinished 方法中找出 Fed Auth 令牌并将其附加到请求 URL
- (void)webViewDidFinishLoad:(UIWebView *)webView { //Retrive HTTPOnly Cookie NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; NSArray *cookiesArray = [storage cookies]; //Search for Fed Auth cookie for (NSHTTPCookie *cookie in cookiesArray) { if ([[cookie name] isEqualToString:@"FedAuth"]) { /*** DO WHATEVER YOU WANT WITH THE COOKIE ***/ NSLog(@"Found FedAuth"); NSURL *url=[NSURL URLWithString:@"http://my server/_vti_bin/listdata.svc"]; RKClient *client = [RKClient clientWithBaseURL:url]; client.requestQueue.requestTimeout = 10; client.cachePolicy = RKRequestCachePolicyNone; client.authenticationType = RKRequestAuthenticationTypeHTTPBasic; client.username = @"username"; client.password = @"Password"; NSString *cookieVale=[cookie value]; NSString *getResourcePath=[@"?" stringByAppendingFormat:@"%@",cookieVale]; [client get:getResourcePath delegate:self]; break; } } }
在这里你可以找到答案。
- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response { id xmlParser = [[RKParserRegistry sharedRegistry] parserForMIMEType:RKMIMETypeXML]; NSError *error = nil; id parsedResponse = [xmlParser objectFromString:[response bodyAsString] error:&error]; RKLogInfo(@"Parsed response : %@, error:%@",parsedResponse,error); if ([response isSuccessful]) { NSLog(@"%d",[response isCreated]); // Response status was 200..299 if ([response isCreated] && [response isJSON]) { // Looks like we have a 201 response of type application/json RKLogInfo(@"The JSON is %@", [response bodyAsJSON]); } } else if ([response isError]) { // Response status was either 400..499 or 500..599 RKLogInfo(@"Ouch! We have an HTTP error. Status Code description: %@", [response localizedStatusCodeString]); } }
自我接受的答案让我失去了很多小时的试验和错误。它省略了一些关键方面,例如您还需要获取 rtFa cookie。用户代码中提供的 client.username = @"username" 和 client.password = @"Password" 是怎么回事。那是什么?请注意,客户端在任何时候都不知道用户名或密码...
无论如何,下面是一篇很棒的文章,它将引导您朝着正确的方向前进:http: //www.codeproject.com/Articles/571996/Development-of-iPhone-client-application-for-Share
这描述了如何在不使用 UIWebView 的情况下获取 cookie http://allthatjs.com/2012/03/28/remote-authentication-in-sharepoint-online/
将 FedAuth cookie 与所有后续请求一起发送。
通过身份验证后,您可以调用 REST API,此处的文档:http: //msdn.microsoft.com/en-us/library/fp142385 (v=office.15).aspx#bk_determining
当用户完成对 Office365 Sharepoint 实例的登录过程时,Web 视图将分几个步骤重定向。作为加载实际 Sharepoint 网站之前的最后一步之一,Web 视图将被要求加载“about:blank”。
检测您的 Web 视图何时开始加载“about:blank”,并且您知道用户何时完成登录过程并可以关闭 Web 视图。下面的示例代码。
// Load page in web view
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(@"WebView should start loading %@", request.URL.absoluteString);
// Detect that the user finished the sign in process
if ([request.URL.absoluteString isEqualToString:@"about:blank"]) {
// Do your stuff here
return NO;
}
return YES;
}
如果身份验证成功,Sharepoint 实例还将设置 FedAuth cookie。cookie 必须包含在以后对服务器的请求中。
您不必手动附加 cookie,只要 cookie 已被接受并存储在 NSHTTPCookieStorage 中并且您将请求发送到同一服务器,URL 加载系统就会处理此问题。
来自 Apple 文档
URL 加载系统会自动发送任何适合 NSURLRequest 的存储 cookie。除非请求指定不发送 cookie。同样,在 NSURLResponse 中返回的 cookie 根据当前的 cookie 接受策略被接受。