3***@qq.com
3***@qq.com
  • 发布:2017-02-21 15:15
  • 更新:2018-04-09 15:28
  • 阅读:1942

iOS调用系统通讯录

分类:5+ SDK

SysContacts.js


 // Created by Ugen on 17/2/21.  
(function(window) {  
    var m = {};  

    document.addEventListener("plusready", function () {  
// 声明的JS“扩展插件别名”  
        var _BLECODE = 'SysContacts',  
            B = window.plus.bridge;  
        var sysContacts =  
        {  
// 声明异步返回方法  
            callSysContacts: function (successCallback,errorCallback) {  
                              var success = typeof successCallback !== 'function' ? null : function (args) {  
                              successCallback(args);  
                              }, fail = typeof errorCallback !== 'function' ? null : function (code) {  
                              errorCallback(code);  
                              };  
                callbackID = B.callbackId(success, fail);  
                return B.exec(_BLECODE, "callSysContacts", [callbackID]);  
            }  
        };  
        window.plus.sysContacts = sysContacts;  
    }, true);  

    m.callSysContacts = function (success,failure) {  
    plus.sysContacts.callSysContacts(function (ret) {  
                     success(ret);  
           },function (err) {  
                     failure(err);  
        });  
    };  

    window.SysContacts = m;  

})(window);

SysContactsPlug.h

 #import <UIKit/UIKit.h>  
#import "PGPlugin.h"  
#import "PGMethod.h"  

@interface SysContactsPlug : PGPlugin  

- (void)callSysContacts:(PGMethod *)command;  

@end

SysContactsPlug.m

#import "SysContactsPlug.h"  
#import <Contacts/Contacts.h>  
#import <ContactsUI/ContactsUI.h>  

@interface SysContactsPlug()<CNContactPickerDelegate>  

    @property (nonatomic, strong) NSString *callbackID;  

@end  

@implementation SysContactsPlug  

- (void)callSysContacts:(PGMethod *)command{  
    NSString *callbackId = [command.arguments objectAtIndex:0];  
    self.callbackID = callbackId;  
    NSLog(@"调用系统通讯录");  
    // 1.创建选择联系人的控制器  
    CNContactPickerViewController *contactVc = [[CNContactPickerViewController alloc] init];  
    // 2.设置代理  
    contactVc.delegate = self;  
    // 3.弹出控制器  
    [self presentViewController:contactVc animated:YES completion:nil];  

}  

- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker  
{  
        NSLog(@"取消选择联系人,这里返回PDRCommandStatusError");  
}  

- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact  
    {  

        NSString *lastname = contact.familyName;  
        NSString *firstname = contact.givenName;  
        NSString *fullname = [NSString stringWithFormat:@"%@ %@", lastname, firstname];  

        NSMutableArray *numberStrs = [NSMutableArray array];  

        NSArray *phoneNums = contact.phoneNumbers;  
        for (CNLabeledValue *phone in phoneNums) {  
            CNPhoneNumber *phoneNumer = phone.value;  
            NSString *phoneNumber = phoneNumer.stringValue;  
            [numberStrs addObject:phoneNumber];  
        }  
        NSMutableDictionary *backDict = [NSMutableDictionary dictionary];  
        [backDict setObject:numberStrs forKey:fullname];  

        PDRPluginResult *pluginResult= nil;  

        pluginResult = [PDRPluginResult resultWithStatus:PDRCommandStatusOK messageAsDictionary:backDict];  
        [self toCallback:self.callbackID withReslut:[pluginResult toJSONString]];  

    }  

@end  

sysContacts.html

<!doctype html>  
<html>  

    <head>  
        <meta charset="UTF-8">  
        <title></title>  
        <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />  
        <link href="./css/mui.min.css" rel="stylesheet" />  
        <link rel="stylesheet" href="./css/common.css" type="text/css" charset="utf-8"/>  
        <script src="./js/mui.min.js"></script>  
        <script src="js/common.js"></script>  
        <script src="js/SysContacts.js"></script>  
        <script type="text/javascript"></script>  
        <script>  
            function sysContacts(){  

                SysContacts.callSysContacts(function(ret){  
                    alert(JSON.stringify(ret));  
                },function(err){})  
            }  
        </script>  
    </head>  

    <body>  

        <header class="mui-bar mui-bar-nav">  
            <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>  
            <h1 class="mui-title">调用系统通讯录</h1>  
        </header>  

        <div id="dcontent" class="dcontent">  
            <br/>  
            <br/>  
            <br/>  
            <br/>  
            <div id="oauth">  

            </div>  
            <br/>  
            <div class="button" onclick="sysContacts()">调用系统通讯录</div>  
        </div>  

        <div id="output">  
            获取系统通讯录  
        </div>  
        </body>  
        <script type="text/javascript" src="js/immersed.js" ></script>  

    </body>  

</html>
2017-02-21 15:15 负责人:无 分享
已邀请:
摩西bfjr

摩西bfjr

赞,一字没改的情况下调试正常!

该问题目前已经被锁定, 无法添加新回复