Lauching App with URL (via UIApplicationDelegate's handleOpenURL) working under iOS 4, but not under iOS 3.2

后端 未结 4 1889
终归单人心
终归单人心 2020-12-12 16:43

I have implemented UIApplicationDelegate\'s

application:didFinishLaunchingWithOptions:

and

application:handleOpenURL:
         


        
4条回答
  •  一生所求
    2020-12-12 17:31

    Add the following to the end of application:DidFinishLaunchingWithOptions:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
        ...    
    
        NSURL *url = (NSURL *)[launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];
        if (url != nil && [url isFileURL]) {
            return YES;
        }  else return NO;
    } // End of application:didFinishLaunchingWithOptions:
    
    // New method starts 
    -(BOOL) application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
    {
        mvc = [nc.viewControllers objectAtIndex:0];
        if (url != nil && [url isFileURL]) {
            [mvc handleOpenURL:url];
        }
        return YES;
    }
    

    where mvc is my main ViewController, and nc my navigation controller.

    Then in the MainViewController, do something like this:

    - (void)handleOpenURL:(NSURL *)url {
        [self.navigationController popToRootViewControllerAnimated:YES];
    
        // Next bit not relevant just left in as part of the example
        NSData *jsonData = [NSData dataWithContentsOfURL:url];        
        NSError *error;
        NSDictionary *dictionary = [[NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error] objectAtIndex:0];
        [self managedObjectFromStructure:dictionary withManagedObjectContext:self.context];
        ...
    }
    

    after declaring handleOpenURL in the .h of course.

    Thanks goes to Christian for putting in the effort for this.

提交回复
热议问题