I have a view controller in which I have created a simple contact app using Apple's addressbook, I have created another view controller which has a web view that create and displays all contacts and phonenumber when a user click on generate button. Now what I need is that, on generate the contacts should be hyperlinks, and when I click on any contact it should display the details of contact which I have done it in my 1st view controller. I know I have to use the delete of webview but not sure how to implement it, so far this is all I have written.
-(BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
ViewController *latestView = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
[self.navigationController pushViewController:latestView animated:YES];
return YES;
}
This is the function that generates and displays contacts in webview, in this I want to know how to add hyperlink along with the contact details
- (IBAction)createFileAction:(id)sender {
NSString *tweet, *phoneNumbers=@"", *temp, *allContacts=@"";
addressBooks =ABAddressBookCreate();
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBooks);
for (CFIndex i = 0; i < CFArrayGetCount(people); i++)
{
person12 = CFArrayGetValueAtIndex(people, i);
tweet=[[NSString stringWithFormat:@"%@",(__bridge_transfer NSString *)ABRecordCopyValue(person12, kABPersonFirstNameProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
//Appending Last Name
if(CFBridgingRelease(ABRecordCopyValue(person12,kABPersonLastNameProperty))!=NULL)
{
tweet=[tweet stringByAppendingString:@" "];
tweet=[tweet stringByAppendingString:[[NSString stringWithFormat:@"%@",(__bridge_transfer NSString *)ABRecordCopyValue(person12, kABPersonLastNameProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
}
tweet=[@"<h5>" stringByAppendingString:[NSString stringWithFormat:@"%@",tweet]];
if(CFBridgingRelease(ABRecordCopyValue(person12,kABPersonFirstNameProperty))!=NULL)
{
ABMultiValueRef multi = ABRecordCopyValue(person12, kABPersonPhoneProperty);
if(ABMultiValueGetCount(multi)!=0)
{
//For fetching multiple Phone Numbers
for (int i=0; i < ABMultiValueGetCount(multi); i++)
{
temp = (__bridge NSString*)ABMultiValueCopyValueAtIndex(multi, i);
phoneNumbers=[phoneNumbers stringByAppendingString:[NSString stringWithFormat:@"%@/",temp]];
}
//For Trimming characters in contacts (),-," "
NSCharacterSet *trim = [NSCharacterSet characterSetWithCharactersInString:@"()-\" \""];
phoneNumbers = [[phoneNumbers componentsSeparatedByCharactersInSet: trim] componentsJoinedByString: @""];
phoneNumbers=[phoneNumbers substringToIndex:[phoneNumbers length]- 1];
if([phoneNumbers rangeOfString:@"/"].location!=NSNotFound)
{
phoneNumbers = [phoneNumbers stringByReplacingOccurrencesOfString:@"/" withString:@" / "];
}
}
else
{
phoneNumbers=[phoneNumbers stringByAppendingString:@"No Numbers"];
}
tweet=[tweet stringByAppendingString:@" ---> "];
tweet=[tweet stringByAppendingString:[[NSString stringWithFormat:@"%@",phoneNumbers] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
phoneNumbers=@"";
NSLog(@"%@",tweet);
allContacts = [allContacts stringByAppendingFormat:@"%@</h5>",tweet];
}
}
CFBridgingRelease(people);
// To create file
[self.fileMgr createFileAtPath:file contents:nil attributes:nil];
[allContacts writeToFile:self.file atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];
//Test if file exists now that we have tried creating it
if([self.fileMgr fileExistsAtPath:self.file])
{
NSString *content = @"Contact list generated click on view to show the list";
[webview loadHTMLString:content baseURL:nil];
}
else
{
NSString *content = @"Contact list is not created yet, click on create to generate the contact list";
[webview loadHTMLString:content baseURL:nil];
}
}
Can you suggest the modifications to these two codes which will help me achieve the results??
If I understand you correctly, you want to have HTML links that open and instance of ViewController. Basically you can create any type of link <a href="myapp://{some_id_of_the_person}">click here</a>
, assuming you want to pass the person's id to your ViewController.
Then in [UIWebView webView:shouldStartLoadWithRequest:navigationType:]
check if request.URL.scheme
matches your scheme (myapp://
) in the example:
return NO
to the method). return YES
(assuming there might be some other standard links in the view that should be opened normally).