在ios里使用Bmob步骤

  1. 在项目根目录里建立文件:Podfile
  2. 输入内容:
platform :ios, '9.0'
target 'xxx' do  //项目名
    pod 'BmobSDK'     
end
  1. 在项目根目录执行 pod install
  2. 关闭xcode项目,重新进入项目根目录,点击编译后的文件打开xxx.xcworkspace来打开项目
  3. 随便新建个oc文件,会出线提示是否新建桥接文件, 选择建立, 然后在 桥接文件 内加入#import
  4. 在AppDelegate.swift注册申请的AppKey
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        Bmob.register(withAppKey: "xxxxx")
        return true
    }
  1. 增删改方法:
//创建方法. 每次执行都会生成一个objectid, 供其他方法使用.
    func save(){
        let gamescore:BmobObject = BmobObject(className: "GameScore")
        gamescore.setObject("赵三", forKey: "playerName")
        gamescore.setObject(90, forKey: "score")
        gamescore.saveInBackground { (isSuccessful, error) in
            if error != nil{
                print("error is \(error?.localizedDescription)")
            }else{
                print("success")
                print(BmobObject())
            }
        }
    }
    
    //更新方法
    func update() {
        let  gamescore:BmobObject = BmobObject(outDataWithClassName: "GameScore", objectId: "862b311786")
        gamescore.setObject(1222, forKey: "score")
        gamescore.updateInBackground { (isSuccessful, error) in
            if error != nil{
                print("error is \(error?.localizedDescription)")
            }else{
                print("success")
            }
        }
    }
    
    //删除方法
    func deleteGameScore()  {
        let  gamescore:BmobObject = BmobObject(outDataWithClassName: "GameScore", objectId: "4faf28f4dd")
        gamescore.deleteInBackground { (isSuccessful, error) in
            if error != nil{
                print("error is \(error?.localizedDescription)")
            }else{
                print("success")
            }
        }
    }
  1. 关于查询
    • 如果知道某条数据的objectId,而且想得知该条数据的内容,可以使用Bmoquery检索得到一个完整的BmobObject
    //查询单条数据
    func searchOneData(){
        let query:BmobQuery = BmobQuery(className: "yourDataBaseName")
        query.getObjectInBackground(withId: "yourDataBaseID") { (ojj, error) in
            if error != nil {
                //进行错误处理
            }else{
                if ojj != nil{
                    let playerName = ojj?.object(forKey: "title")as? String
                    let url  = ojj?.object(forKey: "url") as? String
                    print("playerName \(playerName),url  \(url)")
                    print("objectid   \(ojj?.objectId)")
                    print("createdAt  \(ojj?.createdAt)")
                    print("updatedAt  \(ojj?.updatedAt)")
                
                }
            }
        }
    }
* 根据表名,查询多条数据
    func searchAllData() {
        let query:BmobQuery = BmobQuery(className: "youhuiquan")
        query.findObjectsInBackground { (array, error) in

            var i =  0
            while(i<(array?.count)!){
                let obj = array?[i] as! BmobObject
                let playerName = obj.object(forKey: "title") as? String
                let url = obj.object(forKey: "url") as? String

                print("playerName \(playerName)")
                print("url \(url)")
                print("objectid   \(obj.objectId)")
                print("createdAt  \(obj.createdAt)")
                print("updatedAt  \(obj.updatedAt)")
                i = i + 1
            }
        }
    }

默认情况下,系统实际上并不会返回所有的数据,而是默认返回10条数据记录,你可以通过setLimit方法设置返回的记录数量。更多细节可点击查看查询一节中的分页查询。

  • 当查询的是用户表这种系统表的时候,返回的是BmobUser的数组,设备表,角色表也是这样的。
  • 查询用户表,设备表、角色表为:
let query:Bmoquery = BmobUser.query() //用户表let 
query:Bmoquery = BmobInstallation.query() //设备表let 
query:Bmoquery = BmobRole.query() //角色表

在ios端使用Bmob数据库里的图片

@IBOutlet weak var imageUI: UIImageView!

func searchOneImage(){
        let query:BmobQuery = BmobQuery(className: "xxxxxx")
        query.getObjectInBackground(withId: "xxxxx") { (ojj, error) in
            if error != nil {
                //进行错误处理
            }else{
                if ojj != nil{

                    //获取云数据库里image字段的内容
                    let image  = ojj?.object(forKey: "image") as? BmobFile
                    
                    //获取图片的url
                    let urlStr = NSURL(string: (image?.url)!)

                    //加载到ImageView控件
                    let data = NSData(contentsOf: urlStr! as URL)
                    self.imageUI.image  = UIImage(data: data! as Data)
              
                }
            }
        }
    }

你可能感兴趣的:(在ios里使用Bmob步骤)