0

我有一个视图控制器,在其中我使用 Apple 的地址簿创建了一个简单的联系人应用程序,我创建了另一个视图控制器,它具有一个 Web 视图,当用户单击生成按钮时,它创建并显示所有联系人和电话号码。现在我需要的是,在生成联系人时应该是超链接,当我单击任何联系人时,它应该显示我在第一个视图控制器中完成的联系人详细信息。我知道我必须使用 webview 的删除,但不知道如何实现它,到目前为止,这就是我所写的全部内容。

-(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;
}

这是在 webview 中生成和显示联系人的功能,在此我想知道如何添加超链接以及联系人详细信息

- (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];
        }



}

您能否建议对这两个代码进行修改以帮助我实现结果?

4

1 回答 1

1

如果我理解正确,您希望打开 HTML 链接和 ViewController 的实例。基本上你可以创建任何类型的链接<a href="myapp://{some_id_of_the_person}">click here</a>,假设你想将人的 id 传递给你的 ViewController。

然后在示例中[UIWebView webView:shouldStartLoadWithRequest:navigationType:]检查是否request.URL.scheme与您的方案 ( myapp://) 匹配:

  • 如果是,则解析该人的 id 的 URL,并为该人打开您的 viewController(以及return NO该方法)。
  • 如果不只是return YES(假设视图中可能有一些其他标准链接应该正常打开)。
于 2013-08-07T14:36:45.810 回答