2

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?

Manted
  • 99
  • 2
  • 10

1 Answers1

7

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 :)

The iOSDev
  • 5,237
  • 7
  • 41
  • 78
  • can u give me more details plz? like where should I add these lines to? – Manted Dec 03 '12 at 05:35
  • does this is ok ? or need some more info ? :) – The iOSDev Dec 03 '12 at 05:39
  • I did what u told me. But the `[appDel.paramArray count]` in my `viewDidLoad` method returns `0`. When will the `- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url` be called? – Manted Dec 03 '12 at 06:04
  • does paramArray is one property with (strong, nonatomic) declared in appDelegate.h file? – The iOSDev Dec 03 '12 at 06:07
  • After the view is loaded and it is in appear state ie in viewDidAppear methed will be properly suited for this as I think :) – The iOSDev Dec 03 '12 at 06:10
  • make sure that the paramArray contains data at the application launch. use NSLog() to conforms that it contains proper data – The iOSDev Dec 03 '12 at 06:16
  • oh, `[appDel.paramArray count]` is not `0` anymore if I check it outside the `viewDidLoad` method. So u r right :) – Manted Dec 03 '12 at 06:17
  • Thanks buddy :) Glade to know that I am able to help you out. – The iOSDev Dec 03 '12 at 06:18
  • Thanks man, hope that I can help others in the future like what u did :)) – Manted Dec 03 '12 at 06:22
  • Yep It is true that you can in future same as me. some times before I am also same as you. And still looking for help from this site as much more experienced developers are on this site to help us like "new bee" developers, and as you get some experience you also able to help some other who is new to this. – The iOSDev Dec 03 '12 at 06:26