2016-04-15 61 views
0

我有這樣的代碼,要求委託人打開由URL標識的資源,並希望將此代碼放在AppDelegate文件之外,即在不同的文件中:請代表打開由AppDelegate外部的URL標識的資源

-(BOOL)application:(UIApplication *)application 
       openURL:(NSURL *)url 
    sourceApplication:(NSString *)sourceApplication 
      annotation:(id)annotation { 

     NSLog(@"url --> %@", url); 
     //Do something... 
     return YES; 
    } 

它工作正常,如果它在AppDelegate,但如果我在另一個文件中有此方法,它將無法運行。

爲了給出更多關於爲什麼我使用這種方法的內容,涉及到一個認證過程。因此,在我的應用程序的某個時候,Safari會打開並提示用戶使用某些憑據。 然後我用URL Scheme +一些信息啓動我的應用程序,例如myurl://someinfo

+0

這是不是很清楚你想要什麼。你能解釋一下嗎? –

+0

當我打開我的應用程序使用URL計劃這個函數被調用。但只有在我的AppDelegate.m文件中才有效。我想使它在同一目錄中的另一個文件中工作。 –

+0

啊,你有幾個選擇。例如,您可以使用自定義類定義委託對象。你有沒有嘗試使用'NSNotificationCenter'的通知?我相信這是一個更好的選擇。 –

回答

-1

將代碼留在AppDelegate中,而是在另一個類中使用該方法。

定義全球的AppDelegate在AppDelegate.h這樣

#define APP_DELEGATE ((AppDelegate *)[[UIApplication sharedApplication] delegate]) 

這樣做後,你可以調用AppDelegate.m的方法在代碼的任何地方,像這樣:

[APP_DELEGATE methodYouWantToCall]; 
+0

完全相反。他希望應用程序委託通知其他對象。換句話說:將該方法的業務邏輯委派給另一個對象。 –

+0

我以爲他想從AppDelegate文件之外運行該代碼。這就是他在第一行中所說的。 –

+0

是的,但讀了評論。它寫錯了。 –

1

好像使用NSNotificationCenter可能是您的要求的最佳選擇。

在AppDelegate中發佈使用NSNotificationCenter

-(BOOL)application:(UIApplication *)application 
      openURL:(NSURL *)url 
      sourceApplication:(NSString *)sourceApplication 
      annotation:(id)annotation 
{ 
    NSLog(@"url --> %@", url); 
    // Do something... 
    // 1 - Post recieved URL by usig NSNotificationCenter 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"APP_LAUNCHED_BY_URL_SCHEMA_NOTIFICATION" object:url]; 
    return YES; 
} 

,並在自定義類中的通知(例如:視圖控制器)添加以下代碼

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // 2 - Add observer to the notification name posted from AppDelegate 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidLaunchedByURLSchemaNotification:) name:@"APP_LAUNCHED_BY_URL_SCHEMA_NOTIFICATION" object:nil]; 
} 
-(void)appDidLaunchedByURLSchemaNotification:(NSNotification *)notification 
{ 
    // 3 - Here you can get the URL Object posted with notification,you will get this Callback on every launch of your application using Your Custom URL Scheme by other applications. 
    NSURL *url = notification.object; 
    NSLog(@"url --> %@", url); 
} 

請記住刪除添加的觀察者在自定義類的-dealloc

-(void)dealloc 
{ 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 

H2H,玩得開心編碼:)

0

這與Naresh的回答非常相似,因爲所有來自NSNotificationCenter的通知都以相同的方式工作。

基本上,您將選擇通知的名稱(例如:@"NOTIFICACION_NAME"),然後您需要接收該事件的每個對象都將訂閱具有該名稱的所有通知。

所以,在你的應用程序代理,你將發佈(例如,「廣播」)這個名字的通知:

-(BOOL)application:(UIApplication *)application 
     openURL:(NSURL *)url 
     sourceApplication:(NSString *)sourceApplication 
     annotation:(id)annotation 
{ 
    // First, create a dictionary with all the data needed to pass. 
    NSDictionary *notificationInfo = @{ 
             @"url"    : url, 
             @"sourceApplication" : sourceApplication, 
             @"annotation"  : annotation 
             }; 
    // Post the notification to all objects that listen to it. 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFICATION_NAME" 
                 object:self 
                 userInfo:notificationInfo]; 
} 

現在,我們需要趕在你需要它的其他文件通知。所以:

- (instancetype)init { 
    self = [super init]; 
    if (self) { 

    // Subscribe to the notifications called "NOTIFICATION_NAME" 
    // The "object" parameter is nil so we get notification from all objects that post that particular notification name. 
    // It could also be [[UIApplication sharedApplication] delegate] to receive notifications only from the app delegate. 
    // If you process the event in a UIViewController, do this in viewDidLoad instead of init. 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(openedWithURLScheme:) 
               name:@"NOTIFICATION_NAME" 
               object:nil]; 
    } 
} 

- (void)dealloc { 
    // Unsubscribe from all notifications at dealloc time. If you don't do this, 
    // your app will crash when the app delegate posts the notifications 
    // after this particular object was deallocated. 
    // If it's a UIViewController, do this in viewDidUnload. 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 

- (void)openedWithURLScheme:(NSNotification *)notification { 
    // The notification was received, so this method is called with the 
    // notification object as a parameter. Now we get the dictionary 
    // we sent with all the info. 
    NSDictionary *notificationInfo = notification.userInfo; 

    // Now get the data you need: 
    NSURL *url = notificationInfo[@"url"]; 
    NSString *sourceApplication = notificationInfo[@"sourceApplication"]; 
    id annotation = notificationInfo[@"annotation"]; 

    // Do what you need with that info... 
} 

在簡單的話:當你的應用程序代理接收到特定的方法,我們廣播接收到的「聽」(觀察)特定的通知每一個對象(通知)的信息。應該使用NSDictionary完成。

欲瞭解更多信息,請閱讀official documentation at Apple website