views:

233

answers:

1

Hi,

I want to know how to write a program in Objective-C language in XCode for iPhone OS, i want to call an application(Ex: hello world) from another application(its a calling application, whose functionality is just to call hello world application). how to give the path of hello world in the calling application and where to place my hello world application(i mean , should i import hello world in my project or just specify its URL in my calling application)..??Pls give a coding example???

+1  A: 

See: iPhone App Programming Guide

This is what your second application must do to handle an open call from the first:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    if ([[url scheme] isEqualToString:@"todolist"]) {
        ToDoItem *item = [[ToDoItem alloc] init];
        NSString *taskName = [url query];
        if (!taskName || ![self isValidTaskString:taskName]) { // must have a task name
            [item release];
            return NO;
        }
        taskName = [taskName stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        item.toDoTask = taskName;
        NSString *dateString = [url fragment];
        if (!dateString || [dateString isEqualToString:@"today"]) {
            item.dateDue = [NSDate date];
        } else {
            if (![self isValidDateString:dateString]) {
                [item release];
                return NO;
            }
            // format: yyyymmddhhmm (24-hour clock)
            NSString *curStr = [dateString substringWithRange:NSMakeRange(0, 4)];
            NSInteger yeardigit = [curStr integerValue];
            curStr = [dateString substringWithRange:NSMakeRange(4, 2)];
            NSInteger monthdigit = [curStr integerValue];
            curStr = [dateString substringWithRange:NSMakeRange(6, 2)];
            NSInteger daydigit = [curStr integerValue];
            curStr = [dateString substringWithRange:NSMakeRange(8, 2)];
            NSInteger hourdigit = [curStr integerValue];
            curStr = [dateString substringWithRange:NSMakeRange(10, 2)];
            NSInteger minutedigit = [curStr integerValue];

            NSDateComponents *dateComps = [[NSDateComponents alloc] init];
            [dateComps setYear:yeardigit];
            [dateComps setMonth:monthdigit];
            [dateComps setDay:daydigit];
            [dateComps setHour:hourdigit];
            [dateComps setMinute:minutedigit];
            NSCalendar *calendar = [NSCalendar currentCalendar];
            NSDate *itemDate = [calendar dateFromComponents:dateComps];
            if (!itemDate) {
                [dateComps release];
                [item release];
                return NO;
            }
            item.dateDue = itemDate;
            [dateComps release];
        }

        [(NSMutableArray *)self.list addObject:item];
        [item release];
        return YES;
    }
    return NO;
}

And the first app opens it like this:

NSURL *myURL = [NSURL URLWithString:@"todolist://www.acme.com?Quarterly%20Report#200806231300"];
[[UIApplication sharedApplication] openURL:myURL];
mahboudz
That's most of the story. The app being called also needs to register the URL protocol in its info.plist (in your example, with "todolist"), explained here: http://www.mobileorchard.com/apple-approved-iphone-inter-process-communication/
Justin Searls
Thanks a lots...Hey,but todolist is stored somewhere as directory in our local disk right??? then y to call its as ://www.acme.com?Quarterly%20Report#200806231300"];for instance if a save my todolist app in path ://macintosh HD/Users/home/todolist can i write the above code as NSURL URLWithString:@"todolist://://macintosh HD/Users/home/todolist"];plz tel me is it the right way to cal my todolist app ??
suse
hi Mahboudz,Where should i register URLtype... in todolist app or in the app which calls todolist???
suse
Register URL type in your todolist app's info.plist, as Justin mentioned above. See the Apple link for: "To register a URL type for your application, you must specify the subproperties of the CFBundleURLTypes property, which was introduced in “The Information Property List.” The CFBundleURLTypes property is an array of dictionaries in the application’s Info.plist file, with each dictionary defining a URL type the application supports." – mahboudz 0 secs ago
mahboudz
I believe that of the URL, only the "todolist:" is necessary to launch your app and the rest is for you to use for sending data to that app. You don't need to know the path to your app or anything like that.
mahboudz