How to pass data from AppDelegate to ViewControlle

2019-05-28 12:34发布

I'm using Safari to browse a webpage. After I click a button on this page, my Ipad will launch my app. So I implement the method - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url in the AppDelegate.m.

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
    if (!url) {  return NO; }   
    NSString *URLString = [url absoluteString];
    self.paramArray = [URLString componentsSeparatedByString:@"@"];
    NSLog(@"%d",[self.paramArray count]);
    for (int i = 1; i < [self.paramArray count]; i++) {
        NSLog([self.paramArray objectAtIndex:i]);
    }
    return YES;
}

The url I used on the webpage was some thing like myapp://@first_part@second_part@third_part. self.paramArray stores the substrings of url (myapp:// first_part second_part third_part).

Now I want to show these strings in the textfields in my ViewController. How can I pass this NSArray to the ViewController?

1条回答
Animai°情兽
2楼-- · 2019-05-28 12:57

This is just a few line of code, required to accomplish it.

Put this line in appDelegate.h file

First of all you need to conforms to a protocol as follow

@interface AppDelegate : UIResponder <UIApplicationDelegate>

Now need to declare one property for it to use as follow

@property (strong, nonatomic) id<UIApplicationDelegate>delagete;

Now the last and final stage to use the property in view controller is as follow

In .h file of viewcontroller
First put reference to appdelegate like this #import "AppDelegate.h"
Then one iVar as AppDelegate *appDel; in @interface

Now in .m file
put appDel = [[UIApplication sharedApplication]delegate]; in view did load method and access the appdelegate's all property there like appDel.paramArray.

Happy Coding :)

查看更多
登录 后发表回答