2012-12-15 46 views
1

我開始使用Dropbox的SDK爲iOS和我看到的代碼來檢測,如果登錄成功與否是這樣的:的Dropbox SDK爲iOS - 檢測登錄取消

在AppDelegate中:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url 
{ 
    if ([[DBSession sharedSession] isLinked]) 
    { 
     // Success 
    } 
    else 
    { 
     // Failed 
    } 
    return YES; 
} 

在發生故障的情況下,如何識別原因?我想至少區分錯誤和取消。

+0

- (BOOL)應用:(UIApplication的*)應用handleOpenURL:(NSURL *)網址{ 如果([[DBSession sharedSession] handleOpenURL:URL]){ 如果([[DBSession sharedSession] isLinked]){ ALog(@「App linked successfully!」); //此時您可以開始進行API調用 } return YES; } \t return NO; } – Bala

+0

我沒有找到如何識別失敗的原因..但[UIWebViewDelegate的webView:didFailLoadWithError:](http://developer.apple.com/library/ios/documentation/uikit/reference/UIWebViewDelegate_Protocol/Reference/ Reference.html#// apple_ref/doc/uid/TP40006951-CH3-SW5)應該可以幫助你找到我認爲是的原因.. – Bala

+0

@Bala我使用DropBox SDK,我不控制UIWebView,所以我不能添加我自己的委託...除非有辦法這樣做? –

回答

3

要確定取消

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { 

    NSArray *components = [[url path] pathComponents]; 
    NSString *methodName = [components count] > 1 ? [components objectAtIndex:1] : nil; 
    if ([methodName isEqual:@"cancel"]) { 
     NSLog(@"Dropbox link Cancelled"); 
    } 
} 
+0

並看看這個鏈接http://stackoverflow.com/q/5643493/1059705 .. – Bala

+0

哈哈,我從來沒有想過看和URL。對於SDK來說,這是一種不常見和脆弱的方式,表示取消!我會盡快嘗試,並且接受答案。謝謝! –

+0

工程就像一個魅力!我甚至發現他們在SDK源代碼中發送的內容。謝謝! –

1

如果有人遇到這種並卡在巴拉的答案,那是因爲這個方法handleOpenURL是過時的。 Dropbox現在使用openURL。 openURL進入應用程序委託。

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url sourceApplication:(NSString *)source annotation:(id)annotation { 
if ([[DBSession sharedSession] handleOpenURL:url]) { 
    if ([[DBSession sharedSession] isLinked]) { 
     NSLog(@"App linked successfully!"); 
     // At this point you can start making API calls 
     // Send notification to load an initial root dropbox path 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"getDropboxRoot" object:self]; 
    }else{// Add whatever other url handling code your app requires here in this else 
     //if the user clicks cancel that will appear here in the methodName variable, 
     //we post a notification to wherever we want. 
     NSArray* components = [[url path] pathComponents]; 
     NSString *methodName = [components count] > 1 ? [components objectAtIndex:1] : nil; 
     if ([methodName isEqual:@"cancel"]) { 
      NSLog(@"Dropbox link Cancelled"); 
      [[NSNotificationCenter defaultCenter] postNotificationName:@"dropboxRegistrationCancel" object:self]; 
     } 
    } 
    return YES; 
} 
return NO;} 

基本上檢測取消您剛纔檢查URL組件字的「取消」,如果它發現你剛剛發送的地方適用於您的應用程序告訴它的用戶取消了進程的通知。

觀察者代碼爲您的通知發佈,把這個地方放在你想檢測上面發佈的通知。

下面這爲我工作的代碼,我已經使用
[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(dropboxRegistrationCancel) 
             name:@"dropboxRegistrationCancel" 
             object:nil]; 
-(void) dropboxRegistrationCancel{ 
/*do stuff here that you want to do when the @"dropboxRegistrationCancelled" is triggered*/} 
0

,它有助於在兩種情況下,如果用戶成功

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{ 
NSString *stringUrl = [url absoluteString]; 
if ([stringUrl containsString:@"cancel"]) { 

    // Handle if user cancelled the login 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"dropboxRegistrationCancel" object:self]; 

    return NO; 

} 
if ([[DBSession sharedSession] handleOpenURL:url]) { 
    if ([[DBSession sharedSession] isLinked]) { 
     // From below notification u can fetch your data from Dropbox 
     [[NSNotificationCenter defaultCenter] 
     postNotificationName:@"isDropboxLinked" 
     object:[NSNumber numberWithBool:[[DBSession sharedSession] isLinked]]]; 
// Add whatever other url handling code your app requires here 

    } 
    return YES; 
} return NO; 
} 

取消Dropbox的登錄名或用戶登錄來進行登錄後發生此代碼中獲取文件第一次在您的課堂,你是顯示文件列表中viewDidLoad中

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(isDropboxLinkedHandle:) name:@"isDropboxLinked" object:nil]; 

和實施isDropboxLinkedHandle:

- (void)isDropboxLinkedHandle:(id)sender { 
if ([[sender object] intValue]) { 
    // fetch all files 
    [[self restClient] loadMetadata:@"/"]; 
} 
} 

我希望這將幫助表示感謝。